Laravel 自定义用户验证邮件问题

Laravel custom user verification email issue

在我的 laravel 应用程序中,我正在尝试创建自定义的用户验证电子邮件模板。

首先,我在 App 中创建了一个名为 Notifications 的文件夹,在我的 Notifications 文件夹中,我有一个名为 CustomVerifyEmailNotification.php

的文件
app/Notifications/CustomVerifyEmailNotification.php

下面是我在CustomVerifyEmailNotification.php

里面的代码
<?php

namespace Illuminate\Auth\Notifications;

use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\URL;

class CustomVerifyEmailNotification extends Notification
{
    /**
     * The callback that should be used to build the mail message.
     *
     * @var \Closure|null
     */
    public static $toMailCallback;

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

    /**
     * Build the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        $verificationUrl = $this->verificationUrl($notifiable);

        if (static::$toMailCallback) {
            return call_user_func(static::$toMailCallback, $notifiable, $verificationUrl);
        }

        return (new MailMessage)
            ->subject(Lang::get(''.('sentence.Verify Email Address').''))
            ->line(Lang::get(''.('sentence.If you did not create an account, no further action is required.').''));
    }

    /**
     * Get the verification URL for the given notifiable.
     *
     * @param  mixed  $notifiable
     * @return string
     */
    protected function verificationUrl($notifiable)
    {
        return URL::temporarySignedRoute(
            'verification.verify',
            Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
            [
                'id' => $notifiable->getKey(),
                'hash' => sha1($notifiable->getEmailForVerification()),
            ]
        );
    }

    /**
     * Set a callback that should be used when building the notification mail message.
     *
     * @param  \Closure  $callback
     * @return void
     */
    public static function toMailUsing($callback)
    {
        static::$toMailCallback = $callback;
    }
}

然后我根据下面的代码

改变了我的User.php
<?php

namespace App;

use App/Notifications/CustomVerifyEmailNotification;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Cashier\Billable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable,Billable;
    use HasRoles;

     public function sendEmailVerificationNotification() 
    {
        $this->notify(new CustomVerifyEmailNotification);
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */

    protected $fillable = [
        'name','last_name', 'email', 'password','username','mobile','propic','user_roles','user_source',
    ];

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

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

但是,

use App/Notifications/CustomVerifyEmailNotification;

向我显示一条错误消息 syntax error, unexpected '/', expecing ';' or ','

因此我在这里也遇到了错误

public function sendEmailVerificationNotification() 
    {
        $this->notify(new CustomVerifyEmailNotification);
    }

错误是,undefined type 'App\CustomVerifyEmailNotification'

如何解决此问题并创建此自定义验证电子邮件

我正在使用 laravel 6

命名空间应该是

namespace App\Notifications

而不是

namespace Illuminate\Auth\Notifications;

并且在 user.php 而不是

use App/Notifications/CustomVerifyEmailNotification;

你应该使用

use App\Notifications\CustomVerifyEmailNotification;