kmcm

Web Development with PHP, HTML, CSS, & JavaScript

Html and plain text multipart email

Thursday 1st July 2010

When sending an email using PHP, the most basic way is to use the mail() function, with plain text for the message body. Another way is to specify headers and use html content for the message. There are problems with both these approaches - using plain text can be very limited, with nothing but plain text your email can look very boring, while some popular email clients and interfaces can have difficulty handling html emails, leaving your recipient with an email full of code.

A way to solve both of these problems in one is to create a multipart email, which basically sends 2 emails in one. You can create a fancy html email, and when a user's email client can't handle this, it will default to plain text, in a gracefully degrading fashion. There are a number of steps involved in this:

First off, create 2 variables: your recipients email address and the subject of the email.


//specify the email address you are sending to, and the email subject
$email = 'email@example.com';
$subject = 'Email Subject';

Next, use the PHP unique() function to create a unique string, this will serve as the boundary for the different parts of the email. This tells the email to differentiate between the plain text and html versions of the email.

 
//create a boundary for the email. This
$boundary = uniqid('np');

The above code will result in a string along the lines of --np2c6cf41f304e3

Next up you specify the headers for the email, which includes the content types and the to & from email addresses.

 
//headers - specify your from email address and name here
//and specify the boundary for the email
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: Your Name \r\n";
$headers .= "To: ".$email."\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";

Then we create the message body. Here we start the message, and separate the different parts of the message with the boundary.

//Plain text body
$message .= "Hello,\nThis is a text email, the text/plain version.
\n\nRegards,\nYour Name";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";

//Html body
$message .= "
 Hello,
This is a text email, the html version.

Regards,
Your Name"; $message .= "\r\n\r\n--" . $boundary . "--";

Finally, we invoke the mail() function to send the email. Having already specified the to email address in the headers, we can leave this parameter blank, then include the subject, message border and headers.


//invoke the PHP mail function
mail('', $subject, $message, $headers);

Here is the code all together:

//specify the email address you are sending to, and the email subject
$email = 'email@example.com';
$subject = 'Email Subject';
				
//create a boundary for the email. This 
$boundary = uniqid('np');
				
//headers - specify your from email address and name here
//and specify the boundary for the email
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: Your Name \r\n";
$headers .= "To: ".$email."\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";

//here is the content body
$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/plain;charset=utf-8\r\n\r\n";

//Plain text body
$message .= "Hello,\nThis is a text email, the text/plain version.
\n\nRegards,\nYour Name";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";

//Html body
$message .= "
 Hello,
This is a text email, the html version.

Regards,
Your Name"; $message .= "\r\n\r\n--" . $boundary . "--"; //invoke the PHP mail function mail('', $subject, $message, $headers);


Filed under: