How to Send E-Mail in PHP: A “Share This” Form
This question comes up a lot in forums - how do you send e-mail in PHP?
There are plenty of reasons you’d want to do this. Perhaps you want to send a newsletter to your users. Or you want people to be able to leave feedback through e-mail. Or you just want your users to be able to e-mail the page (or a link to it) to a friend.
How do we do this? The mail() function.
While there are more in depth and robust ways to send e-mail with PHP, the simplest method is to use the mail() function. In most cases this is all you need to send plain text e-mail to a number of recipients.
Let’s see how this function works by creating a form that lets the user “Share This Article” - e-mail a link of the current article to a friend.
Examining the Mail Function
First, we’ll take a look at the mail function. It’s pretty simple.
bool mail($to, $subject, $content, $headers)
mail() returns a boolean value - true for success, false for failure.
The first three parameters are pretty simple. $to is the sending e-mail address (your user). $subject is the subject line of your message (one line - no linebreaks!). $content is a string containing the text of the message.
$headers is optional. It can be used to add additional headers - such as cc: or bcc: lines. You can also use the $headers to add a “From:” - otherwise the standard sending e-mail addressing in the php.ini file is used.
Building the Form
Before we get to the script processing, let’s build a quick HTML form to get some user input.
The form should give us the sender’s e-mail address and name along with the recipient’s e-mail address and name. For now, we’ll hard-code the subject and message, although this could be gathered in the form as well.
<form method="post" action="share.php"> <input type="text" name="sendname" id="sendname" /><br /> <input type="text" name="sendaddress" id="sendaddress" /><br /> <input type="text" name="recname" id="recname" /><br /> <input type="text" name="recaddress" id="recaddress" /><br /> <input type="submit" value="Send Message" /> </form>
That should gather the four pieces of input for us.
Processing the Input and Sending the Message
Now we can add build a quick php snippet to process the form input, send it to the mail() function, and let the user know if the mail was sent or not. For the sake of brevity, I won’t include a lot of error checking and validation - but you should if you’re implementing this for real.
$sendName = $_POST['sendname']; $sendAddress = $_POST['sendaddress']; $recName = $_POST['recname']; $recAddress = $_POST['recaddress']; // Get the URL of the page we're on $pageURL = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; // Build the message $content = "Hey $recName, I found this <a href='$pageURL'>cool article</a> on Web Cash. Why don't you read it and tell me what you think. Later, - $sendName"; $to = $recAddress; $subject = "Cool Article on Web Cash - Check It Out!"; $headers = "From: $sendAddress"; if ( mail($to, $subject, $content, $headers) ) { echo "Mail was sent successfully!"; } else { echo "There was an error. Doh!"; }
And that’s about all there is to it…
Jazzing It Up
Once you understand how the mail() function works, you can improve the form a lot.
You could allow the user to input their own message, using a textarea. You could also allow them to enter a custom subject in a text field.
You could allow multiple recipients with extra text fields. To do this, separate each e-mail address with a , and a space - like “joe@bob.com, bob@joe.com” - in the $to variable.
A few words of caution, though.
First, you’ll often run into trouble using the mail() function on a local server. If you do, I’d suggest testing your script on a remote server before you pull your hair out for no reason.
Second, beware of spam. This is a good exercise to go through to understand the mail() function, but you should be aware that people do use forms to spam people. You may want to find a captcha module and use that to verify your users are real people - or you could inadvertently be spamming the world with email about your articles.







ricardo said this on February 17th, 2008 at 7:01 am
hello there how are you?
martin said this on April 16th, 2008 at 4:58 pm
tesing this site
monica said this on April 20th, 2008 at 5:56 pm
Cool! I love this form and it works great. However — is there a way to send the url of the previous page rather than the page the form is on? Something to replace this line:
$pageURL = ‘http://’ . $_SERVER[’SERVER_NAME’] . $_SERVER[’REQUEST_URI’];
I’m teaching myself PHP so I haven’t yet found a solution on my own. Thanks!
Walkere said this on April 21st, 2008 at 6:53 pm
You could try using the variable $_SERVER[’HTTP_REFERER’]. This should contain the URL of the referring page.
I’m guessing you want to create a link from a content page to a script that handles the e-mailing. If so, you could also add a $_GET variable to the URL and send that to the script.