PHP Mail Headers

Most MTA/MUA’s insert a lot of extra headers; however, here is sort of the bare minimum you can expect.

From: 
Reply-To:
To: 
Subject:
Date: date("r");
MIME-Version:
Content-Type:

If you using HTML, then you should probably be using multipart messages–but it’s not strictly necessary.

Here is a snippet of code that shows how multi-part mime headers can be assembled without the help of a mailer class.

<?
$mime_boundary=md5(time());
$headers .= 'From: My Email <me@company.com>' . "\n";
$headers .= 'MIME-Version: 1.0'. "\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"". "\n"; 
 
$msg .= "--".$mime_boundary. "\n";
$msg .= "Content-Type: text/plain; charset=iso-8859-1". "\n";
$msg .= "Content-Transfer-Encoding: 7bit". "\n\n";
$msg .= $textmessage . "\n\n";
 
$msg .= "--".$mime_boundary. "\n";
$msg .= "Content-Type: application/pdf; name=\"".$filename."\"". "\n";
$msg .= "Content-Transfer-Encoding: base64". "\n";
$msg .= "Content-Disposition: attachment; filename=\"".$filename."\"". "\n\n";
$msg .= chunk_split(base64_encode($doc)) . "\n\n";
$msg .= "--".$mime_boundary."--". "\n\n";
?>