[-] Classes : #PSCFI-4688 : Added a base64_encode() for the mail receiver name. Needed for names with accentuated chars

This commit is contained in:
François Gaillard
2012-01-17 16:56:51 +00:00
parent a04797b779
commit 1932c68f8a
+55 -37
View File
@@ -32,26 +32,37 @@ include_once(_PS_SWIFT_DIR_.'Swift/Plugin/Decorator.php');
class MailCore 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')); $configuration = Configuration::getMultiple(array('PS_SHOP_EMAIL',
if(!isset($configuration['PS_MAIL_SMTP_ENCRYPTION'])) $configuration['PS_MAIL_SMTP_ENCRYPTION'] = 'off'; 'PS_MAIL_METHOD',
if(!isset($configuration['PS_MAIL_SMTP_PORT'])) $configuration['PS_MAIL_SMTP_PORT'] = 'default'; '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($from)) $from = $configuration['PS_SHOP_EMAIL'];
if (!isset($fromName)) $fromName = $configuration['PS_SHOP_NAME']; 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); Tools::dieOrLog(Tools::displayError('Error: parameter "from" is corrupted'), $die);
return false; 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); Tools::dieOrLog(Tools::displayError('Error: parameter "fromName" is corrupted'), $die);
return false; 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); Tools::dieOrLog(Tools::displayError('Error: parameter "to" is corrupted'), $die);
return false; return false;
@@ -64,10 +75,10 @@ class MailCore
} }
// Do not crash for this error, that may be a complicated customer name // 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)) if (!empty($toName) && !Validate::isMailName($toName))
$toName = NULL; $toName = null;
} }
if (!Validate::isTplName($template)) if (!Validate::isTplName($template))
@@ -83,42 +94,44 @@ class MailCore
} }
/* Construct multiple recipients list if needed */ /* Construct multiple recipients list if needed */
if (is_array($to) and isset($to)) if (is_array($to) && isset($to))
{ {
$to_list = new Swift_RecipientList(); $to_list = new Swift_RecipientList();
foreach ($to AS $key => $addr) foreach ($to as $key => $addr)
{ {
$to_name = NULL; $to_name = null;
$addr = trim($addr); $addr = trim($addr);
if (!Validate::isEmail($addr)) if (!Validate::isEmail($addr))
{ {
Tools::dieOrLog(Tools::displayError('Error: invalid email address'), $die); Tools::dieOrLog(Tools::displayError('Error: invalid email address'), $die);
return false; 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_name = $toName[$key];
} }
$to_list->addTo($addr, $to_name); $to_list->addTo($addr, base64_encode($to_name));
} }
$to_plugin = $to[0]; $to_plugin = $to[0];
$to = $to_list; $to = $to_list;
} else { } else {
/* Simple recipient, one address */ /* Simple recipient, one address */
$to_plugin = $to; $to_plugin = $to;
$to = new Swift_Address($to, $toName); $to = new Swift_Address($to, base64_encode($toName));
} }
try { try {
/* Connect with the appropriate configuration */ /* Connect with the appropriate configuration */
if ($configuration['PS_MAIL_METHOD'] == 2) 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); Tools::dieOrLog(Tools::displayError('Error: invalid SMTP server or SMTP port'), $die);
return false; 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); $connection->setTimeout(4);
if (!$connection) if (!$connection)
return false; return false;
@@ -146,43 +159,44 @@ class MailCore
$overrideMail = false; $overrideMail = false;
// get templatePath // 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]; $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'))) file_exists(_PS_THEME_DIR_.'modules/'.$moduleName.'/mails/'.$template.'.html')))
$templatePath = _PS_THEME_DIR_.'modules/'.$moduleName.'/mails/'; $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/'; $templatePath = _PS_THEME_DIR_.'mails/';
$overrideMail = true; $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); Tools::dieOrLog(Tools::displayError('Error - The following email template is missing:').' '.$templatePath.$template.'.txt', $die);
return false; return false;
} }
$templateHtml = file_get_contents($templatePath.$template.'.html'); $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'); 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'); include_once(_PS_THEME_DIR_.'mails/'.$iso.'/lang.php');
else else
include_once(dirname(__FILE__).'/../mails/'.$iso.'/lang.php'); include_once(dirname(__FILE__).'/../mails/'.$iso.'/lang.php');
/* Create mail and attach differents parts */ /* Create mail and attach differents parts */
$message = new Swift_Message('['.Configuration::get('PS_SHOP_NAME').'] '. $subject); $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_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_name}'] = Tools::safeOutput(Configuration::get('PS_SHOP_NAME'));
$templateVars['{shop_url}'] = Tools::getShopDomain(true, true).__PS_BASE_URI__; $templateVars['{shop_url}'] = Tools::getShopDomain(true, true).__PS_BASE_URI__;
$swift->attachPlugin(new Swift_Plugin_Decorator(array($to_plugin => $templateVars)), 'decorator'); $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')); $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')); $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'])); $message->attach(new Swift_Message_Attachment($fileAttachment['content'], $fileAttachment['name'], $fileAttachment['mime']));
/* Send mail */ /* Send mail */
$send = $swift->send($message, $to, new Swift_Address($from, $fromName)); $send = $swift->send($message, $to, new Swift_Address($from, $fromName));
@@ -190,18 +204,22 @@ class MailCore
return $send; 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) public static function sendMailTest($smtpChecked, $smtpServer, $content, $subject, $type, $to, $from, $smtpLogin, $smtpPassword, $smtpPort = 25, $smtpEncryption)
{ {
$swift = NULL; $swift = null;
$result = false; $result = false;
try 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->setUsername($smtpLogin);
$smtp->setpassword($smtpPassword); $smtp->setpassword($smtpPassword);
$smtp->setTimeout(5); $smtp->setTimeout(5);
@@ -236,7 +254,7 @@ class MailCore
* *
* @param string $string raw sentence (write directly in file) * @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; global $_LANGMAIL;
if (!$context) if (!$context)
@@ -244,7 +262,7 @@ class MailCore
$key = str_replace('\'', '\\\'', $string); $key = str_replace('\'', '\\\'', $string);
if ($id_lang == null) 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'; $file_core = _PS_ROOT_DIR_.'/mails/'.$context->language->iso_code.'/lang.php';
if (Tools::file_exists_cache($file_core) && empty($_LANGMAIL)) if (Tools::file_exists_cache($file_core) && empty($_LANGMAIL))