PDA

View Full Version : Multipart Mail Messages


michitopf
03-02-2010, 12:56 PM
In PHP one can easily send Multipart MIME messages. I use the code below to accomplish this. In Quercus this no longer works. It seems that the whole message is wrapped in a single large MIME part of a JavaMail MimeMessage.
How can I then send Multipart MIME messages in Quercus?

/*----------
* $parts is an array where each entry represents a MIME part and is in turn another array with following entries:
* $parts[$i]['content-type']
* $parts[$i]['content-transfer-encoding'] ... 8bit (default), base64
* $parts[$i]['filename']
* $parts[$i]['content']
*/
function send_multipart_mail($to, $from, $subject, $parts) {
global $mail_error;

$boundary = '=_' . strtoupper(md5(uniqid('', true)));
$headers = '';
if ($from)
$headers .= 'From: ' . $from . CRLF;
$headers .= 'MIME-Version: 1.0' . CRLF;
$headers .= 'Date: ' . date('r') . CRLF; // 'r' ... RFC 822
$headers .= 'Content-Type: multipart/mixed;' . CRLF;
$headers .= ' boundary="' . $boundary . '"';
$headers .= 'Content-Transfer-Encoding: 8bit' . CRLF;

$text = 'This is a message with multiple parts in MIME format.' . CRLF;

foreach ($parts as $part) {
$text .= '--' . $boundary . CRLF;
$text .= 'Content-Type: ' . $part['content-type'] . CRLF;
$content_transfer_encoding = $part['content-transfer-encoding'];
if (!$content_transfer_encoding)
$content_transfer_encoding = '8bit';
$text .= 'Content-Transfer-Encoding: ' . $content_transfer_encoding . CRLF;
$filename = $part['filename'];
if ($filename)
$text .= 'Content-Disposition: attachment; filename="' . $filename . '"' . CRLF;
$text .= CRLF;
$content = $part['content'];
if ($content_transfer_encoding == 'base64')
$content = trim(chunk_split(base64_encode($content)));
$text .= $content . CRLF;
}
$text .= '--' . $boundary . '--';

set_error_handler(mail_error_handler);
$mail_ok = mail($to, $subject, $text, $headers);
restore_error_handler();
return $mail_ok;
}

The code can be used like this (also serves as a testcase):

$text = array(
'content-type' => 'text/plain; charset="ISO-8859-1"',
'content' => 'Dear recipient,' . CRLF . CRLF . 'attached you will find the requested information' . CRLF . CRLF . 'regards' . CRLF . 'Server' . CRLF . CRLF
);
$attachment = array(
'content-type' => 'text/xml',
'content-transfer-encoding' => 'base64',
'filename' => 'info.xml',
'content' => '<?xml version="1.0" encoding="ISO-8859-1" ?>' . CRLF .
'<info>Test</info>');
send_multipart_mail('to@recipient.com', 'from@sender.org', 'Subject', array($text, $attachment));

emil
03-03-2010, 07:43 PM
Looks like a bug. I've filed it here:

http://bugs.caucho.com/view.php?id=3923

Thanks,
Emil