Mail is extremely easy to send from PHP, unlike using scripting languages which require special setup (like CGI). There is actually just one command, mail() for sending mail. In this tutorial we will discuss how to use PHP’s mail() function. It is used as follows:

mail($to,$subject,$body,$headers);

In this example I have used variables as they have descriptive names but you could also just place text in the mail command. Firstly, $to. This variable (or section of the command) contains the e-mail address to which the mail will be sent. $subject is the section for the subject of the e-mail and $body is the actual text of the e-mail.

The section $headers is used for any additional e-mail headers you may want to add. The most common use of this is for the From field of an e-mai but you can also include other headers like cc and bcc.

Sending An E-mail

Before sending your mail, if you are using variables, you must, of course, set up the variable content beforehand. Here is some simple code for sending a message:

$to = "[email protected]"; 
$subject = "PHP Is Great"; 
$body = "PHP is one of the best scripting languages around"; 
$headers = "From: [email protected]\n"; 
mail($to,$subject,$body,$headers); 
echo "Mail sent to $to";

This code will acutally do two things. Firstly it will send a message to ‘[email protected] with the subject ‘PHP Is Great‘ and the text: PHP is one of the best scripting languages around. The e-mail will be from ‘[email protected]’ and it will also output the following text: ‘Mail sent to [email protected] to the browser.

Formatting E-mail

Something you may have noticed from the example is that the From line ended with \n. This is acutally a very important character when sending e-mail. It is the new line character and tells PHP to take a new line in an e-mail. It is very important that this is put in after each header you add so that your e-mail will follow the international standards and will have no issues in being delivered.

The \n code can also be used in the body section of the e-mail to put line breaks in but should not be used in the subject or the tofield.

Mail Without Variables

The e-mail above could have been sent using different variable names (it is the position of the variables in relation to the commas, not the name of them which decides on their use). It could also have been done on one line using code like this:

mail("[email protected]","PHP Is Great","PHP is one of the best scripting languages around","From: [email protected]\n");

But that would make your code slightly harder to read.

Error Control

As anyone who has been scripting for a while will know, it is extremely easy to make mistakes in your code and it is also very easy to input an invalid e-mail address (especially if you are using your script for form to mail). Because of this, you can (and should) add in a small piece of code which will check if the e-mail is sent:

if(mail($to,$subject,$body,$headers)) { 
echo "An e-mail was sent to $to with the subject: $subject"; 
} else { 
echo "There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid"; 
}

This code is quite self explanitory. If the mail is sent successfully it will output a message to the browser telling the user, if not, it will display an error message with some suggestions for correcting the problem. Here is a complete mail script:

$to = "[email protected]"; 
$subject = "PHP Is Great"; 
$body = "PHP is one of the best scripting languages around"; 
$headers = "From: [email protected]\n";
if(mail($to,$subject,$body,$headers)) { 
echo "An e-mail was sent to $to with the subject: $subject"; 
} else { 
echo "There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid"; 
}

Published by Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Join the Conversation

2 Comments

Leave a comment

Your email address will not be published. Required fields are marked *