phpswiftmailer

Retrieving SMTP conversation in SwiftMailer


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
}

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:

    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.