SwiftMailer with different users


(Lfparedes) #1

I have the following in main-local.php:

[color=#008000]‘mailer’ [/color]=> [

[color=#008000][b]'class' [/b][/color]=> [color=#008000][b]'yii\swiftmailer\Mailer'[/b][/color],


[color=#008000][b]'viewPath' [/b][/color]=> [color=#008000][b]'@common/mail'[/b][/color],[color=#808080][i]

[/i][/color][color=#808080] [/color][color=#008000]‘transport’ [/color]=> [

    [color=#008000][b]'class' [/b][/color]=> [color=#008000][b]'Swift_SmtpTransport'[/b][/color],


    [color=#008000][b]'host' [/b][/color]=> [color=#008000][b]'mail.[/b][/color]myexample.com'[size=2],[/size]


    [color=#008000][b]'username' [/b][/color]=> [color=#008000][b]'notifications@myexample.com'[/b][/color],


    [color=#008000][b]'password' [/b][/color]=> [color=#008000][b]'SuperS3cretPass[/b][/color],


    [color=#008000][b]'port' [/b][/color]=> [color=#008000][b]'587'[/b][/color],


    [color=#008000][b]'encryption' [/b][/color]=> [color=#008000][b]'tls'[/b][/color],


    [color=#008000][b]'streamOptions' [/b][/color]=> [


        [color=#008000][b]'ssl' [/b][/color]=> [


            [color=#008000][b]'allow_self_signed' [/b][/color]=> [color=#000080][b]true[/b][/color],


            [color=#008000][b]'verify_peer' [/b][/color]=> [color=#000080][b]false[/b][/color],


            [color=#008000][b]'verify_peer_name' [/b][/color]=> [color=#000080][b]false

[/b][/color][color=#000080] [/color]],

    ],


],

],

and it’s working just fine but now I need to add another user, lets say [color="#008000"][size=“2”]support@[/size][/color][color="#2e8b57"]myexample.com[/color][size=“2”] to send another type of emails.[/size]

My question is, how do I add this new user or how do I change the one configured in main-local.php on the fly?

Thank you.


(Jacob Moen) #2

Simply set the properties in code :)

The configuration is basically just convenience, you can set it up in code, but a config helps to avoid code duplication.

So just set the properties in the code where it makes sense.


(Lfparedes) #3

But how? [font=Menlo][size=2]Yii::[/size][/font][color=#660E7A][font=Menlo][size=2]$app[/size][/font][/color][font=Menlo][size=2]->[/size][/font][color=#000080][font=Menlo][size=2]mailer[/size][/font][/color][font=Menlo][size=2]-> [/size][/font]

[font=Menlo][size=2]only gives "compose()", "send()" and "sendMultiple()"[/size][/font]

[size=2]I’m sure it’s possible but can’t find a way to do so.[/size]


(Jacob Moen) #4

Exactly!


Yii::$app->mail->compose()

     ->setFrom('somebody@domain.com')

     ->setTo('myemail@yourserver.com')

     ->setSubject('Email sent from Yii2-Swiftmailer')

     ->send();



Read this: http://www.yiiframework.com/wiki/656/how-to-send-emails-using-smtp


(Lfparedes) #5

Ok, got it!!

In case someone else needs to do the same:

[color="#808080"][i]// Create the transport

[/i][/color][color="#660000"]$transport [/color]= \Swift_SmtpTransport::[i]newInstance/i;

[color="#660000"]$transport[/color]->[color="#000080"]setUsername/color;

[color="#660000"]$transport[/color]->[color="#000080"]setPassword/color;

[color="#660000"]$transport[/color]->setHost([color="#008000"]mail.example.com[/color]);

[color="#660000"]$transport[/color]->setPort([color="#0000ff"]587[/color]);

[color="#660000"]$transport[/color]->setEncryption([color="#008000"]‘tls’[/color]);

[color="#660000"]$transport[/color]->setStreamOptions([[color="#008000"]‘ssl’ [/color]=> [[color="#008000"]‘allow_self_signed’ [/color]=> [color="#000080"]true[/color], [color="#008000"]‘verify_peer’ [/color]=> [color="#000080"]false[/color], [color="#008000"]‘verify_peer_name’ [/color]=> [color="#000080"]false[/color]]]);

[color="#808080"][i]// Create the message

[/i][/color][color="#660000"]$message [/color]= \Swift_Message::[i]newInstance/i;

[color="#660000"]$message[/color]->setTo([[color="#008000"]‘user@gmail.com’[/color]]);

[color="#660000"]$message[/color]->setSubject([color="#008000"]"Some contact email"[/color]);

[color="#660000"]$message[/color]->setBody([color="#008000"]"Thank you for contacting us!"[/color]);

[color="#660000"]$message[/color]->setFrom([color="#008000"]"contact@mail.example.net"[/color], [color="#008000"]"John Doe"[/color]);

[color="#808080"][i]// Send the email

[/i][/color][color="#660000"]$mailer [/color]= \Swift_Mailer::[i]newInstance/i;

[color="#660000"]$mailer[/color]->send([color="#660000"]$message[/color]);

Thank you!!!


(Shivcharan Panjeta) #6

We do this a lot. You can create component on fly if you have to …




$mailer = \Yii::createObject ( [ 

					'class' => 'yii\swiftmailer\Mailer',

					'useFileTransport' => (YII_ENV == 'dev' || defined ( 'DRYRUN' )) ? true : false,

					'transport' => [ 

							'class' => 'Swift_SmtpTransport',

							'host' => $emailAccount->server_info,

							'username' => $emailAccount->email,

							'password' => $emailAccount->password 

					] 

			] );


(Lfparedes) #7

Nice!! Didn’t know you could do this. Much less verbose than my method, thanks for the tip!!