CancelImage Upload

How to send both a plain text and html email in PHP

If you are looking to send both a HTML and Plain Text version of an email in PHP, you will require a more advanced format of the PHP mail() function. To make this work first of all a HTML and Plain Text alternative have to be written.

However, the more interesting part of the advanced email code displayed below is that the content of both versions is enabled through headers. This means that the emails function is one of the first things that have to be called upon once the code is loaded. Headers need to be sent out at the beginning of a loaded page. That being said let's have a closer look at the code itself:


$receipient = '';

//Plain Text content version:

$messageText = "Your text here";

//HTML Text content version:

$messageHTML = "Your text here";

$headers = 'MIME-Version: 1.0' . "\r\n";

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$boundary = uniqid('np');

$headers = "MIME-Version: 1.0\r\n";

$headers .= "From: Your Name " . "\r\n";

//Define your subject:

$headers .= "Subject: Your Subject\r\n";

$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";

$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 included here:

$message .= $messageText;

$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";

//HTML content included here:

$message .= $messageHTML;

$message .= "\r\n\r\n--" . $boundary . "--";

//PHP mail function into which the headers are included:

mail($receipient, $subject, $message, $headers, '-f'.'');


Want to leave a comment?

No problem. Just enter your email and password below.

register | home | reminder

myDesignTool Networking •