laravel 傳送郵件修改密碼

listenon發表於2018-07-26

1、執行命令 php artisan make:notification RestPassword

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class ResetPassword extends Notification
{
    use Queueable;
    var $token;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($token)
    {
        $this->token = $token;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('密碼重置')
            ->line('我們收到你的郵件是因為你發了一個重置密碼的請求.')
            ->action('點選重置密碼', url('/password/reset',$this->token))
            ->line('謝謝你的使用!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}
複製程式碼

2、修改 App\User:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

use App\Notifications\ResetPassword;


class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','sex','mobile','uid','provider','avatar',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * 傳送密碼重置通知.
     *
     * @param  string $token
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPassword($token));
    }
}
複製程式碼

3、配置env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.qq.com
MAIL_PORT=465
MAIL_USERNAME=郵箱地址
MAIL_PASSWORD=郵箱密碼
MAIL_ENCRYPTION=ssl
MAIL_FROM_NAME=發件人複製程式碼

4、自定義郵件內容

laravel 傳送郵件修改密碼

@isset($actionText)
    @component('mail::subcopy')
        @lang(
            "如果你無法點選此按鈕 \":actionText\" , 請複製此連結從瀏覽器中開啟: [:actionURL](:actionURL)",
            [
                'actionText' => $actionText,
                'actionURL' => $actionUrl
            ]
        )
    @endcomponent
@endisset
@endcomponent複製程式碼



相關文章