Page 1 of 1

Swiftmailer Multiple SMTP

Posted: Tue Jan 13, 2015 12:39 am
by rorossf
I'm using the PHP Swiftmailer package and was looking for a way to use multiple SMTP servers. I found the Swift LoadBalance Transport and from what I read this would round robin between SMTP connection. However, I have two SMTP servers with each one is running a different mailserver domain. Is there a way to change the ->setFrom(MAIL_FROM) based on the SMTP server transport instance being used?

Ex. $transport1 uses a FROM: company@domain1.com $transport2 uses a FROM: company@domain2.com

Code: Select all

// POINT of this sample
$transport1 = Swift_SmtpTransport::newInstance(SMTP_HOST, SMTP_PORT);
$transport2 = Swift_SmtpTransport::newInstance(SMTP_HOST2, SMTP_PORT2);
$transport = Swift_LoadBalancedTransport::newInstance([
$transport1,
$transport2,
]);

$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance();
$message
->setFrom(MAIL_FROM)
->setSubject('LoadbalancedTransport sample')
->setBody('This is a mail.');
$recipients = [MAIL_TO, MAIL_TO2];
$total = 0;


foreach ($recipients as $recipient) {
$message->setTo($recipient);
$result = $mailer->send($message);
$total += $result;
}

Re: Swiftmailer Multiple SMTP

Posted: Tue Jan 13, 2015 2:06 am
by Weirdan
It should be pretty easy with a plugin handling 'beforeSendPerformed' event (you could base it on ImpersonatePlugin). It receives Swift_Events_SendEvent, from which you could get both transport being used to send the message and the message itself, so you could modify the originating address accordingly.

Re: Swiftmailer Multiple SMTP

Posted: Tue Jan 13, 2015 2:31 am
by rorossf
Weirdan wrote:It should be pretty easy with a plugin handling 'beforeSendPerformed' event (you could base it on ImpersonatePlugin). It receives Swift_Events_SendEvent, from which you could get both transport being used to send the message and the message itself, so you could modify the originating address accordingly.
Thank you very much. Starting to understand how to approach this, the only question I have is how would I get the transports being used to send the message.

Re: Swiftmailer Multiple SMTP

Posted: Tue Jan 13, 2015 2:59 am
by Weirdan
$event->getTransport() where $event is an instance of Swift_Events_SendEvent passed in to the event handler method.