在SwiftMailer中检索SMTP会话

I am using SwiftMailer to send emails and my site is all of a sudden having timeout issues when trying to send an email.

I need to extract the "SMTP conversation" so my host can debug it.

Is there any code that can give me this?

include('SwiftMailer/swift_required.php');
$transport = Swift_SmtpTransport::newInstance('smtp.example.com', 587);
$transport->setUsername('username');
$transport->setPassword('password');
$swift = Swift_Mailer::newInstance($transport);

// Create a message
$message = new Swift_Message($subject);
$message->setFrom($from);
$message->setBody($message, $content_type, SITE_CHARSET);
$message->setTo($to);

try {
    $swift->send($message);
} catch (Swift_TransportException $e) {
    // Log array for further inspection
}
php

2个回答

You might want to take a look at the SwiftMailer Logger Plugin which will allow you log all the interactions between your client and the SMTP server.

There is two types of loggers available:

  • The ArrayLogger - (Keeps a collection of log messages inside an array. The array content can be cleared or dumped out to the screen)
  • The EchoLogger (Prints output to the screen in realtime. Handy for very rudimentary debug output)

You can register the plugin using either:

$logger = new Swift_Plugins_Loggers_ArrayLogger();
$swift->registerPlugin(new Swift_Plugins_LoggerPlugin($logger));

or

$logger = new Swift_Plugins_Loggers_EchoLogger();
$swift->registerPlugin(new Swift_Plugins_LoggerPlugin($logger));

Just make sure you have the plugin installed and that your class autoloader can access the required class on demand.

Refer to the documentation for further details.

dongqiaozhe5070
dongqiaozhe5070 谢谢@SquareCat。 原来是某种连接拥堵,最终在大约24小时后自行解决。 记录器虽然很有帮助但感谢您分享它的帮助:)
接近 7 年之前 回复
dongwo5940
dongwo5940 哦。 那么在这种情况下,您可以安全地假设您的SMTP主机名或端口无效(或无法访问)。 当连接失败时,您无论如何都不会看到太多的SMTP会话。 试着搞清楚它为什么不起作用。 检查SMTP服务器是否有变化。 另外,看看你是否可以通过telnet手动连接(如果它不起作用,找出你正在处理的防火墙问题)。
接近 7 年之前 回复
ds34222
ds34222 这很好,但记录器似乎没有给我SMTP对话...比较它给我这个例子,它是非常不同的。 记录器给了我:错误:++启动Swift_SmtpTransport !! 无法与主机smtp.example.com建立连接[连接超时#110]
接近 7 年之前 回复

Example with Laravel Dump the conversation to log:

$content = 'Test';
$title = 'Test now()= ' . date("Y-m-d",time());                
$logger = new \Swift_Plugins_Loggers_ArrayLogger();
Mail::getSwiftMailer()->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger));
Mail::send('emails.send', ['title' => $title, 'content' => $content], function ($message){
 $message->from('fromuser@mydomain.com');
 $message->to('touser@mydomain.com');
});
Log::info($logger->dump());

And send.blade.php at esources\emails

<html>
<head></head>
<body style="background: orange; color: white">
<h1>{{$title}}</h1>
<p>{{$content}}</p>

AGARCIA - 2018
</body>
</html>
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐