PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Oct 23, 2017 8:13 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Tue Jan 13, 2015 1:39 am 
Offline
Forum Newbie

Joined: Tue Jan 13, 2015 1:37 am
Posts: 2
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;
}


Top
 Profile  
 
PostPosted: Tue Jan 13, 2015 3:06 am 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine
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.


Top
 Profile  
 
PostPosted: Tue Jan 13, 2015 3:31 am 
Offline
Forum Newbie

Joined: Tue Jan 13, 2015 1:37 am
Posts: 2
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.


Top
 Profile  
 
PostPosted: Tue Jan 13, 2015 3:59 am 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine
$event->getTransport() where $event is an instance of Swift_Events_SendEvent passed in to the event handler method.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group