PHP Mail
You can use PHP to dynamically send emails to one or more recipients. This can be handy for a lot of reasons, for example:
- Sending newsletters to a mailing list
- Sending a "welcome" email to new members of your website
- A user has just made a purchase and you need to send them a receipt by email
- Sending an email alert to your technical administrator whenever an error occurs
- Many more reasons...
The PHP mail() Function
To send email using PHP, you use the mail() function. This accepts 5 parameters as follows (the last 2 are optional)
mail(to,subject,message,headers,parameters)
Below is an explanation of the parameters.
| Parameter | Description |
|---|---|
| to | Required. The recipient's email address. |
| subject | Required. The email's subject line. |
| message | Required. The actual email body. |
| headers | Optional. Additional header fields such as "From", "Cc", "Bcc" etc. |
| parameters | Optional. Any additional parameters. |
Sending an Email
You could send email by simply doing this:
mail("homer@quackit.com",
"Thank you for registering!",
"Hello Homer, thank you for registering!",
"From: ian@quackit.com");
Although, in reality, you would probably set your parameters up as variables. Also, if the email was triggered by a user, you would probably provide them with feedback to say that the email had been sent.
// Set up parameters
$to = "homer@quackit.com";
$subject = "Your password";
$message = "Hello Homer, thanks for registering. Your password is: springfield";
$from = "ian@quackit.com";
$headers = "From: $from";
// Send email
mail($to,$subject,$message,$headers);
// Inform the user
echo "Thanks for registering! We have just sent you an email with your password.";
HTML Emails
To send an HTML email, the process is the same, however, you need to provide additional headers (as well as an HTML formatted message).
Note that you need to separate each header with a carriage return.
For Windows systems, use this code:
// Set up parameters
$to = "homer@quackit.com";
$subject = "Your password";
$message = "<p>Hello Homer,</p>
<p>Thanks for registering.</p>
<p>Your password is: <b>springfield</b></p>
";
$from = "ian@quackit.com";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: $from" . "\r\n";
// Send email
mail($to,$subject,$message,$headers);
// Inform the user
echo "Thanks for registering! We have just sent you an email with your password.";
For UNIX systems, use this code:
// Set up parameters
$to = "homer@quackit.com";
$subject = "Your password";
$message = "<p>Hello Homer,</p>
<p>Thanks for registering.</p>
<p>Your password is: <b>springfield</b></p>
";
$from = "ian@quackit.com";
$headers = "MIME-Version: 1.0" . "\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\n";
$headers .= "From: $from" . "\n";
// Send email
mail($to,$subject,$message,$headers);
// Inform the user
echo "Thanks for registering! We have just sent you an email with your password.";
Difference Between UNIX and Windows Code?
You probably noticed in the above example that there's a Windows version and a UNIX version.
The only difference is in the way the carriage returns are specified. On Windows it's "\r\n", on UNIX it's "\n".
The PHP specification stipulates that you should use "\r\n" for creating the carriage returns. This should work fine on Windows systems. UNIX systems however, have a tendency to add "\r" to the "\n", therefore resulting in "r\r\n", which of course, wouldn't work. Therefore, we simply leave out the "\r" on the UNIX version.
Configuring Mail on PHP
The above steps assume that your PHP installation is configured to send mail. Your hosting provider should already have configured PHP to send mail, so you shouldn't need to do anything further if your website is hosted with a third party hosting provider.
If you need to send mail from your local computer, you may need to configure PHP to send mail. The next lesson explains this.
