Prestashop源码分析之通知邮件发送及标题多语言配置
2024-12-30 07:32阅读:
PS框架中邮件发送的主函数是在Mail类中的Send函数,如下:
public static function
Send($id_lang, $template, $subject,
$template_vars, $to,
$to_name = null, $from = null, $from_name = null,
$file_attachment = null, $mode_smtp = null,
$template_path = _PS_MAIL_DIR_, $die = false, $id_shop =
null, $bcc = null)
{
if (!$id_shop)
$id_shop =
Context::getContext()->shop->id;
$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_TYPE'
), null, null, $id_shop);
// Returns immediatly if emails are deactivated
if
($configuration['PS_MAIL_METHOD']
== 3)
return true;
$theme_path = _PS_THEME_DIR_;
// Get the path of theme by id_shop if exist
if
(is_numeric($id_shop) &&
$id_shop)
{
$shop = new
Shop((int)$id_shop);
$theme_name =
$shop->getTheme();
if (_THEME_NAME_ != $theme_name)
$theme_path =
_PS_ROOT_DIR_.'/themes/'.$theme_name.'/';
}
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';
// Sending an e-mail can be of vital importance for the
merchant, when his password is lost for example, so we must not die
but do our best to send the e-mail
if (!isset($from) ||
!Validate::isEmail($from))
$from =
$configuration['PS_SHOP_EMAIL'];
if
(!Validate::isEmail($from))
$from = null;
// $from_name is not that important, no need to die if it is
not valid
if (!isset($from_name)
||
!Validate::isMailName($from_name))
$from_name =
$configuration['PS_SHOP_NAME'];
if
(!Validate::isMailName($from_name))
$from_name = null;
// It would be difficult to send an e-mail if the e-mail is
not valid, so this time we can die if there is a
problem
if (!is_array($to)
&&
!Validate::isEmail($to))
{
Tools::dieOrLog(Tools::displayError('Error:
parameter 'to' is corrupted'), $die);
var_dump('mail
send1');
return false;
}
// if bcc is not null, make sure it's a vaild
e-mail
if (!is_null($bcc)
&& !is_array($bcc) &&
!Validate::isEmail($bcc))
{
Tools::dieOrLog(Tools::displayError('Error:
parameter 'bcc' is corrupted'), $die);
$bcc = null;
}
if
(!is_array($template_vars))
$template_vars =
array();
// Do not crash for this error, that may be a complicated
customer name
if
(is_string($to_name) &&
!empty($to_name) &&
!Validate::isMailName($to_name))
$to_name = null;
if
(!Validate::isTplName($template))
{
Tools::dieOrLog(Tools::displayError('Error:
invalid e-mail template'), $die);
var_dump('mail
send2');
return false;
}
if
(!Validate::isMailSubject($subject))
{
Tools::dieOrLog(Tools::displayError('Error:
invalid e-mail subject'), $die);
var_dump('mail
send3');
return false;
}
$to_list = new
Swift_RecipientList();
if (is_array($to)
&& isset($to))
{
foreach ($to as $key => $addr)
{
$addr = trim($addr);
if
(!Validate::isEmail($addr))
{
Tools::dieOrLog(Tools::displayError('Error:
invalid e-mail address'), $die);
return false;
}
if (is_array($to_name)
&& $to_name &&
is_array($to_name) &&
Validate::isGenericName($to_name[$key]))
$to_name = $to_name[$key];
$to_name = (($to_name == null || $to_name == $addr)
? '' :
self::mimeEncode($to_name));
$to_list->addTo($addr,
$to_name);
}
$to_plugin = $to[0];
}
else
{
$to_plugin = $to;
$to_name = (($to_name == null || $to_name == $to)
? '' :
self::mimeEncode($to_name));
$to_list->addTo($to,
$to_name);
}
if
(isset($bcc))
$to_list->addBcc($bcc);
try {
if
($configuration['PS_MAIL_METHOD']
== 2)
{
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->setTimeout(4);
if (!$connection)
return false;
if
(!empty($configuration['PS_MAIL_USER']))
$connection->setUsername($configuration['PS_MAIL_USER']);
if
(!empty($configuration['PS_MAIL_PASSWD']))
$connection->setPassword($configuration['PS_MAIL_PASSWD']