Search code examples
phpswiftmailer

How do I setup fallback on Swift Mailer framework in php


How do I set fallback, for swift mailer, and test my fallback code is working?. I got this standart code from the howto.

$transport = Swift_SmtpTransport::newInstance(SMTP_HOST, SMTP_PORT, 'tls')
->setUsername(SMTP_USERNAME)
->setPassword(SMTP_PASSWORD)
;

$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('S5_Dagsrapport_' . $dato1)
->setFrom(array($from => 'S5'))
->setTo(array($to => $navn))
->setBody($body, 'text/html')
->addPart($body, 'text/html')
->attach(Swift_Attachment::newInstance($attachment, 's5_dagsreport_' . $dato1 . '.xls',          'application/xls'))
;
// Send the message
$mailer->send($message);

if i use

if(
$mailer->send($message)){
echo "Sent\n";
}else{
echo "Failed\n";
}

it shows sent, but if I put wrong info , into the parameters, it does not show Failed. I have been reading that another solution is to use

Swift_Transport_FailoverTransport

but I can't find examples online, on that.

what is the easy way to make a fallback in Swift_mailer,


Solution

  • I can tell from the class Swift_Transport_FailoverTransport extends.

    https://github.com/swiftmailer/swiftmailer/blob/master/lib/classes/Swift/Transport/LoadBalancedTransport.php

    Contains a method called setTransports. In there you can set the transports to use when one fails. It is a bit like a chain class. The class itself is an instance of Swift_Transport which contains multiple other transports.

    Create the transports you are going to use to connect to mail servers. Then add them to an instance of Swift_Transport_FailoverTransport. Then use that instance as your mailer transport.