1

I'd like to use different smtp account for sending emails depending on context of the application. So I found this answer, but it work only only for changing username and password.

Changing smtp settings in SwiftMailer dynamically

I need to dynamically change host as well.

Any idea will be appreciated.

Community
  • 1
  • 1
Ladislav M
  • 2,157
  • 4
  • 36
  • 52
  • just instantiate a different swiftmailer object with the new settings. at some point you'd have to be making so many changes to the setup the object that it's just easier to carry multiple versions of the object instead. – Marc B Nov 14 '13 at 14:57

1 Answers1

7

The same way as you set username and password, you can set host, port and encryption:

$transport = $fromYourMailerVariable->getTransport();
$transport
    ->setUserName(...)
    ->setPassword(...)
    ->setHost(...)
    ->setPort(...)
    ->setEncryption(...);

Or just create different mailers (with different transports):

$transport1 = Swift_SmtpTransport::newInstance('localhost');

$transport2 = Swift_SmtpTransport::newInstance('smtp.domain.com', 666)
                ->setUsername('user')
                ->setPassword('pass');

$transport3 = Swift_SendmailTransport::newInstance('/usr/sbin/exim -bs');

$transport4 = Swift_MailTransport::newInstance();
Glavić
  • 42,781
  • 13
  • 77
  • 107
  • But I always get "Fatal error: Call to undefined method Swift_Transport_Esmtp_AuthHandler::setHost()". – Ladislav M Nov 14 '13 at 15:11
  • 1
    I don't know how you are creating or fetching transport, but here is the working example > http://pastebin.com/fsVEkgZK – Glavić Nov 14 '13 at 15:15