note 57009 modified in ref.mail by danbrown

From: Date: Sun, 17 May 2009 20:18:52 +0000
Subject: note 57009 modified in ref.mail by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155368@lists.php.net to get a copy of this message
Problems with Microsoft Exchange and PHP as ISAPI-module We found out, that if you want to send multipart mime emails using the PHP mail-function on a Windows box using a Microsoft Exchange server, you have to use separate containers for the mail body and the mail header. In many examples like in http://www.zend.com/zend/trick/html-email.php or in the book PHP developers cookbook you find html multipart/alternative mailing solutions that build the mime header and the mail body into one PHP variable and send this as fourth argument (header) to the PHP mail-function. This works fine on most systems but not on the above mentioned combination. We found a rather trivial solution: Simply split the mime mail header and the mail body into two separate variables and give them separately to the PHP mail function, example: <?php //add From: header $headers = "From: webserver@localhost\r\n"; //specify MIME version 1.0 $headers .= "MIME-Version: 1.0\r\n"; //unique boundary $boundary = uniqid("HTMLDEMO"); //tell e-mail client this e-mail contains//alternate versions $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; //plain text version of message $body = "--$boundary\r\n" . "Content-Type: text/plain; charset=ISO-8859-1\r\n" . "Content-Transfer-Encoding: base64\r\n\r\n"; $body .= chunk_split(base64_encode("This is the plain text version!")); //HTML version of message $body .= "--$boundary\r\n" . "Content-Type: text/html; charset=ISO-8859-1\r\n" . "Content-Transfer-Encoding: base64\r\n\r\n"; $body .= chunk_split(base64_encode("This the <b>HTML</b> version!")); //send message mail("root@localhost", "An HTML Message", $body, $headers); ?> --was-- Problems with Microsoft Exchange and PHP as ISAPI-module We found out, that if you want to send multipart mime emails using the PHP mail-function on a Windows box using a Microsoft Exchange server, you have to use separate containers for the mail body and the mail header. In many examples like in http://www.zend.com/zend/trick/html-email.php or in the book PHP developers cookbook you find html multipart/alternative mailing solutions that build the mime header and the mail body into one PHP variable and send this as fourth argument (header) to the PHP mail-function. This works fine on most systems but not on the above mentioned combination. We found a rather trivial solution: Simply split the mime mail header and the mail body into two separate variables and give them separately to the PHP mail function, example: <?php //add From: header $headers = "From: webserver@localhost\r\n"; //specify MIME version 1.0 $headers .= "MIME-Version: 1.0\r\n"; //unique boundary $boundary = uniqid("HTMLDEMO"); //tell e-mail client this e-mail contains//alternate versions $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; //plain text version of message $body = "--$boundary\r\n" . "Content-Type: text/plain; charset=ISO-8859-1\r\n" . "Content-Transfer-Encoding: base64\r\n\r\n"; $body .= chunk_split(base64_encode("This is the plain text version!")); //HTML version of message $body .= "--$boundary\r\n" . "Content-Type: text/html; charset=ISO-8859-1\r\n" . "Content-Transfer-Encoding: base64\r\n\r\n"; $body .= chunk_split(base64_encode("This the <b>HTML</b> version!")); //send message mail("root@localhost", "An HTML Message", $body, $headers); ?> http://php.net/manual/en/ref.mail.php

« previous php.notes (#155368) next »