From 1932c68f8a91b51be44d514b653f587596564478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Gaillard?= Date: Tue, 17 Jan 2012 16:56:51 +0000 Subject: [PATCH] [-] Classes : #PSCFI-4688 : Added a base64_encode() for the mail receiver name. Needed for names with accentuated chars --- classes/Mail.php | 92 +++++++++++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 37 deletions(-) diff --git a/classes/Mail.php b/classes/Mail.php index c840225ec..d6b8eff7e 100644 --- a/classes/Mail.php +++ b/classes/Mail.php @@ -32,26 +32,37 @@ include_once(_PS_SWIFT_DIR_.'Swift/Plugin/Decorator.php'); class MailCore { - public static function Send($id_lang, $template, $subject, $templateVars, $to, $toName = NULL, $from = NULL, $fromName = NULL, $fileAttachment = NULL, $modeSMTP = NULL, $templatePath = _PS_MAIL_DIR_, $die = false) + public static function Send($id_lang, $template, $subject, $templateVars, $to, + $toName = null, $from = null, $fromName = null, $fileAttachment = null, $modeSMTP = null, $templatePath = _PS_MAIL_DIR_, $die = false) { - $configuration = Configuration::getMultiple(array('PS_SHOP_EMAIL', 'PS_MAIL_METHOD', 'PS_MAIL_SERVER', 'PS_MAIL_USER', 'PS_MAIL_PASSWD', 'PS_SHOP_NAME', 'PS_MAIL_SMTP_ENCRYPTION', 'PS_MAIL_SMTP_PORT', 'PS_MAIL_METHOD', 'PS_MAIL_TYPE')); - if(!isset($configuration['PS_MAIL_SMTP_ENCRYPTION'])) $configuration['PS_MAIL_SMTP_ENCRYPTION'] = 'off'; - if(!isset($configuration['PS_MAIL_SMTP_PORT'])) $configuration['PS_MAIL_SMTP_PORT'] = 'default'; + $configuration = Configuration::getMultiple(array('PS_SHOP_EMAIL', + 'PS_MAIL_METHOD', + 'PS_MAIL_SERVER', + 'PS_MAIL_USER', + 'PS_MAIL_PASSWD', + 'PS_SHOP_NAME', + 'PS_MAIL_SMTP_ENCRYPTION', + 'PS_MAIL_SMTP_PORT', + 'PS_MAIL_METHOD', + 'PS_MAIL_TYPE')); + + if (!isset($configuration['PS_MAIL_SMTP_ENCRYPTION'])) $configuration['PS_MAIL_SMTP_ENCRYPTION'] = 'off'; + if (!isset($configuration['PS_MAIL_SMTP_PORT'])) $configuration['PS_MAIL_SMTP_PORT'] = 'default'; if (!isset($from)) $from = $configuration['PS_SHOP_EMAIL']; if (!isset($fromName)) $fromName = $configuration['PS_SHOP_NAME']; - if (!empty($from) AND !Validate::isEmail($from)) + if (!empty($from) && !Validate::isEmail($from)) { Tools::dieOrLog(Tools::displayError('Error: parameter "from" is corrupted'), $die); return false; } - if (!empty($fromName) AND !Validate::isMailName($fromName)) + if (!empty($fromName) && !Validate::isMailName($fromName)) { Tools::dieOrLog(Tools::displayError('Error: parameter "fromName" is corrupted'), $die); return false; } - if (!is_array($to) AND !Validate::isEmail($to)) + if (!is_array($to) && !Validate::isEmail($to)) { Tools::dieOrLog(Tools::displayError('Error: parameter "to" is corrupted'), $die); return false; @@ -64,10 +75,10 @@ class MailCore } // Do not crash for this error, that may be a complicated customer name - if(is_string($toName)) + if (is_string($toName)) { - if (!empty($toName) AND !Validate::isMailName($toName)) - $toName = NULL; + if (!empty($toName) && !Validate::isMailName($toName)) + $toName = null; } if (!Validate::isTplName($template)) @@ -83,42 +94,44 @@ class MailCore } /* Construct multiple recipients list if needed */ - if (is_array($to) and isset($to)) + if (is_array($to) && isset($to)) { $to_list = new Swift_RecipientList(); - foreach ($to AS $key => $addr) + foreach ($to as $key => $addr) { - $to_name = NULL; + $to_name = null; $addr = trim($addr); if (!Validate::isEmail($addr)) { Tools::dieOrLog(Tools::displayError('Error: invalid email address'), $die); return false; } - if(is_array($toName)) + if (is_array($toName)) { - if ($toName AND is_array($toName) AND Validate::isGenericName($toName[$key])) + if ($toName && is_array($toName) && Validate::isGenericName($toName[$key])) $to_name = $toName[$key]; } - $to_list->addTo($addr, $to_name); + $to_list->addTo($addr, base64_encode($to_name)); } $to_plugin = $to[0]; $to = $to_list; } else { /* Simple recipient, one address */ $to_plugin = $to; - $to = new Swift_Address($to, $toName); + $to = new Swift_Address($to, base64_encode($toName)); } try { /* Connect with the appropriate configuration */ if ($configuration['PS_MAIL_METHOD'] == 2) { - if (empty($configuration['PS_MAIL_SERVER']) OR empty($configuration['PS_MAIL_SMTP_PORT'])) + if (empty($configuration['PS_MAIL_SERVER']) || empty($configuration['PS_MAIL_SMTP_PORT'])) { Tools::dieOrLog(Tools::displayError('Error: invalid SMTP server or SMTP port'), $die); return false; } - $connection = new Swift_Connection_SMTP($configuration['PS_MAIL_SERVER'], $configuration['PS_MAIL_SMTP_PORT'], ($configuration['PS_MAIL_SMTP_ENCRYPTION'] == "ssl") ? Swift_Connection_SMTP::ENC_SSL : (($configuration['PS_MAIL_SMTP_ENCRYPTION'] == "tls") ? Swift_Connection_SMTP::ENC_TLS : Swift_Connection_SMTP::ENC_OFF)); + $connection = new Swift_Connection_SMTP($configuration['PS_MAIL_SERVER'], $configuration['PS_MAIL_SMTP_PORT'], + ($configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'ssl') ? Swift_Connection_SMTP::ENC_SSL : + (($configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'tls') ? Swift_Connection_SMTP::ENC_TLS : Swift_Connection_SMTP::ENC_OFF)); $connection->setTimeout(4); if (!$connection) return false; @@ -146,43 +159,44 @@ class MailCore $overrideMail = false; // get templatePath - if (preg_match('#'.__PS_BASE_URI__.'modules/#', $templatePath) AND preg_match('#modules/([a-z0-9_-]+)/#ui' , $templatePath , $res)) + if (preg_match('#'.__PS_BASE_URI__.'modules/#', $templatePath) && preg_match('#modules/([a-z0-9_-]+)/#ui', $templatePath, $res)) $moduleName = $res[1]; - if ($moduleName !== false AND (file_exists(_PS_THEME_DIR_.'modules/'.$moduleName.'/mails/'.$template.'.txt') OR + if ($moduleName !== false && (file_exists(_PS_THEME_DIR_.'modules/'.$moduleName.'/mails/'.$template.'.txt') || file_exists(_PS_THEME_DIR_.'modules/'.$moduleName.'/mails/'.$template.'.html'))) $templatePath = _PS_THEME_DIR_.'modules/'.$moduleName.'/mails/'; - elseif (file_exists(_PS_THEME_DIR_.'mails/'.$template.'.txt') OR file_exists(_PS_THEME_DIR_.'mails/'.$template.'.html')) + else if (file_exists(_PS_THEME_DIR_.'mails/'.$template.'.txt') || file_exists(_PS_THEME_DIR_.'mails/'.$template.'.html')) { $templatePath = _PS_THEME_DIR_.'mails/'; $overrideMail = true; } - elseif (!file_exists($templatePath.$template.'.txt') OR !file_exists($templatePath.$template.'.html')) + else if (!file_exists($templatePath.$template.'.txt') || !file_exists($templatePath.$template.'.html')) { Tools::dieOrLog(Tools::displayError('Error - The following email template is missing:').' '.$templatePath.$template.'.txt', $die); return false; } $templateHtml = file_get_contents($templatePath.$template.'.html'); - $templateTxt = strip_tags(html_entity_decode(file_get_contents($templatePath.$template.'.txt'), NULL, 'utf-8')); + $templateTxt = strip_tags(html_entity_decode(file_get_contents($templatePath.$template.'.txt'), null, 'utf-8')); - if ($overrideMail AND file_exists($templatePath.$iso.'/lang.php')) + if ($overrideMail && file_exists($templatePath.$iso.'/lang.php')) include_once($templatePath.$iso.'/lang.php'); - elseif ($moduleName AND file_exists($templatePath.$iso.'/lang.php')) + else if ($moduleName && file_exists($templatePath.$iso.'/lang.php')) include_once(_PS_THEME_DIR_.'mails/'.$iso.'/lang.php'); else include_once(dirname(__FILE__).'/../mails/'.$iso.'/lang.php'); /* Create mail and attach differents parts */ - $message = new Swift_Message('['.Configuration::get('PS_SHOP_NAME').'] '. $subject); - $templateVars['{shop_logo}'] = (file_exists(_PS_IMG_DIR_.'logo_mail.jpg')) ? $message->attach(new Swift_Message_Image(new Swift_File(_PS_IMG_DIR_.'logo_mail.jpg'))) : ((file_exists(_PS_IMG_DIR_.'logo.jpg')) ? $message->attach(new Swift_Message_Image(new Swift_File(_PS_IMG_DIR_.'logo.jpg'))) : ''); + $message = new Swift_Message('['.Configuration::get('PS_SHOP_NAME').'] '.$subject); + $templateVars['{shop_logo}'] = (file_exists(_PS_IMG_DIR_.'logo_mail.jpg')) ? $message->attach(new Swift_Message_Image(new Swift_File(_PS_IMG_DIR_.'logo_mail.jpg'))) : + ((file_exists(_PS_IMG_DIR_.'logo.jpg')) ? $message->attach(new Swift_Message_Image(new Swift_File(_PS_IMG_DIR_.'logo.jpg'))) : ''); $templateVars['{shop_name}'] = Tools::safeOutput(Configuration::get('PS_SHOP_NAME')); $templateVars['{shop_url}'] = Tools::getShopDomain(true, true).__PS_BASE_URI__; $swift->attachPlugin(new Swift_Plugin_Decorator(array($to_plugin => $templateVars)), 'decorator'); - if ($configuration['PS_MAIL_TYPE'] == 3 OR $configuration['PS_MAIL_TYPE'] == 2) + if ($configuration['PS_MAIL_TYPE'] == 3 || $configuration['PS_MAIL_TYPE'] == 2) $message->attach(new Swift_Message_Part($templateTxt, 'text/plain', '8bit', 'utf-8')); - if ($configuration['PS_MAIL_TYPE'] == 3 OR $configuration['PS_MAIL_TYPE'] == 1) + if ($configuration['PS_MAIL_TYPE'] == 3 || $configuration['PS_MAIL_TYPE'] == 1) $message->attach(new Swift_Message_Part($templateHtml, 'text/html', '8bit', 'utf-8')); - if ($fileAttachment AND isset($fileAttachment['content']) AND isset($fileAttachment['name']) AND isset($fileAttachment['mime'])) + if ($fileAttachment && isset($fileAttachment['content']) && isset($fileAttachment['name']) && isset($fileAttachment['mime'])) $message->attach(new Swift_Message_Attachment($fileAttachment['content'], $fileAttachment['name'], $fileAttachment['mime'])); /* Send mail */ $send = $swift->send($message, $to, new Swift_Address($from, $fromName)); @@ -190,18 +204,22 @@ class MailCore return $send; } - catch (Swift_ConnectionException $e) { return false; } + catch (Swift_ConnectionException $e) + { + return false; + } } public static function sendMailTest($smtpChecked, $smtpServer, $content, $subject, $type, $to, $from, $smtpLogin, $smtpPassword, $smtpPort = 25, $smtpEncryption) { - $swift = NULL; + $swift = null; $result = false; try { - if($smtpChecked) + if ($smtpChecked) { - $smtp = new Swift_Connection_SMTP($smtpServer, $smtpPort, ($smtpEncryption == "off") ? Swift_Connection_SMTP::ENC_OFF : (($smtpEncryption == "tls") ? Swift_Connection_SMTP::ENC_TLS : Swift_Connection_SMTP::ENC_SSL)); + $smtp = new Swift_Connection_SMTP($smtpServer, $smtpPort, ($smtpEncryption == 'off') ? + Swift_Connection_SMTP::ENC_OFF : (($smtpEncryption == 'tls') ? Swift_Connection_SMTP::ENC_TLS : Swift_Connection_SMTP::ENC_SSL)); $smtp->setUsername($smtpLogin); $smtp->setpassword($smtpPassword); $smtp->setTimeout(5); @@ -236,7 +254,7 @@ class MailCore * * @param string $string raw sentence (write directly in file) */ - static public function l($string, $id_lang = null, Context $context = null) + public static function l($string, $id_lang = null, Context $context = null) { global $_LANGMAIL; if (!$context) @@ -244,7 +262,7 @@ class MailCore $key = str_replace('\'', '\\\'', $string); if ($id_lang == null) - $id_lang = (!isset($context->language) OR !is_object($context->language)) ? (int)Configuration::get('PS_LANG_DEFAULT') : (int)$context->language->id; + $id_lang = (!isset($context->language) || !is_object($context->language)) ? (int)Configuration::get('PS_LANG_DEFAULT') : (int)$context->language->id; $file_core = _PS_ROOT_DIR_.'/mails/'.$context->language->iso_code.'/lang.php'; if (Tools::file_exists_cache($file_core) && empty($_LANGMAIL))