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.


Bookmark and Share:
These icons link to social bookmarking sites where readers can share and discover new web pages.

  • Digg
  • Furl
  • del.icio.us
  • StumbleUpon
  • MisterWong
  • DZone
  • Technorati

Tags: , , ,

4 Comments to “How to Send E-Mail in PHP: A “Share This” Form”

  1. ricardo said this on

    hello there how are you?

  2. martin said this on

    tesing this site

  3. monica said this on

    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!

  4. Walkere said this on

    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.

Leave a Reply

buy cheap viagra buy cheap viagra online buy discount viagra online buy viagra from india buy online viagra buy viagra india buy viagra online cheap viagra cheap viagra online discount viagra discount viagra online viagra india rx get viagra online non prescription viagra order viagra order viagra india order viagra online viagra for sale viagra no prescription viagra on line buy viagra online no prescription best price for viagra buy cheapest viagra buy generic viagra buy generic viagra from india online buy sildenafil citrate buy sildenafil citrate online buy viagra cheap buy viagra cheap prices buy viagra online without prescription cheapest prices on generic viagra cheapest viagra cheapest viagra prices cheap generic viagra cheap sildenafil citrate discount viagra canada email viagra get viagra no prescription get viagra without prescription low price viagra mail order viagra mastercard viagra order generic viagra order sildenafil citrate viagra 50/100mg viagra online without prescription viagra order online viagra sildenafil citrate viagra soft tabs viagra soft tabs for sale viagra soft tabs online viagra soft tabs without prescription buy viagra soft tabs cheap viagra soft tabs discount viagra soft tabs generic viagra soft tabs order viagra soft tabs buy cheap kamagra buy cheap kamagra online buy discount kamagra online buy online kamagra buy kamagra india buy kamagra online cheap kamagra cheap kamagra online discount kamagra discount kamagra online get kamagra online non prescription kamagra order kamagra order kamagra india order kamagra online kamagra for sale kamagra no prescription kamagra on line buy kamagra online no prescription best price for kamagra buy cheapest kamagra buy generic kamagra buy kamagra cheap buy kamagra cheap prices buy kamagra online without prescription cheapest prices on generic kamagra cheapest kamagra cheapest kamagra prices cheap generic kamagra discount kamagra canada email kamagra get kamagra no prescription get kamagra without prescription low price kamagra mail order kamagra mastercard kamagra order generic kamagra kamagra online without prescription kamagra order online