Intro
阿里网易企业邮发件可参考另一篇:Laravel 6 结合网易/阿里邮箱基本邮件发送功能使用
基本配置
Composer 安装 illuminate/mail 组件后将下方内容保存为 mail.php 放置于 Project/config 目录
1<?php 2 3return [ 4 5 /* 6 |-------------------------------------------------------------------------- 7 | Mail Driver 8 |-------------------------------------------------------------------------- 9 | 10 | Laravel supports both SMTP and PHP's "mail" function as drivers for the 11 | sending of e-mail. You may specify which one you're using throughout 12 | your application here. By default, Laravel is setup for SMTP mail. 13 | 14 | Supported: "smtp", "sendmail", "mailgun", "ses", 15 | "postmark", "log", "array" 16 | 17 */ 18 19 'driver' => env('MAIL_DRIVER', 'smtp'), 20 21 /* 22 |-------------------------------------------------------------------------- 23 | SMTP Host Address 24 |-------------------------------------------------------------------------- 25 | 26 | Here you may provide the host address of the SMTP server used by your 27 | applications. A default option is provided that is compatible with 28 | the Mailgun mail service which will provide reliable deliveries. 29 | 30 */ 31 32 'host' => env('MAIL_HOST', 'smtp.mailgun.org'), 33 34 /* 35 |-------------------------------------------------------------------------- 36 | SMTP Host Port 37 |-------------------------------------------------------------------------- 38 | 39 | This is the SMTP port used by your application to deliver e-mails to 40 | users of the application. Like the host we have set this value to 41 | stay compatible with the Mailgun e-mail application by default. 42 | 43 */ 44 45 'port' => env('MAIL_PORT', 587), 46 47 /* 48 |-------------------------------------------------------------------------- 49 | Global "From" Address 50 |-------------------------------------------------------------------------- 51 | 52 | You may wish for all e-mails sent by your application to be sent from 53 | the same address. Here, you may specify a name and address that is 54 | used globally for all e-mails that are sent by your application. 55 | 56 */ 57 58 'from' => [ 59 'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'), 60 'name' => env('MAIL_FROM_NAME', 'Example'), 61 ], 62 63 /* 64 |-------------------------------------------------------------------------- 65 | E-Mail Encryption Protocol 66 |-------------------------------------------------------------------------- 67 | 68 | Here you may specify the encryption protocol that should be used when 69 | the application send e-mail messages. A sensible default using the 70 | transport layer security protocol should provide great security. 71 | 72 */ 73 74 'encryption' => env('MAIL_ENCRYPTION', 'tls'), 75 76 /* 77 |-------------------------------------------------------------------------- 78 | SMTP Server Username 79 |-------------------------------------------------------------------------- 80 | 81 | If your SMTP server requires a username for authentication, you should 82 | set it here. This will get used to authenticate with your server on 83 | connection. You may also set the "password" value below this one. 84 | 85 */ 86 87 'username' => env('MAIL_USERNAME'), 88 89 'password' => env('MAIL_PASSWORD'), 90 91 /* 92 |-------------------------------------------------------------------------- 93 | Sendmail System Path 94 |-------------------------------------------------------------------------- 95 | 96 | When using the "sendmail" driver to send e-mails, we will need to know 97 | the path to where Sendmail lives on this server. A default path has 98 | been provided here, which will work well on most of your systems. 99 | 100 */ 101 102 'sendmail' => '/usr/sbin/sendmail -bs', 103 104 /* 105 |-------------------------------------------------------------------------- 106 | Markdown Mail Settings 107 |-------------------------------------------------------------------------- 108 | 109 | If you are using Markdown based email rendering, you may configure your 110 | theme and component paths here, allowing you to customize the design 111 | of the emails. Or, you may simply stick with the Laravel defaults! 112 | 113 */ 114 115 'markdown' => [ 116 'theme' => 'default', 117 118 'paths' => [ 119 resource_path('views/vendor/mail'), 120 ], 121 ], 122 123 /* 124 |-------------------------------------------------------------------------- 125 | Log Channel 126 |-------------------------------------------------------------------------- 127 | 128 | If you are using the "log" driver, you may specify the logging channel 129 | if you prefer to keep mail messages separate from other log entries 130 | for simpler reading. Otherwise, the default channel will be used. 131 | 132 */ 133 134 'log_channel' => env('MAIL_LOG_CHANNEL'), 135];
最后在 .env 中新增以下 8 项内容:
1# 发信驱动 2MAIL_DRIVER=smtp 3# 发信服务器 4MAIL_HOST= 5# 发信端口 6MAIL_PORT=465 7# 发件人邮箱帐号 8MAIL_USERNAME=abc@example.com 9# 发件人邮箱密码 10MAIL_PASSWORD=password 11# 加密方式 ssl/tls 12MAIL_ENCRYPTION=tls 13# 配置全局默认发件地址 14MAIL_FROM_ADDRESS=abc@example.com 15# 发件人名称 16MAIL_FROM_NAME=张三
服务注册
此处建议在 Project/app/Providers 中注册邮件服务,当然,所有的服务都建议在此注册。
1// AppServiceProvider.php 2 3use Illuminate\Contracts\Mail\{Mailer as MailerContract, MailQueue as MailerQueueContract}; 4use Illuminate\Mail\Mailer; 5use Illuminate\Mail\MailServiceProvider; 6 7// function register 8$this->app->register(MailServiceProvider::class); 9 10// 下方的 mailer 别名设置与否不影响发信 11$this->app->alias('mailer', Mailer::class); 12$this->app->alias('mailer', MailerContract::class); 13$this->app->alias('mailer', MailerQueueContract::class);
邮件发送
创建模板
在 Project/resources/views/emails 目录下创建 test.blade.php 文件:
1@component('mail::message') 2 # 你好 3 4{{ $message }} 5 6@component('mail::button', ['url' => $url]) 7 打开网站 8@endcomponent 9 10感谢,<br> 11{{ $app_name }} 12@endcomponent
创建邮件类
在 Project/app/Mail 下新建 TestMail.php 类载入模板
1<?php 2 3namespace App\Mail; 4 5use Illuminate\Bus\Queueable; 6use Illuminate\Mail\Mailable; 7use Illuminate\Queue\SerializesModels; 8 9class TestMail extends Mailable { 10 use Queueable, SerializesModels; 11 12 private $data; 13 14 /** 15 * Create a new message instance. 16 * 17 * @param array $data 18 */ 19 public function __construct(array $data = []) { 20 $this->data = $data; 21 } 22 23 /** 24 * Build the message. 25 * 26 * @return $this 27 */ 28 public function build(): InvoiceMail { 29 return $this->markdown('emails.test', $this->data); 30 } 31}
发送
1use App\Mail\TestMail; 2 3Mail::to('收件人地址')->send(new TestMail([ 4 'message' => '测试邮件发送消息', 5 'url' => 'https://maxsky.cc', 6 'app_name' => '邮件发送项目' 7]))
多发件人
举个栗子我们在 .env 中这样配置:(更多同理)
1MAIL_A_USERNAME=A@example.com 2MAIL_A_PASSWORD=password 3MAIL_A_FROM_ADDRESS=A@example.com 4MAIL_A_FROM_NAME="名称 A" 5 6MAIL_B_USERNAME=B@example.com 7MAIL_B_PASSWORD=password 8MAIL_B_FROM_ADDRESS=B@example.com 9MAIL_B_FROM_NAME="名称 B" 10 11# etc.
一般情况下,邮件服务注册的是默认配置文件,但我们可以对配置进行临时性修改
取消服务注册
在 AppServiceProvider.php 中注册的邮件服务我们可以删掉了
创建邮件服务类
在 Project/app/Services/Common 中创建 MailService 类:(参考,路径可任意)
1<?php 2 3namespace App\Services\Common; 4 5use Exception; 6use Illuminate\Mail\MailServiceProvider; 7 8class MailService { 9 10 private $type; 11 12 /** 13 * @param string $type 14 * 15 * @return MailService 16 */ 17 public function setFromType(string $type): MailService { 18 $this->type = strtoupper($type); 19 return $this; 20 } 21 22 /** 23 * @return void 24 * @throws Exception 25 */ 26 public function initSendService(): void { 27 if (!$this->type) { 28 throw new Exception('Send type not exist.'); 29 } 30 31 $this->registerProvider($this->getMailAccountConfig($this->type)); 32 } 33 34 /** 35 * @param string $type 36 * 37 * @return array 38 */ 39 private function getMailAccountConfig(string $type): array { 40 $username = env("MAIL_{$type}_USERNAME"); 41 $password = env("MAIL_{$type}_PASSWORD"); 42 $fromAddr = env("MAIL_{$type}_FROM_ADDRESS"); 43 $fromName = env("MAIL_{$type}_FROM_NAME"); 44 45 $config = config('mail'); 46 47 $config['username'] = $username; 48 $config['password'] = $password; 49 $config['from']['address'] = $fromAddr; 50 $config['from']['name'] = $fromName; 51 52 return $config; 53 } 54 55 /** 56 * @param array $config 57 * 58 * @return void 59 */ 60 private function registerProvider(array $config): void { 61 // set new mail config 62 config(['mail' => $config]); 63 // register provider 64 app()->register(MailServiceProvider::class); 65 } 66}
邮件发送
此处用依赖注入的方式实例化服务。
1<?php 2 3namespace App\Http\Controllers; 4 5use App\Http\Controllers\Controller; 6use App\Mail\TestMail; 7use App\Services\Common\MailService; 8use Exception; 9use Log; 10use Mail; 11 12class Test extends Controller { 13 14 private $mailService; 15 16 public function __construct(MailService $mailService) { 17 $this->mailService = $mailService; 18 } 19 20 public function index() { 21 try { 22 $this->mailService->setFromType('A')->initSendService(); 23 } catch (Exception $e) { 24 Log::error($e->getMessage()); 25 } 26 27 Mail::to('收件人地址')->send(new TestMail([ 28 'message' => '测试邮件发送 A', 29 'url' => 'https://maxsky.cc', 30 'app_name' => '邮件发送项目' 31 ])); 32 33 try { 34 $this->mailService->setFromType('B')->initSendService(); 35 } catch (Exception $e) { 36 Log::error($e->getMessage()); 37 } 38 39 Mail::to('收件人地址')->send(new TestMail([ 40 'message' => '测试邮件发送 B', 41 'url' => 'https://maxsky.cc', 42 'app_name' => '邮件发送项目' 43 ])); 44 } 45}
模板预览
更新一个模板预览的方法:
1$api->get('preview', function () { 2 $mail = new App\Mail\TestMail([ 3 'message' => '测试邮件发送 A', 4 'url' => 'https://maxsky.cc', 5 'app_name' => '邮件发送项目' 6 ]); 7 8 return $mail->render(); 9});
然后项目根目录运行 php -S localhost:80 -t public,最后访问 http://localhost/preview 即可。
修改模板
进入 Project/vendor/illuminate/mail/resources/views 目录,将 html 和 text 复制到 Project/resources/views/vendor/mail 下进行修改方可预览。
