Previously I've discussed sending a simple eMail with PHP. At its heart, the PHP mail() function requires just three arguments:

<?php mail(to,subject,message); ?>

You'll note that the basic function is extremely sparse: there's not even information as to whom the eMail is from! (In fact the mail function will use the server administrator's eMail address as the default "Sent from" address for all eMail; as you will see, we can set the "from" address to any address we like, which is another reason to never trust an eMail based solely on who it appears to come from).

To add a sender, or to set the message body to HTML, as well as many other eMail options, we must use the fourth optional argument, header.

header is, in many ways, the trickiest part of sending an eMail via PHP: it must be formatted in exactly the right manner in order to achieve what we want. Because of the possible length and complication of the header section, we will often use the special concatenation method I mentioned in the earlier article to build up the header details: pay close attention to the use of periods, particularly before equals signs, in the code that follows.

As before, I'll form some variables before adding them into the mail function; this time, I'll add a $header variable and a matching argument to modify the sender information for the eMail. (And as before, you should check that your server has the mail function enabled, or if its use has any special tricks or conditions, before trying this code).

<?php
$mailto = "youremailaddress@domain.com";
$mailsubject = "New eMail";
$mailmessage = "This is a new mail message.";
$mailheader = "From: sender@otherdomain.com\r\n";
$mailheader .= "Reply-To: sender@otherdomain.com\r\n";
mail($mailto,$mailsubject,$mailmessage,$mailheader); ?>

\r\n are escape characters, used to create a return and force a new line, respectively. You will need them after every new section added to the header. You can also use these characters as part of the $message variable to create new lines in the body of the eMail. (Alternatively you could format the message as HTML by further manipulation of the header section, using standard markup in the message body such as <p> to gain new lines (see below).)

Note that the "From" and "Reply-To" eMail addresses may be different: the latter, if specified, is the address a reply eMail will be sent to.

If you wanted to carbon-copy or blind carbon copy the eMail to a third party, add the following:

<?php
$mailheader = "From: sender@otherdomain.com\r\n";
$mailheader .= "Reply-To: sender@otherdomain.com\r\n";
$mailheader .= "CC: sombodyelse@domain.com\r\n";
$mailheader .= "BCC: hidden@domain.com\r\n"; ?>

To format the eMail as HTML:

<?php
$mailheader = "From: sender@otherdomain.com\r\n";
$mailheader .= "Reply-To: sender@otherdomain.com\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
$mailheader .= "Content-Type: text/html; charset=utf-8\r\n"; ?>

You can then format the eMail message in HTML:

<?php
$mailmessage = '<html><body>';
$mailmessage .= '<img src="http://thenewcode.com/assets/svg/favicon.svg" alt="The New Code" />';
$mailmessage .= '<h1>A Message from The New Code</h1>';
$mailmessage .= '<p>Message body text</p>';
$mailmessage .= '</body></html>'; ?>

(Note the use of an absolute path for the image src value, necessary due to the fact that the eMail may be viewed from anywhere.)

Enjoy this piece? I invite you to follow me at twitter.com/dudleystorey to learn more.