Sending PHP Email From Apache
Apache and IIS both interpret the mail() function differently. Here is how to send text email, HTML email, text email with an attachment, and HTML email with an attachment ... from a server running Apache.
This code must be copied EXACTLY as it is, including all \ " ' characters. Remember that a list of email addresses must be comma-separated, as semi-colons will result in errors.
$headers = "From: WebServer\n";
$body = "This is the text section";
mail("test@test.com", "Text Email", $body, $headers);
$random_hash = md5(date("jS M Y", time()));
$boundary = "boundary-alt-".$random_hash;
$attboundary = "boundary-mixed-".$random_hash;
$headers="From: WebServer\n"
."MIME-Version: 1.0\n"
."Content-Type: multipart/mixed; boundary=\"".$attboundary."\"\n\n";
$body =
"--".$attboundary."\n"
."Content-Type: multipart/alternative; boundary=\"".$boundary."\"\n\n"
."--".$boundary."\n"
."Content-Type: text/plain; charset=\"iso-8859-1\"\n"
."Content-Transfer-Encoding: 7bit\n\n"
."This is the text section\n\n"
."--".$boundary."\n"
."Content-Type: text/html; charset=\"iso-8859-1\"\n"
."Content-Transfer-Encoding: 7bit\n\n"
."This is the HTML section\n\n"
."--".$boundary."--\n"
."--".$attboundary."--\n";
mail("test@test.com", "HTML Email", $body, $headers); $random_hash = md5(date("jS M Y", time()));
$boundary = "boundary-alt-".$random_hash;
$attboundary = "boundary-mixed-".$random_hash;
$headers="From: WebServer\n"
."MIME-Version: 1.0\n"
."Content-Type: multipart/mixed; boundary=\"".$attboundary."\"\n\n";
$handle=fopen("filename.pdf", "r");
$content=fread($handle, filesize("filename.pdf"));
fclose($handle);
$attachment = chunk_split(base64_encode($content));
$body =
"--".$attboundary."\n"
."Content-Type: multipart/alternative; boundary=\"".$boundary."\"\n\n"
."--".$boundary."\n"
."Content-Type: text/plain; charset=\"iso-8859-1\"\n"
."Content-Transfer-Encoding: 7bit\n\n"
."This is the text section\n\n"
."--".$boundary."--\n"
."--".$attboundary."\n"
."Content-Type: application/octet-stream; name=\"my_attachment.pdf\"\n"
."Content-Transfer-Encoding: base64\n"
."Content-Disposition: attachment; filename=\"my_attachment.pdf\"\n\n"
.$attachment."\n"
."--".$attboundary."--\n";
mail("test@test.com", "Text Email with Attachment", $body, $headers); $random_hash = md5(date("jS M Y", time()));
$boundary = "boundary-alt-".$random_hash;
$attboundary = "boundary-mixed-".$random_hash;
$headers="From: WebServer\n"
."MIME-Version: 1.0\n"
."Content-Type: multipart/mixed; boundary=\"".$attboundary."\"\n\n";
$handle=fopen("filename.pdf", "r");
$content=fread($handle, filesize("filename.pdf"));
fclose($handle);
$attachment = chunk_split(base64_encode($content));
$body = "--".$attboundary."\n"
."Content-Type: multipart/alternative; boundary=\"".$boundary."\"\n\n"
."--".$boundary."\n"
."Content-Type: text/plain; charset=\"iso-8859-1\"\n"
."Content-Transfer-Encoding: 7bit\n\n"
."This is the text section\n\n"
."--".$boundary."\n"
."Content-Type: text/html; charset=\"iso-8859-1\"\n"
."Content-Transfer-Encoding: 7bit\n\n"
."This is the HTML section\n\n"
."--".$boundary."--\n"
."--".$attboundary."\n"
."Content-Type: application/octet-stream; name=\"my_attachment.pdf\"\n"
."Content-Transfer-Encoding: base64\n"
."Content-Disposition: attachment; filename=\"my_attachment.pdf\"\n\n"
.$attachment."\n"
."--".$attboundary."--\n";
mail("test@test.com", "HTML Email with Attachment", $body, $headers);
>> Posted in Websites at 16:02, and viewed 10,108 times
Next article: Marathon Training Programme (30 September 2011)
CommentsHakan Says:
23rd June 2011 at 02:16
helped me alot, thanks a bunch! H.
Add your comments