PHP HTML Email Sender

Tired of the same old boring text newsletter? Well send your next out in HTML using this PHP script.

The Code

$theboundary = md5(uniqid(""));

$header = "From: \"$fromname\" <$fromemail>";
$header .= "\nMIME-Version: 1.0";
$header .= "\nContent-Type: multipart/alternative;";
$header .= "\n        boundary=\"----=_NextPart_$theboundary\"";
$header .= "\nX-Priority: 3";
$header .= "\nX-MSMail-Priority: Normal";

$htmlmessage = file_get_contents("htmlemail.html");
$textmessage = file_get_contents("htmlemail.txt");

$body = "This is a multi-part message in MIME format.\n\n";
$body = "------=_NextPart_$theboundary\nContent-Type: text/plain;\n\n";
$body .= $textmessage;
$body .= "\n------=_NextPart_$theboundary\nContent-Type: text/html;\n\n";
$body .= $htmlmessage;
$body .= "\n\n";

mail($to, $subject, $body, $header);

The Explaination
Line 2: Creates a string to be used as a boundary between different sections of the email.

Line 4: “From” Header
Line 5: MIME Version Header
Line 6: Header declaring multiple parts
Line 7: Header declaring part boundary
Line 8-9: Sets the email priority

Line 12: Gets the HTML version of email
Line 13: Gets the TEXT version of email

Line 15: Tells viewer its in multiple parts
Line 16: Begins TEXT part
Line 18: Begins HTML part

Sending HTML emails is very easy, once you know the format of sending one. Just remember to not overdo the HTML page, as it will increase bandwidth if you do a mass mail, and some people dont like big emails ;-)

One thought to “PHP HTML Email Sender”

Comments are closed.