Home > PHP Framework > Laravel > body text

Laravel6 elegantly switches sending accounts

Guanhui
Release: 2020-06-20 18:00:03
forward
3257 people have browsed it

Laravel6 elegantly switches sending accounts

##Preface

When making a notification system, According to business needs, use different accounts to send emails according to different scenarios. Laravel only supports sending emails from one email address by default. Unsatisfied with the actual situation, after using the Config::set() method to dynamically set the account, the email can be successfully sent, but the sending account cannot be modified again by setting again.

Recommended tutorial: "

Laravel Tutorial"

##The method is as follows: Create email account configuration file/config/my_emails.php

 [
  'a' => [
   'email' => '[email protected]',
   'password' => '专属客户端密码',
   'smtp' => 'smtp.188.com',
   'port' => '465',
   'encryption' => 'ssl',
   'name' => '靓仔A',
  ],
  'b' => [
   'email' => '[email protected]',
   'password' => '专属客户端密码',
   'smtp' => 'smtp.188.com',
   'port' => '994',
   'encryption' => 'ssl',
   'name' => '靓女b',
  ],
 ],
];
Copy after login

Next create the switching assistant class

setUsername(config("my_emails.emails.{$accountName}.email"));
  $transport->setPassword(config("my_emails.emails.{$accountName}.password"));
  $mailer = new \Swift_Mailer($transport);
  Mail::setSwiftMailer($mailer);
  Mail::alwaysFrom(config("my_emails.emails.{$accountName}.email"), config("my_emails.emails.{$accountName}.name"));
 }
}
Copy after login

The actual usage is as follows:

send(new TestMail());
MailHelper::setAccount('b');
Mail::to('[email protected]')->send(new TestMail());
Copy after login

SummaryThis concludes this article on how to elegantly switch sending accounts in Laravel 6.18.19

Related recommendations: "
PHP Tutorial

The above is the detailed content of Laravel6 elegantly switches sending accounts. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!