php 在 Laravel 5.4 中自定义忘记密码的电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42581847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Customize Forgotten Password Email in Laravel 5.4
提问by prgrm
I am trying to customize password reset email in Laravel.
我正在尝试在 Laravel 中自定义密码重置电子邮件。
I have to override this function:
我必须覆盖这个功能:
namespace Illuminate\Auth\Passwords;
use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification;
use Illuminate\Http\Request;
trait CanResetPassword
{
/**
* Get the e-mail address where password reset links are sent.
*
* @return string
*/
public function getEmailForPasswordReset()
{
return $this->email;
}
/**
* Send the password reset notification.
*
* @param string $token
* @return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}
This is my attempt:
这是我的尝试:
public function sendPasswordResetNotification($token, Requests $request)
{
Mail::to($request->email)->send(new newpassword($token));
}
I get this error:
我收到此错误:
Declaration of Illuminate\Foundation\Auth\User::sendPasswordResetNotification($token, Illuminate\Http\Request $request) must be compatible with Illuminate\Contracts\Auth\CanResetPassword::sendPasswordResetNotification($token)
Illuminate\Foundation\Auth\User::sendPasswordResetNotification($token, Illuminate\Http\Request $request) 的声明必须兼容Illuminate\Contracts\Auth\CanResetPassword::sendPasswordResetNotification($token)
回答by user1669496
If you read the error, it's telling you your class is not compatible with CanResetPassword
. If you look at that....
如果您阅读错误,则表明您的课程与CanResetPassword
. 如果你看那个......
interface CanResetPassword
{
/**
* Get the e-mail address where password reset links are sent.
*
* @return string
*/
public function getEmailForPasswordReset();
/**
* Send the password reset notification.
*
* @param string $token
* @return void
*/
public function sendPasswordResetNotification($token);
}
You can see the function sendPasswordResetNotification
should only take one parameter, $token
. So you need to remove Request $request
as a parameter from the method's signature.
你可以看到这个函数sendPasswordResetNotification
应该只接受一个参数,$token
。所以你需要Request $request
从方法的签名中删除作为参数。
In order to get the request, you will want to use the function request()
inside the sendPasswordResetNotification
method.
为了获得请求,您需要使用方法request()
内部的函数sendPasswordResetNotification
。
public function sendPasswordResetNotification($token)
{
Mail::to(request()->email)->send(new newpassword($token));
}
回答by Ligemer
I'm surprised you're going to that length to customize the email.
我很惊讶你会用这么长的时间来定制电子邮件。
Try this instead:
试试这个:
php artisan vendor:publish
Then modify the file here
然后在这里修改文件
/resources/views/vendor/notifications/email.blade.php
Works great for our usage.
非常适合我们的使用。
user@default:~/laravel_5.4$ php artisan vendor:publish
Copied Directory [/vendor/laravel/framework/src/Illuminate/Pagination/resources/views] To [/resources/views/vendor/pagination]
Copied Directory [/vendor/laravel/framework/src/Illuminate/Notifications/resources/views] To [/resources/views/vendor/notifications]
Copied Directory [/vendor/laravel/framework/src/Illuminate/Mail/resources/views] To [/resources/views/vendor/mail]
Publishing complete.
Now if you NEED to change the copy and you want the fancy button that the original ResetPassword class uses, you can extend the mail class in your User.php class like the following example.
现在,如果您需要更改副本并且想要原始 ResetPassword 类使用的精美按钮,您可以像以下示例一样扩展 User.php 类中的邮件类。
Here's a copy of ours that works great AS AN EXAMPLE ONLY:
这是我们的副本,仅作为示例非常有效:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Notifications\Messages\MailMessage;
class User extends Authenticatable
{
use Notifiable;
protected $table = 'Users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'firstName',
'lastName',
'email',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* Sends the password reset notification.
*
* @param string $token
*
* @return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new CustomPassword($token));
}
}
class CustomPassword extends ResetPassword
{
public function toMail($notifiable)
{
return (new MailMessage)
->line('We are sending this email because we recieved a forgot password request.')
->action('Reset Password', url(config('app.url') . route('password.reset', $this->token, false)))
->line('If you did not request a password reset, no further action is required. Please contact us if you did not submit this request.');
}
}