let utf8 mails work again. I hope this is not giving problems for "strange" mail setups.

git-svn-id: file:///svn/phpbb/trunk@6564 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
Meik Sievertsen 2006-11-11 12:24:10 +00:00
parent a098417514
commit 8cf0bc433e
2 changed files with 76 additions and 42 deletions

View file

@ -44,8 +44,8 @@ class messenger
*/ */
function reset() function reset()
{ {
$this->addresses = array(); $this->addresses = $this->extra_headers = array();
$this->vars = $this->msg = $this->extra_headers = $this->replyto = $this->from = ''; $this->vars = $this->msg = $this->replyto = $this->from = '';
$this->mail_priority = MAIL_NORMAL_PRIORITY; $this->mail_priority = MAIL_NORMAL_PRIORITY;
} }
@ -118,7 +118,7 @@ class messenger
*/ */
function headers($headers) function headers($headers)
{ {
$this->extra_headers .= trim($headers) . "\n"; $this->extra_headers[] = trim($headers);
} }
/** /**
@ -273,6 +273,50 @@ class messenger
} }
} }
/**
* Return email header
*/
function build_header($to, $cc, $bcc)
{
global $config;
$headers = array();
$headers[] = 'From: ' . $this->from;
if ($cc)
{
$headers[] = 'Cc: ' . $cc;
}
if ($bcc)
{
$headers[] = 'Bcc: ' . $bcc;
}
$headers[] = 'Reply-To: ' . $this->replyto;
$headers[] = 'Return-Path: <' . $config['board_email'] . '>';
$headers[] = 'Sender: <' . $config['board_email'] . '>';
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Message-ID: <' . md5(unique_id(time())) . '@' . $config['server_name'] . '>';
$headers[] = 'Date: ' . gmdate('D, d M Y H:i:s T', time());
$headers[] = 'Content-Type: text/plain; charset=UTF-8'; // format=flowed
$headers[] = 'Content-Transfer-Encoding: 8bit'; // 7bit
$headers[] = 'X-Priority: ' . $this->mail_priority;
$headers[] = 'X-MSMail-Priority: ' . (($this->mail_priority == MAIL_LOW_PRIORITY) ? 'Low' : (($this->mail_priority == MAIL_NORMAL_PRIORITY) ? 'Normal' : 'High'));
$headers[] = 'X-Mailer: PhpBB3';
$headers[] = 'X-MimeOLE: phpBB3';
$headers[] = 'X-phpBB-Origin: phpbb://' . str_replace(array('http://', 'https://'), array('', ''), generate_board_url());
if (sizeof($this->extra_headers))
{
$headers[] = implode("\r\n", $this->extra_headers);
}
return implode("\r\n", $headers);
}
/** /**
* Send out emails * Send out emails
*/ */
@ -296,8 +340,18 @@ class messenger
$use_queue = true; $use_queue = true;
} }
$to = $cc = $bcc = ''; if (empty($this->replyto))
{
$this->replyto = '<' . $config['board_email'] . '>';
}
if (empty($this->from))
{
$this->from = '<' . $config['board_email'] . '>';
}
// Build to, cc and bcc strings // Build to, cc and bcc strings
$to = $cc = $bcc = '';
foreach ($this->addresses as $type => $address_ary) foreach ($this->addresses as $type => $address_ary)
{ {
if ($type == 'im') if ($type == 'im')
@ -311,34 +365,8 @@ class messenger
} }
} }
if (empty($this->replyto))
{
$this->replyto = '<' . $config['board_email'] . '>';
}
if (empty($this->from))
{
$this->from = '<' . $config['board_email'] . '>';
}
// Build header // Build header
$headers = 'From: ' . $this->from . "\n"; $headers = $this->build_header($to, $cc, $bcc);
$headers .= ($cc != '') ? "Cc: $cc\n" : '';
$headers .= ($bcc != '') ? "Bcc: $bcc\n" : '';
$headers .= 'Reply-to: ' . $this->replyto . "\n";
$headers .= 'Return-Path: <' . $config['board_email'] . ">\n";
$headers .= 'Sender: <' . $config['board_email'] . ">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= 'Message-ID: <' . md5(unique_id(time())) . "@" . $config['server_name'] . ">\n";
$headers .= 'Date: ' . gmdate('D, d M Y H:i:s T', time()) . "\n";
$headers .= "Content-type: text/plain; charset=UTF-8\n";
$headers .= "Content-transfer-encoding: 8bit\n";
$headers .= "X-Priority: {$this->mail_priority}\n";
$headers .= 'X-MSMail-Priority: ' . (($this->mail_priority == MAIL_LOW_PRIORITY) ? 'Low' : (($this->mail_priority == MAIL_NORMAL_PRIORITY) ? 'Normal' : 'High')) . "\n";
$headers .= "X-Mailer: PhpBB3\n";
$headers .= "X-MimeOLE: phpBB3\n";
$headers .= "X-phpBB-Origin: phpbb://" . str_replace(array('http://', 'https://'), array('', ''), generate_board_url()) . "\n";
$headers .= ($this->extra_headers != '') ? $this->extra_headers : '';
// Send message ... // Send message ...
if (!$use_queue) if (!$use_queue)
@ -348,11 +376,11 @@ class messenger
if ($config['smtp_delivery']) if ($config['smtp_delivery'])
{ {
$result = smtpmail($this->addresses, $this->subject, wordwrap($this->msg), $err_msg, $headers); $result = smtpmail($this->addresses, mail_encode($this->subject), wordwrap($this->msg), $err_msg, $headers);
} }
else else
{ {
$result = @$config['email_function_name']($mail_to, $this->subject, implode("\n", preg_split("/\r?\n/", wordwrap($this->msg))), $headers); $result = @$config['email_function_name']($mail_to, mail_encode($this->subject), implode("\n", preg_split("/\r?\n/", wordwrap($this->msg))), $headers);
} }
if (!$result) if (!$result)
@ -576,7 +604,7 @@ class queue
$err_msg = ''; $err_msg = '';
$to = (!$to) ? 'Undisclosed-Recipient:;' : $to; $to = (!$to) ? 'Undisclosed-Recipient:;' : $to;
$result = ($config['smtp_delivery']) ? smtpmail($addresses, $subject, wordwrap($msg), $err_msg, $headers) : @$config['email_function_name']($to, $subject, implode("\n", preg_split("/\r?\n/", wordwrap($msg))), $headers); $result = ($config['smtp_delivery']) ? smtpmail($addresses, mail_encode($subject), wordwrap($msg), $err_msg, $headers) : @$config['email_function_name']($to, mail_encode($subject), implode("\n", preg_split("/\r?\n/", wordwrap($msg))), $headers);
if (!$result) if (!$result)
{ {
@ -724,7 +752,7 @@ function smtpmail($addresses, $subject, $message, &$err_msg, $headers = '')
{ {
if (is_array($headers)) if (is_array($headers))
{ {
$headers = (sizeof($headers) > 1) ? join("\n", $headers) : $headers[0]; $headers = (sizeof($headers) > 1) ? join("\r\n", $headers) : $headers[0];
} }
$headers = chop($headers); $headers = chop($headers);
@ -1341,16 +1369,22 @@ class smtp_class
* from php.net and modified. There is an alternative encoding method which * from php.net and modified. There is an alternative encoding method which
* may produce less output but it's questionable as to its worth in this * may produce less output but it's questionable as to its worth in this
* scenario. * scenario.
*
* This version is using base64 encoded date. The downside of this
* is if the mail client does not understand this encoding the user
* is basically doomed with an unreadable subject.
*/ */
function mail_encode($str) function mail_encode($str)
{ {
// define start delimimter, end delimiter and spacer // define start delimimter, end delimiter and spacer
$end = "?="; $end = '?=';
$start = "=?UTF-8?B?"; $start = '=?UTF-8?B?';
$spacer = "$end\r\n $start";
// It may sound strange, but within my tests using \n instead of \r\n seem to work better
$spacer = "$end\n $start";
// determine length of encoded text within chunks and ensure length is even // determine length of encoded text within chunks and ensure length is even
$length = 75 - strlen($start) - strlen($end); $length = 76 - strlen($start) - strlen($end);
$length = floor($length / 2) * 2; $length = floor($length / 2) * 2;
// encode the string and split it into chunks with spacers after each chunk // encode the string and split it into chunks with spacers after each chunk

View file

@ -639,11 +639,11 @@ switch ($mode)
$error = array(); $error = array();
$name = request_var('name', ''); $name = request_var('name', '', true);
$email = request_var('email', ''); $email = request_var('email', '');
$email_lang = request_var('lang', $config['default_lang']); $email_lang = request_var('lang', $config['default_lang']);
$subject = request_var('subject', ''); $subject = request_var('subject', '', true);
$message = request_var('message', ''); $message = request_var('message', '', true);
$cc = (isset($_POST['cc_email'])) ? true : false; $cc = (isset($_POST['cc_email'])) ? true : false;
$submit = (isset($_POST['submit'])) ? true : false; $submit = (isset($_POST['submit'])) ? true : false;