Yii Framework Forum: SwiftMailer with different users - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

SwiftMailer with different users How to add another host, username and password?

#1 User is offline   KVron 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 25-May 10

Posted 14 September 2017 - 06:54 PM

I have the following in main-local.php:

'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'mail.myexample.com',
'username' => 'notifications@myexample.com',
'password' => 'SuperS3cretPass,
'port' => '587',
'encryption' => 'tls',
'streamOptions' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false
],
],
],
],
and it's working just fine but now I need to add another user, lets say support@myexample.com to send another type of emails.

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.
0

#2 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 3,710
  • Joined: 10-October 10
  • Location:Denmark

Posted 14 September 2017 - 07:02 PM

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.
"Less noise - more signal"
0

#3 User is offline   KVron 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 25-May 10

Posted 14 September 2017 - 09:20 PM

View Postjacmoe, on 14 September 2017 - 07:02 PM, said:

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.


But how? Yii::$app->mailer->
only gives "compose()", "send()" and "sendMultiple()"
I'm sure it's possible but can't find a way to do so.
0

#4 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 3,710
  • Joined: 10-October 10
  • Location:Denmark

Posted 14 September 2017 - 10:18 PM

Exactly!

Yii::$app->mail->compose()
     ->setFrom('somebody@domain.com')
     ->setTo('myemail@yourserver.com')
     ->setSubject('Email sent from Yii2-Swiftmailer')
     ->send();


Read this: http://www.yiiframew...ails-using-smtp
"Less noise - more signal"
0

#5 User is offline   KVron 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 25-May 10

Posted 14 September 2017 - 10:21 PM

Ok, got it!!

In case someone else needs to do the same:

// Create the transport
$transport = \Swift_SmtpTransport::newInstance();
$transport->setUsername('contact@mail.example.com');
$transport->setPassword('S3cretPass');
$transport->setHost('mail.example.com');
$transport->setPort(587);
$transport->setEncryption('tls');
$transport->setStreamOptions(['ssl' => ['allow_self_signed' => true, 'verify_peer' => false, 'verify_peer_name' => false]]);

// Create the message
$message = \Swift_Message::newInstance();
$message->setTo(['user@gmail.com']);
$message->setSubject("Some contact email");
$message->setBody("Thank you for contacting us!");
$message->setFrom("contact@mail.example.net", "John Doe");

// Send the email
$mailer = \Swift_Mailer::newInstance($transport);
$mailer->send($message);

Thank you!!!
1

#6 User is offline   shiv 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 109
  • Joined: 05-November 11
  • Location:India

Posted 14 September 2017 - 10:52 PM

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 
					] 
			] );


Shiv charan panjeta
Website: https://toxsl.com
E mail : sales@toxsl.com
Contact: +91 9569127788
1

#7 User is offline   KVron 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 25-May 10

Posted 15 September 2017 - 10:19 AM

View Postshiv, on 14 September 2017 - 10:52 PM, said:

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 
					] 
			] );




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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users