自定义电子邮件验证通知

时间:2019-06-21 18:44:59

标签: laravel

我有一个项目要求,我们希望从SendGrid发送电子邮件验证消息。为此,我已覆盖了 App \ User

中的 sendEmailVerificationNotification
public function sendEmailVerificationNotification()
    {
        $this->notify(new \App\Notifications\SendGridEmailNotifications);
    }

并生成了一个通知类:

namespace App\Notifications;

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

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Config;
use Log;

class SendGridEmailNotifications extends Notification
{
    use Queueable;

    public static $toMailCallback;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * 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)
    {


        /* Send via SendGrid. */

        //Array of variables for SendGrid.
        $payload = array();

        $this->callSendGridAPI('mail/send', 'POST', $payload);

        /*
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');

        */

        return true;
    }

    /**
     * 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()]
        );
    }

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

我的问题是,该过程假定返回了MailMessage类型的返回,以便稍后通过MailChannel发送。但是,如果我删除了“ via”(例如)

public function via($notifiable)
    {
        return;
    }

然后该类根本不会运行,并且什么也没有发生。如果我将“邮件”保留在原位,则会在MailChannel中遇到错误,因为显然这里没有返回MailMessage。

简而言之,如何配置此通知以简单地单击SendGrid API然后退出?我可以定义一个“空”或死胡同的通道吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

作为一种可能的解决方案,您可以创建自己的频道:

namespace App\Channels;

use Illuminate\Notifications\Notification;

class SendGridChannel
{
    /**
     * Send the given notification.
     *
     * @param  mixed  $notifiable
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return void
     */
    public function send($notifiable, Notification $notification)
    {
        $message = $notification->toSendGrid($notifiable);

    }
}

并这样称呼:

use App\Channels\SendGridChannel;

class SendGridEmailNotifications extends Notification
{

 public function via($notifiable)
    {
        return [SendGridChannel::class];
    }

  public function toSendGrid($notifiable)
    {
      //As in opening question
    }

}
相关问题