bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]])
Long before the world-wide web became popular, email already had a strong following amongst Internet users, and it is not surprising - having the ability to exchange messages and with people the world over has revolutionised the way we communicate. Being a web development language, PHP makes it remarkably easy to send basic email messages over the net, and, through its IMAP extension, PHP also makes it fairly straightforward to check email boxes and read mail.
The primary function for sending email is mail(), which takes three basic parameters, and one optional one. These parameters are, in order, the email address to send to, the subject of the message, the body of the message, and finally any extra headers you want to include.
We'll get back to the last parameter in a moment, but first let's take a look at the most basic type of mail() call:
<?php mail("email@example.com", "My Subject", "Hello, world!"); ?>
Edit the email address to point to your own email address, then try running the script. If you receive mailing errors or don't receive the test mail, you may well have installed PHP incorrectly.
Naturally it is possible to use variables in place of any of the parameters, like this:
<?php $mailaddress = "firstname.lastname@example.org"; $mailsubject = "My Subject"; $mailbody = "Hello, world!"; mail($mailaddress, $mailsubject, $mailbody); ?>
To make the email address textual, e.g. "A. Friend" rather than "email@example.com", you need to add both values into the mail address, like this:
<?php $mailtoname = "My Best Friend"; $mailtoaddress = "firstname.lastname@example.org"; $mailtocomplete = "$mailtoname <$mailtoaddress>"; mail($mailtocomplete, "My Subject", "Hello, world!"); ?>
With that new code, the email will appear to have been sent to "My Best Friend", which is much easier to read. The last parameter* is where you specify any number of additional email headers to send along with the email - these let you affect how the email looks, how it is parsed, and other key information. For example, we can specify who sent the email using the From header, we can specify who else should get the email using the CC and BCC headers, or we can specify that the email is to be treated as containing HTML. Each header sent in the third parameter needs to be separated by a carriage return and new line, and not just a new line. That is, only \r\n should be used to separate the various parameters, and not any other combination.
Author's Note: * This is not strictly true - there is another parameter to mail() that is very rarely used, which allows you to send parameters directly to sendmail if you are using Unix. This is not covered here, and you will need to check the manual for more information.
Here is a script that sends a HTML mail from a given email address:
<?php $message = "<B>This is a <I>test</I></B>"; $headers = "From: email@example.com\r\nContent-type: text/html\r\n"; mail("firstname.lastname@example.org", "Testing", $message, $headers); ?>
That should send a message with the text all in bold, and the word "test" in italics. Of course, the key part is the content inside $headers - we set From so that it appears to be from email@example.com, then add a carriage return and a new line, and finally send a Content-type header of text/html, which should make the email client display it as HTML. Because HTML emails allow potentially unsafe content, many email clients (such as KMail on Linux) will stop HTML emails being displayed by default, and will instead display a warning - you should be aware of this, and really only use HTML email if it is absolutely necessary.
The final thing to learn about sending mail is that you can send to (and from) fully formed email addresses that include the person's name, as well as their email address. This is in common use, which is why you get emails from "Joe Bloggs", rather than "firstname.lastname@example.org".
To do this, you need to print the full name of the person, followed by their email address in angle brackets, like this:
$headers = "From: Mr Foo Bar <email@example.com>";
Using this same method you can add these fully formed email address to the recipient's addresses also. So, here's a complicated email script that emails three people, using To, CC, and BCC, as well as setting the From header, and also using fully formed email addresses for the lot:
<?php $mailto = "My Best Friend <firstname.lastname@example.org>"; $mailfrom = "Joe Bloggs <email@example.com>"; $mailcc = "My Best Friend2 <firstname.lastname@example.org>"; $mailbcc = "My Best Friend3 <email@example.com>"; mail($mailto, "My Subject", "Hello, world!", "From: $mailfrom\r\nCC: $mailcc\r\nBCC: $mailbcc\r\n"); ?>
Copyright ©2015 Paul Hudson. Follow me: @twostraws.