demosthenes.info

A blog by Dudley Storey on , , , , , , and anything else that strikes his fancy.

featured articles

popular favourites

PHP eMail Headers

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

  1. <?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).

  1. <?php $mailto = "youremailaddress@domain.com";
  2. $mailsubject = "New eMail";
  3. $mailmessage = "This is a new mail message.";
  4. $mailheader = "From: sender@otherdomain.com\r\n";
  5. $mailheader .= "Reply-To: sender@otherdomain.com\r\n";
  6. 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:

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

To format the eMail as HTML:

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

You can then format the eMail message in HTML:

  1. <?php $mailmessage = '<html><body>';
  2. $mailmessage .= '<img src="http://demosthenes.info/apple-touch-icon.png" alt="Demosthenes.info " />';
  3. $mailmessage .= '<h1>New Message from Demosthenes.info</h1>';
  4. $mailmessage .= '<p>Message body text</p>';
  5. $mailmessage .= '</body></html>'; ?>

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

After doing a couple HTML emails at work, I found that in Outlook, HTML emails can be parsed as plain text rather than HTML. The solution to this is removing "\r" from the "MIME-Version:" and "Content-Type:" lines.
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=ISO-8859-1' . "\n";

posted by owenconti

Dudley StoreyThanks, Owen! I cleaned up the formatting of your code example... I need to work on the comment parsing for the blog (job #35742).

posted by Dudley Storey

web developer guide

featured comment

by JoelB in Goodbye, JQuery Validation: HTML5 Form Errors With CSS3

what i'm reading

A Storm of Swords: A Song of Ice and Fire: Book Three
A Storm of Swords: A Song of Ice and Fire: Book Three

what i'm watching

Californication: The Third Season
Californication: The Third Season

what i'm playing

Mass Effect 3 Collector's Edition
Mass Effect 3 Collector's Edition

what i'm hearing

Dub FX
Dub FX

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.