From 05548f8f05d1f64945a4fdb615f0e4e1620323c2 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 3 Jun 2009 16:29:26 +0000 Subject: Correctly split UTF-8 encoded strings when using mail(). #45815 git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@9530 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/functions_messenger.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'phpBB/includes/functions_messenger.php') diff --git a/phpBB/includes/functions_messenger.php b/phpBB/includes/functions_messenger.php index 4fd5fd19e0..4916ee41d7 100644 --- a/phpBB/includes/functions_messenger.php +++ b/phpBB/includes/functions_messenger.php @@ -408,6 +408,8 @@ class messenger $this->from = '<' . $config['board_contact'] . '>'; } + $encode_eol = ($config['smtp_delivery']) ? "\r\n" : $this->eol; + // Build to, cc and bcc strings $to = $cc = $bcc = ''; foreach ($this->addresses as $type => $address_ary) @@ -419,7 +421,7 @@ class messenger foreach ($address_ary as $which_ary) { - $$type .= (($$type != '') ? ', ' : '') . (($which_ary['name'] != '') ? '"' . mail_encode($which_ary['name']) . '" <' . $which_ary['email'] . '>' : $which_ary['email']); + $$type .= (($$type != '') ? ', ' : '') . (($which_ary['name'] != '') ? '"' . mail_encode($which_ary['name'], $encode_eol) . '" <' . $which_ary['email'] . '>' : $which_ary['email']); } } @@ -443,7 +445,7 @@ class messenger $headers = implode($this->eol, $headers); ob_start(); - $result = $config['email_function_name']($mail_to, mail_encode($this->subject), wordwrap(utf8_wordwrap($this->msg), 997, "\n", true), $headers); + $result = $config['email_function_name']($mail_to, mail_encode($this->subject, $this->eol), wordwrap(utf8_wordwrap($this->msg), 997, "\n", true), $headers); $err_msg = ob_get_clean(); } @@ -688,7 +690,7 @@ class queue else { ob_start(); - $result = $config['email_function_name']($to, mail_encode($subject), wordwrap(utf8_wordwrap($msg), 997, "\n", true), implode($this->eol, $headers)); + $result = $config['email_function_name']($to, mail_encode($subject, $this->eol), wordwrap(utf8_wordwrap($msg), 997, "\n", true), implode($this->eol, $headers)); $err_msg = ob_get_clean(); } @@ -1441,13 +1443,15 @@ class smtp_class * is basically doomed with an unreadable subject. * * Please note that this version fully supports RFC 2045 section 6.8. +* +* @param string $eol End of line we are using (optional to be backwards compatible) */ -function mail_encode($str) +function mail_encode($str, $eol = "\r\n") { // define start delimimter, end delimiter and spacer $start = "=?UTF-8?B?"; $end = "?="; - $delimiter = "\r\n "; + $delimiter = "$eol "; // Maximum length is 75. $split_length *must* be a multiple of 4, but <= 75 - strlen($start . $delimiter . $end)!!! $split_length = 60; -- cgit v1.2.1 From cef8c2d87e2c1e55a7fd6d57e3f998e531fd0bf2 Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Wed, 22 Jul 2009 09:07:24 +0000 Subject: Parse email text files with the template engine. Did not check if this influences styles template cache list/purge/etc. git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@9823 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/functions_messenger.php | 75 ++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 22 deletions(-) (limited to 'phpBB/includes/functions_messenger.php') diff --git a/phpBB/includes/functions_messenger.php b/phpBB/includes/functions_messenger.php index 4916ee41d7..2255b3dcb5 100644 --- a/phpBB/includes/functions_messenger.php +++ b/phpBB/includes/functions_messenger.php @@ -27,6 +27,8 @@ class messenger var $mail_priority = MAIL_NORMAL_PRIORITY; var $use_queue = true; + + var $tpl_obj = NULL; var $tpl_msg = array(); var $eol = "\n"; @@ -177,7 +179,7 @@ class messenger if (!trim($template_file)) { - trigger_error('No template file set', E_USER_ERROR); + trigger_error('No template file for emailing set.', E_USER_ERROR); } if (!trim($template_lang)) @@ -185,25 +187,25 @@ class messenger $template_lang = basename($config['default_lang']); } - if (empty($this->tpl_msg[$template_lang . $template_file])) + // tpl_msg now holds a template object we can use to parse the template file + if (!isset($this->tpl_msg[$template_lang . $template_file])) { - $tpl_file = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/'; - $tpl_file .= $template_lang . "/email/$template_file.txt"; + $this->tpl_msg[$template_lang . $template_file] = new template(); + $tpl = &$this->tpl_msg[$template_lang . $template_file]; - if (!file_exists($tpl_file)) - { - trigger_error("Could not find email template file [ $tpl_file ]", E_USER_ERROR); - } + $template_path = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/'; + $template_path .= $template_lang . '/email'; - if (($data = @file_get_contents($tpl_file)) === false) - { - trigger_error("Failed opening template file [ $tpl_file ]", E_USER_ERROR); - } + $tpl->set_custom_template($template_path, $template_lang . '_email'); - $this->tpl_msg[$template_lang . $template_file] = $data; + $tpl->set_filenames(array( + 'body' => $template_file . '.txt', + )); } - $this->msg = $this->tpl_msg[$template_lang . $template_file]; + $this->tpl_obj = &$this->tpl_msg[$template_lang . $template_file]; + $this->vars = &$this->tpl_obj->_rootref; + $this->tpl_msg = ''; return true; } @@ -213,7 +215,22 @@ class messenger */ function assign_vars($vars) { - $this->vars = (empty($this->vars)) ? $vars : $this->vars + $vars; + if (!is_object($this->tpl_obj)) + { + return; + } + + $this->tpl_obj->assign_vars($vars); + } + + function assign_block_vars($blockname, $vars) + { + if (!is_object($this->tpl_obj)) + { + return; + } + + $this->tpl_obj->assign_block_vars($blockname, $vars); } /** @@ -224,15 +241,29 @@ class messenger global $config, $user; // We add some standard variables we always use, no need to specify them always - $this->vars['U_BOARD'] = (!isset($this->vars['U_BOARD'])) ? generate_board_url() : $this->vars['U_BOARD']; - $this->vars['EMAIL_SIG'] = (!isset($this->vars['EMAIL_SIG'])) ? str_replace('
', "\n", "-- \n" . htmlspecialchars_decode($config['board_email_sig'])) : $this->vars['EMAIL_SIG']; - $this->vars['SITENAME'] = (!isset($this->vars['SITENAME'])) ? htmlspecialchars_decode($config['sitename']) : $this->vars['SITENAME']; + if (!isset($this->vars['U_BOARD'])) + { + $this->assign_vars(array( + 'U_BOARD' => generate_board_url(), + )); + } - // Escape all quotes, else the eval will fail. - $this->msg = str_replace ("'", "\'", $this->msg); - $this->msg = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . ((isset(\$this->vars['\\1'])) ? \$this->vars['\\1'] : '') . '", $this->msg); + if (!isset($this->vars['EMAIL_SIG'])) + { + $this->assign_vars(array( + 'EMAIL_SIG' => str_replace('
', "\n", "-- \n" . htmlspecialchars_decode($config['board_email_sig'])), + )); + } + + if (!isset($this->vars['SITENAME'])) + { + $this->assign_vars(array( + 'SITENAME' => htmlspecialchars_decode($config['sitename']), + )); + } - eval("\$this->msg = '$this->msg';"); + // Parse message through template + $this->msg = trim($this->tpl_obj->assign_display('body')); // We now try and pull a subject from the email body ... if it exists, // do this here because the subject may contain a variable -- cgit v1.2.1 From be963ed93892659005f892767548ee4597e73fcb Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Tue, 11 Aug 2009 16:24:18 +0000 Subject: allow custom template path for email files git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@9957 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/functions_messenger.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'phpBB/includes/functions_messenger.php') diff --git a/phpBB/includes/functions_messenger.php b/phpBB/includes/functions_messenger.php index 2255b3dcb5..212ae720cb 100644 --- a/phpBB/includes/functions_messenger.php +++ b/phpBB/includes/functions_messenger.php @@ -173,7 +173,7 @@ class messenger /** * Set email template to use */ - function template($template_file, $template_lang = '') + function template($template_file, $template_lang = '', $template_path = '') { global $config, $phpbb_root_path, $user; @@ -193,8 +193,11 @@ class messenger $this->tpl_msg[$template_lang . $template_file] = new template(); $tpl = &$this->tpl_msg[$template_lang . $template_file]; - $template_path = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/'; - $template_path .= $template_lang . '/email'; + if (!$template_path) + { + $template_path = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/'; + $template_path .= $template_lang . '/email'; + } $tpl->set_custom_template($template_path, $template_lang . '_email'); -- cgit v1.2.1 From 20bf778b93a4057814503dd759d94b50868f6d42 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Tue, 18 Aug 2009 01:43:51 +0000 Subject: Per RFC2047 section 5 do not use quoted strings for encoded recipient names git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@10011 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/functions_messenger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/functions_messenger.php') diff --git a/phpBB/includes/functions_messenger.php b/phpBB/includes/functions_messenger.php index 212ae720cb..9b9c013a02 100644 --- a/phpBB/includes/functions_messenger.php +++ b/phpBB/includes/functions_messenger.php @@ -455,7 +455,7 @@ class messenger foreach ($address_ary as $which_ary) { - $$type .= (($$type != '') ? ', ' : '') . (($which_ary['name'] != '') ? '"' . mail_encode($which_ary['name'], $encode_eol) . '" <' . $which_ary['email'] . '>' : $which_ary['email']); + $$type .= (($$type != '') ? ', ' : '') . (($which_ary['name'] != '') ? mail_encode($which_ary['name'], $encode_eol) . ' <' . $which_ary['email'] . '>' : $which_ary['email']); } } -- cgit v1.2.1 From 9bcc38ea0b9a9b2a97b3032c9a528fe8fba0719a Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Tue, 18 Aug 2009 09:59:24 +0000 Subject: Fix email problems on servers with PHP installations not accepting RFC-compliant subject string passed to the the mail()-function. (Bug #46725) git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@10014 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/functions_messenger.php | 43 +++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 11 deletions(-) (limited to 'phpBB/includes/functions_messenger.php') diff --git a/phpBB/includes/functions_messenger.php b/phpBB/includes/functions_messenger.php index 9b9c013a02..ecd9709f00 100644 --- a/phpBB/includes/functions_messenger.php +++ b/phpBB/includes/functions_messenger.php @@ -390,7 +390,7 @@ class messenger $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-Mailer: phpBB3'; $headers[] = 'X-MimeOLE: phpBB3'; $headers[] = 'X-phpBB-Origin: phpbb://' . str_replace(array('http://', 'https://'), array('', ''), generate_board_url()); @@ -474,13 +474,7 @@ class messenger } else { - // We use the EOL character for the OS here because the PHP mail function does not correctly transform line endings. On Windows SMTP is used (SMTP is \r\n), on UNIX a command is used... - // Reference: http://bugs.php.net/bug.php?id=15841 - $headers = implode($this->eol, $headers); - - ob_start(); - $result = $config['email_function_name']($mail_to, mail_encode($this->subject, $this->eol), wordwrap(utf8_wordwrap($this->msg), 997, "\n", true), $headers); - $err_msg = ob_get_clean(); + $result = phpbb_mail($mail_to, $this->subject, $this->msg, $headers, $this->eol, $err_msg); } if (!$result) @@ -723,9 +717,7 @@ class queue } else { - ob_start(); - $result = $config['email_function_name']($to, mail_encode($subject, $this->eol), wordwrap(utf8_wordwrap($msg), 997, "\n", true), implode($this->eol, $headers)); - $err_msg = ob_get_clean(); + $result = phpbb_mail($to, $subject, $msg, $headers, $this->eol, $err_msg); } if (!$result) @@ -1522,4 +1514,33 @@ function mail_encode($str, $eol = "\r\n") return substr($str, 0, -strlen($delimiter)); } +/** +* Wrapper for sending out emails with the PHP's mail function +*/ +function phpbb_mail($to, $subject, $msg, $headers, $eol, &$err_msg) +{ + global $config; + + // We use the EOL character for the OS here because the PHP mail function does not correctly transform line endings. On Windows SMTP is used (SMTP is \r\n), on UNIX a command is used... + // Reference: http://bugs.php.net/bug.php?id=15841 + $headers = implode($eol, $headers); + + ob_start(); + $result = $config['email_function_name']($to, mail_encode($subject, $eol), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $headers); + $err_msg = ob_get_clean(); + + // Try again... + // On some PHP Versions mail() *may* fail if there are newlines within the subject. + // Newlines are used as a delimiter for lines in mail_encode() according to RFC 2045 section 6.8. + if (!$result) + { + // Use nothing as delimiter (results in SPACE used) + ob_start(); + $result = $config['email_function_name']($to, mail_encode($subject, ''), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $headers); + $err_msg = ob_get_clean(); + } + + return $result; +} + ?> \ No newline at end of file -- cgit v1.2.1 From 92a17752944b527c3212ed3d477a5804450be93e Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Tue, 18 Aug 2009 13:24:45 +0000 Subject: we will use the non-compliant way for mail() until we further inspected PHP's source. git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@10015 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/functions_messenger.php | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) (limited to 'phpBB/includes/functions_messenger.php') diff --git a/phpBB/includes/functions_messenger.php b/phpBB/includes/functions_messenger.php index ecd9709f00..847b2e0c46 100644 --- a/phpBB/includes/functions_messenger.php +++ b/phpBB/includes/functions_messenger.php @@ -1526,19 +1526,11 @@ function phpbb_mail($to, $subject, $msg, $headers, $eol, &$err_msg) $headers = implode($eol, $headers); ob_start(); - $result = $config['email_function_name']($to, mail_encode($subject, $eol), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $headers); - $err_msg = ob_get_clean(); - - // Try again... // On some PHP Versions mail() *may* fail if there are newlines within the subject. // Newlines are used as a delimiter for lines in mail_encode() according to RFC 2045 section 6.8. - if (!$result) - { - // Use nothing as delimiter (results in SPACE used) - ob_start(); - $result = $config['email_function_name']($to, mail_encode($subject, ''), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $headers); - $err_msg = ob_get_clean(); - } + // Because PHP can't decide what is wanted we revert back to the non-RFC-compliant way of separating by one space (Use '' as parameter to mail_encode() results in SPACE used) + $result = $config['email_function_name']($to, mail_encode($subject, ''), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $headers); + $err_msg = ob_get_clean(); return $result; } -- cgit v1.2.1 From 19bae65f4c79c7f50d71fd57bfff6ff1033ed84b Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Tue, 25 Aug 2009 09:04:38 +0000 Subject: Fix possible wrong encodings in email template files in messenger. Related to bug #46725 git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@10052 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/functions_messenger.php | 3 +++ 1 file changed, 3 insertions(+) (limited to 'phpBB/includes/functions_messenger.php') diff --git a/phpBB/includes/functions_messenger.php b/phpBB/includes/functions_messenger.php index 847b2e0c46..0fcf33f999 100644 --- a/phpBB/includes/functions_messenger.php +++ b/phpBB/includes/functions_messenger.php @@ -268,6 +268,9 @@ class messenger // Parse message through template $this->msg = trim($this->tpl_obj->assign_display('body')); + // Because we use \n for newlines in the body message we need to fix line encoding errors for those admins who uploaded email template files in the wrong encoding + $this->msg = str_replace("\r\n", "\n", $this->msg); + // We now try and pull a subject from the email body ... if it exists, // do this here because the subject may contain a variable $drop_header = ''; -- cgit v1.2.1