Home > PHP Framework > Laravel > body text

Laravel6 elegantly switches sending accounts

Guanhui
Release: 2020-06-20 18:00:03
forward
3417 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

<?php
return [
 &#39;emails&#39; => [
  &#39;a&#39; => [
   &#39;email&#39; => &#39;a@188.com&#39;,
   &#39;password&#39; => &#39;专属客户端密码&#39;,
   &#39;smtp&#39; => &#39;smtp.188.com&#39;,
   &#39;port&#39; => &#39;465&#39;,
   &#39;encryption&#39; => &#39;ssl&#39;,
   &#39;name&#39; => &#39;靓仔A&#39;,
  ],
  &#39;b&#39; => [
   &#39;email&#39; => &#39;b@188.com&#39;,
   &#39;password&#39; => &#39;专属客户端密码&#39;,
   &#39;smtp&#39; => &#39;smtp.188.com&#39;,
   &#39;port&#39; => &#39;994&#39;,
   &#39;encryption&#39; => &#39;ssl&#39;,
   &#39;name&#39; => &#39;靓女b&#39;,
  ],
 ],
];
Copy after login

Next create the switching assistant class

<?php
namespace App\Mail;
use Illuminate\Support\Facades\Mail;
 
class MailHelper
{
 public static function setAccount($accountName)
 {
  $transport = new \Swift_SmtpTransport(
   config("my_emails.emails.{$accountName}.smtp"),
   config("my_emails.emails.{$accountName}.port"),
   config("my_emails.emails.{$accountName}.encryption")
  );
  $transport->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:

<?php
MailHelper::setAccount(&#39;a&#39;);
Mail::to(&#39;boy@163.com&#39;)->send(new TestMail());
MailHelper::setAccount(&#39;b&#39;);
Mail::to(&#39;girl@163.com&#39;)->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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template