在可邮寄类中使用 MailMessage 的 Laravel

10

当用户注册时,我想向他们发送电子邮件,我创建了一个可发送的类:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class WelcomeMail extends Mailable
{
use Queueable, SerializesModels;

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

    $this->message = (new MailMessage)
        ->greeting('Bonjour '.$user->name)
        ->line('Nous vous remercions de votre inscription.')
        ->line('Pour rappel voici vos informations :')
        ->line('Mail: '.$user->email)
        ->line('Password: '.$user->password);
}

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->view('message')->with([
        'slot' => $this->message
    ]);


}

但是,如果我使用自定义模板,则必须处理所有的CSS和Html。我看到忘记密码电子邮件使用MailMessage,并且所有模板都已使用MailMessage模板进行制作。

是否有任何方法可以使用相同的模板或直接创建具有自定义内容的MailMessage模板?

谢谢。


我认为你不应该以那种方式扩展Mailable。为什么不创建一个适当的通知,比如“php artisan make:notification WelcomeNotification”-- https://laravel.com/docs/5.6/notifications#mail-notifications - jannej
4个回答

11

我在 Laravel 8 中使用过这个,不确定它是否与 Laravel 的旧版本兼容。 重点是在 Mailable 中使用 html 方法。

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Queue\SerializesModels;

class MyMail extends Mailable
{
    use Queueable, SerializesModels;

    public function build()
    {
        return $this->subject('My Subject')
            ->html((new MailMessage)
                ->line('The introduction to the notification.')
                ->action('Notification Action', url('/'))
                ->line('Thank you for using our application!')
                ->render()
            );
    }
}


1
我需要在邮件消息上调用 render() 方法才能使其正常工作,否则 Laravel 会抛出错误。(new MailMessage)->line(...)->render(),但是你的答案让我朝着正确的方向前进。谢谢。 - Arun A S
这是实现的方法,我需要一种使用Laravel内置通知电子邮件模板以保持一致性的方式,他们应该将其作为独立引擎生成电子邮件,并最终附加到通知中。 - manu144x

10

从供应商发布通知,您可以使用Markdown发送它

    public function __construct($user)
    {
        $this->user = $user;

        $this->message = (new MailMessage)
            ->greeting('Bonjour '.$user->name)
            ->line('Nous vous remercions de votre inscription.')
            ->line('Pour rappel voici vos informations :')
            ->line('Mail: '.$user->email)
            ->line('Password: '.$user->password);
    }

    /**
    * Build the message.
    *
    * @return $this    
    */
    public function build()
    {
         return  $this->markdown('vendor.notifications.email', $this->message->data());
    }

1
不发布的话,可以返回 $this->markdown('notifications::email', $this->message->data()); 这是一个很好的提示。 - Sergiu

9

您混淆了两个不同的Laravel概念,通知和邮件发送器。通知可以是邮件发送器,但邮件发送器不能成为通知。

MailMessage类是一条通知消息,但不能作为Mailable的消息。如果要发送一个MailMessage邮件发送器,您应该扩展Notification类:

<?php

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

class WelcomeNotification extends Notification implements ShouldQueue
{
    use Queueable, SerializesModels;

    public $user;

    public function __construct($user)
    {
        // The $notifiable is already a User instance so not really necessary to pass it here
        $this->user = $user;
    }

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {

        return (new MailMessage)
            ->greeting('Bonjour '.$this->user->name)
            ->line('Nous vous remercions de votre inscription.')
            ->line('Pour rappel voici vos informations :')
            ->line('Mail: '.$this->user->email)
            ->line('Password: '.$this->user->password);
    }

}

此外,可以参考Laravel框架中的ResetPassword通知作为示例。

要向用户发送通知:

$user->notify(new WelcomeNotification($user));

以此方式,您可以使用默认的邮件通知模板创建通用的邮件消息。

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接