Lumen5.7 实现邮件发送

Lumen5.7 实现邮件发送

安装依赖包

1
2
3
composer require illuminate/mail:5.7
composer require guzzlehttp/guzzle:6.3
composer require jeremeamia/SuperClosure

查看相关版本要求
illuminate/mail:5.7
guzzlehttp/guzzle:6.3
jeremeamia/SuperClosure

添加配置

  • 添加mail.php配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php

return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
'log_channel' => env('MAIL_LOG_CHANNEL'),
];
  • 加载配置文件 bootstrap/app.php
1
$app->configure('mail');
  • 注册 mailer – 在 bootstrap/app.php 中,return $app; 之前,增加以下内容:
1
2
3
$app->singleton('mailer', function () use ($app) {
return $app->loadComponent('mail', Illuminate\Mail\MailServiceProvider::class, 'mailer');
});

使用

  • EmailSendController
1
Mail::to($email)->send(new EmailVerify($code, $prefix));
  • EmailVerify
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php

namespace App\Support;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class EmailVerify extends Mailable
{
use Queueable, SerializesModels;

public $code;
public $subject;

const EMAIL_SUBJECT = [
'register' => '【爱豆盒子】用户注册确认',
'password' => '【爱豆盒子】重置密码'
];

/**
* EmailVerify constructor.
* @param $code
* @param string $prefix
*/
public function __construct($code, $prefix = 'register')
{
$this->code = $code;
$this->subject = static::EMAIL_SUBJECT[$prefix];
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from(env('MAIL_FROM_ADDRESS'), env('MAIL_FROM_NAME'))
->view('emails.email')
->subject($this->subject)
->with([
'verify_code' => $this->code,
]);
}
}

Powered by Hexo and Hexo-theme-hiker

Copyright © 2017 - 2021 Keep It Simple And Stupid All Rights Reserved.

访客数 : | 访问量 :