Using The PHP mail Function

Ok been a while since I posted anything so thought I would post my tutorial on using the PHP mail() function. I have basically copied my tutorial word for word from the Lazarus forum.

This is a brief (or not) tutorial on using the PHP mail() function to send emails from your scripts.

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

Let’s break that down. The bool means that the PHP mail function returns either true or false depending on if it was successful or not. The rest is pretty self explanatory. string $to is where we put the email address we want to send to. string $subject is where we put the emails subject and string $message is where we put our message. The string part just means that this variable will be treated as a string when used. We will cover $additional_headers a bit later and won’t even touch on $additional_parameters.

Ok so we want to send an email to Bill Gates with the subject Windows Linux and our message is When is it coming out?. The function we would use looks like this

<?php
mail(’[email protected]’, ‘Windows Linux’, ‘When is it coming out?’);
?>


This would suit our needs but there are some things you need to know. Firstly this email will be sent as plain text so any HTML tags in the message would be displayed in the message. Second this email would have our servers address as the from address. Before I continue I would like to just briefly touch on the point that you can send to multiple address by putting them in the to section separated by a comma like ‘[email protected],[email protected]’. So now let us make it appear to come from our email address [email protected].

<?php
mail(’[email protected]’, ‘Windows Linux’, ‘When is it coming out?’, ‘From: [email protected]’);
?>

As you can see we added a header to our email. The header is From and says what address the email was sent from. We could also use the format From: "Our Name" <[email protected]> so that it also has our name. This is fine and would suit most of your email sending needs but what if you want to send HTML? Then we need some more headers. They are MIME-Version and Content-type. So now our function looks like:

<?php
mail(’[email protected]’, ‘Windows Linux’, ‘When is it coming out?’, "From: [email protected]\nMIME-Version: 1.0\nContent-type: text/html; charset=iso-8859-1");
?>

You will see I switched from using single quotes to double for the headers. This is because we need to separate the different headers with a new line marker (\n) and this does not work when using single quotes. I won’t say a lot about what the MIME type means. You can read up on that HERE. The Content-type says that the email is sent as text but is to be treated as HTML. Part of the Content-type is charset. This tells the email reader what character set to use when displaying the email. You will only have to change this if you are sending your email in a language other than English. Now when our recipient reads the email the word When will be bold.

You can also use the headers section to send carbon copies and blind carbon copies of the email to people by using CC: emailaddress and BCC: emailaddress respectively. You still need a main email address to send to though for these to work.

Now some might argue that you should use \r\n for separating the headers but in my experience just \n on it’s own is fine and will be understood by all email clients where as \r\n may cause problems.

Just a few notes.

If you are ending an email in a language other than English you may need to set the character set even for plain text. This is done the same as above but just replace the text/html with text/plain.

You can use variables for the relevant parts of the email. For example.

<?php
$recipient = ‘[email protected]’;
$subject = ‘Windows Linux’;
$message = ‘When is it coming out?’;
$headers = ‘From: [email protected]’;

mail($recipient, $subject, $message, $headers);
?>

As I also said earlier the mail function returns TRUE or FALSE depending on if it is successful in sending or not. So we can add some error checking like

<?php
if (@mail(’[email protected]’, ‘Windows Linux’, ‘When is it coming out?’, ‘From: [email protected]’))
{
echo ‘Your mail was successfully sent’;
}
else
{
echo ‘There was a problem and the email did not get sent’;
}
?>

You may wonder what the @ is for in front of the mail. The @ before a function tells PHP not to display any error messages that may result from running that function. It just keeps things tidy as we are error checking anyway.

And that’s about it for the basic usage of the PHP mail() function. Next time I will explain how to send emails that are both HTML and plain text so that our recipient can read it regardless of their email reader. I will also explain about adding attachments to your emails and displaying attached images inside the body of the email.

Share and Enjoy:
  • Twitter
  • Facebook
  • Digg
  • MySpace
  • del.icio.us
  • LinkedIn
  • Google Bookmarks
  • Reddit
  • Slashdot
  • Technorati
  • StumbleUpon
  • Fark
  • MSN Reporter
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • E-mail this story to a friend!
  • Print this article!

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>