Question

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
}
Was it helpful?

Solution

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.

OTHER TIPS

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 \resources\emails

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

AGARCIA - 2018
</body>
</html>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top