The mail function doesn't us a template; however, you can customize it so that it is easy to create an HTML MIME email 😉
There are basically a few parts to this.
-
Sending the correct content type in the header
-
Having a unique boundary for the message
-
Including the HTML and text versions of the email
Basically what you'd do is:
Generate a unique boundary key
This can be done by [man]md5/man-ing the current time, or using a random number/letter generator.
$boundary = '--My-Boundary--'.md5(time());
Grab the HTML "template" that you want to use and parse it
However you want to do this; however, just make sure you store in a variable.
$html = file_get_contents('/path/to/template.html');
// -- OR --
ob_start();
include_once('/path/to/template.html');
$html = ob_get_contents();
ob_end_clean();
Construct the text version from the HTML above, or use a template much in the same way as above
Construct the message body, separating the parts by the boundary
The boundary marks the parts of the message (what's html and what's text or an attachment).
$message = '--'.$boundary.'
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 8bit
'.$html.'
--'.$boundary.'
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
'.$text.'
--'.$boundary.'--';
Send the proper headers along with the message
The Content Type would then be "multipart/alternative" and you'd specify the boundary as well:
$headers = 'From: me@mydomain.com'."\n";
$headers .= 'Content-Type: multipart/alternative; boundary="'.$boundary.'"'."\n";
$headers .= 'MIME-Version: 1.0';
Now, put that all together with the mail function to send an html and plain-text email:
mail($to, $subject, $message, $headers);
And there you have it, sending an HTML mail with a plain-text version. Te reason you would want to send a plain-text version is because not all email clients will read HTML. And some people don't like HTML. So it's a good idea to send both versions that way you can be assured that everyone will see your email (in some form).