Laravel5.7 自定义 『密码重置』 『 邮箱验证』 邮件
2018-10-09 本文已影响15人
3275508ab630
#方法一
为了快速看到效果,我们可以先把 config
文件夹下的 mail.php
第一个键值改成 log
。
'driver' => 'log'
再路由改一下,直接访问首页,就能把邮件发送到 storage\logslaravel.log
日志里,
Route::get('/', function () {
$user = new \App\User();
$user->id = 1;
$user->email = 'asdasd@qq.com';
$user->sendPasswordResetNotification('123'); //发送重置密码邮件
$user->sendEmailVerificationNotification(); //发送邮箱认证邮件
dd('发送成功' . now());
});
通过本地化 json
文件,把原来的英语翻译成中文。
在 resources\lang
文件夹下面创建 en.json
,文件里写上以下内容
{
"Hello!" : "您好!",
"Whoops!" : "哦嚯!",
"Reset Password Notification" : "重置密码通知",
"You are receiving this email because we received a password reset request for your account." : "您收到此电子邮件是因为我们收到了您帐户的密码重置请求。",
"Reset Password":"重设密码",
"If you did not request a password reset, no further action is required.":"如果您未请求重置密码,则无需操作。",
"Verify Email Address" : "验证邮箱",
"Please click the button below to verify your email address." : "请点击下面的按钮验证您的邮箱。",
"If you did not create an account, no further action is required." : "如果您未创建帐户,则无需操作。",
"Regards" : "在此送上真诚的问候",
"If you’re having trouble clicking the \":actionText\" button, copy and paste the URL below\ninto your web browser: ": "如果您在点击 \":actionText\" 按钮时遇到问题,请将下面链接复制并粘贴到网络浏览器进行访问:",
"All rights reserved." : "版权所有。"
}
这样邮件里的英文就全部被替换了,邮件中还有一个 Laravel
这个单词是修改 config\app.php
中的 name
这个键的值。
#方法二
通过 重写 发送邮件的方法来修改。在 User.php
这个 model
文件中写上以下方法
//重写『密码重置』邮件 方法
public function sendPasswordResetNotification($token)
{
\Illuminate\Auth\Notifications\ResetPassword::toMailUsing(function ($notifiable, $token) {
return (new \Illuminate\Notifications\Messages\MailMessage)
->subject('重置密码通知')
->line('您收到此电子邮件是因为我们收到了您帐户的密码重置请求。')
->action('重设密码', url(config('app.url').route('password.reset', $token, false)))
->line('如果您未请求重置密码,则无需操作。');
});
$this->notify(new \Illuminate\Auth\Notifications\ResetPassword($token));
}
//重写『 邮箱验证』邮件 方法
public function sendEmailVerificationNotification()
{
\Illuminate\Auth\Notifications\VerifyEmail::toMailUsing(function ($notifiable) {
return (new \Illuminate\Notifications\Messages\MailMessage)
->subject('验证邮箱')
->line('请点击下面的按钮验证您的邮箱。')
->action(
'验证邮箱',
\Illuminate\Support\Facades\URL::temporarySignedRoute(
'verification.verify', now()->addMinutes(60), ['id' => $notifiable->getKey()]
)
)
->line('如果您未创建帐户,则无需操作。');
});
$this->notify(new \Illuminate\Auth\Notifications\VerifyEmail);
}
解释一下,MailMessage
是 Laravel
内置的一个快速生成邮件的类。里面有一些内置的方法让我们快速生成格式相同的邮件。方法如下
->subject('邮件主题')
->line('插入一段文字') //可以调用多次来插入多段文字,在 action 方法之前调用则文字显示位置在按钮的上方,在 action 方法之后调用则文字显示在按钮下方
->action('邮件里的按钮标题', '按钮跳转网页链接')
->level('邮件等级') //只能填 info, success, error,用来修改按钮颜色 info 蓝色,success 绿色,error 红色。
->greeting('邮件开头问候语') //如果没有 则问候语根据邮件等级 error 默认是 Whoops!,其他是 Hello!(参考上面的 json 的键)
->salutation('所属站点') //如果没有 则默认显示 Regards,Laravel(参考上面的 json 的键)
还有剩余的一些英文单词通过以下步骤修改
执行
php artisan vendor:publish --tag=laravel-mail
php artisan vendor:publish --tag=laravel-notifications
这样会在 resources\views\vendor
下生成 MailMessage
对应的模板文件,搜索这个文件夹下 @lang
关键字就能找到剩余的英文进行修改。有了模板文件就可以修改更多内容,css
在 resources\views\vendor\mail\html\themes\default.css
。
#方法三
完全不使用 laravel
自带的模板,使用自己做的模板。依旧是在 User.php
这个 model
文件中重写方法。
『密码重置』邮件
public function sendPasswordResetNotification($token)
{
\Illuminate\Auth\Notifications\ResetPassword::toMailUsing(function ($notifiable, $token) {
return (new \Illuminate\Notifications\Messages\MailMessage)->view(
'xxx', ['key' => 'value']
); // view 方法就是平常的用法,传入自己做的模板做参数
});
$this->notify(new \Illuminate\Auth\Notifications\ResetPassword($token));
}
『 邮箱验证』邮件同上