在 PrestaShop 1.6 中调试 Swift 邮件程序

Debug Swift Mailer in PrestaShop 1.6

PrestaShop 的 Mail::send 功能偶尔会导致发送邮件失败。我使用外部邮件服务器,那里的负责人告诉我没有收到发送任何邮件的请求(虽然我觉得这很奇怪)。所以,我做了一个小记录器,做了以下事情:

if ( !Mail::send() )
    // logging details here here..

Mail::send returns false 正确。现在我想知道原因。因此,我想在 SwiftMailer 中记录有关连接的信息。有人可以给我任何线索如何在一个简单的庄园中解决这个问题(如果可能,不要使用 SwiftMailer 插件)。

Swift 邮件程序版本:3.3.2

设置$die参数为真显示错误。您需要将 _PS_MODE_DEV_ 设置为 true 才能看到它。

/**
 * Send Email
 *
 * @param int $id_lang Language ID of the email (to translate the template)
 * @param string $template Template: the name of template not be a var but a string !
 * @param string $subject Subject of the email
 * @param string $template_vars Template variables for the email
 * @param string $to To email
 * @param string $to_name To name
 * @param string $from From email
 * @param string $from_name To email
 * @param array $file_attachment Array with three parameters (content, mime and name). You can use an array of array to attach multiple files
 * @param bool $mode_smtp SMTP mode (deprecated)
 * @param string $template_path Template path
 * @param bool $die Die after error
 * @param string $bcc Bcc recipient
 * @return bool|int Whether sending was successful. If not at all, false, otherwise amount of recipients succeeded.
 */
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, $reply_to = null)

或者您可以转到后台和菜单 Advanced Parameters -> Logs,输入消息过滤器:error 并搜索结果。不能保证它只会向您显示电子邮件错误,但它会缩小很多范围。

如果您将 $die 参数设置为 true,您将导致该脚本停止,并且在购买时给客户或管理员在更改订单状态等时带来糟糕的体验。您应该检查以下内容:

  1. 检查特定电子邮件模板的电子邮件发送是否失败。如果为真,您可能会遇到模板结构、传递的参数、html 渲染等方面的问题。
  2. Mail::Send 函数中的每个错误都记录在 Prestashop 日志中。所以你应该在那里检查它。您可以知道在函数 Mail::Send: Tools::dieOrLog(Tools::displayError('Error: invalid SMTP server or SMTP port'), $die);
  3. 中识别此类代码的所有可能错误
  4. 特定的 Swift 异常使用此代码记录:PrestaShopLogger::addLog('Swift Error: ' . $e->getMessage(), 3, null, 'Swift_Message'); 因此它也应该出现在 Prestashop 日志中。
  5. 反正Swift有自己的登录classSwift_Log_DefaultLog。您可以在 \tools\swift\Swift\Log\DefaultLog.php 找到它。您可以修改 adddump 函数以将日志保存到特定文件或类似文件。

祝你好运。

无法告诉您 Mail::send() 函数失败的原因。失败可能有多种原因。

在Mail::send()函数中那个returnsfalse有12行代码,每行都有不同的条件。您需要调试函数才能找到确切原因。

您可以在位于 /classes/Mail 的 Mail.php 文件中找到 Mail::send() 函数。php

提示:您可以为函数中的每个 'return false;' 语句添加日志,以检查正在执行的语句。