PHP Developers Network
http://forums.devnetwork.net/

Swiftmailer Multiple SMTP
http://forums.devnetwork.net/viewtopic.php?f=52&t=140849
Page 1 of 1

Author:  rorossf [ Tue Jan 13, 2015 1:39 am ]
Post subject:  Swiftmailer Multiple SMTP

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
Syntax: [ Download ] [ Hide ]
// 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;
}

Author:  Weirdan [ Tue Jan 13, 2015 3:06 am ]
Post subject:  Re: Swiftmailer Multiple SMTP

It should be pretty easy with a plugin handling 'beforeSendPerformed' event (you could base it on ). It receives , 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.

Author:  rorossf [ Tue Jan 13, 2015 3:31 am ]
Post subject:  Re: Swiftmailer Multiple SMTP


Author:  Weirdan [ Tue Jan 13, 2015 3:59 am ]
Post subject:  Re: Swiftmailer Multiple SMTP

$event->getTransport() where $event is an instance of Swift_Events_SendEvent passed in to the event handler method.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/