作为 Laravel 通知,如何在发送的电子邮件中添加标题?

6

有人知道如何在通过 Laravel通知系统发送的电子邮件中添加标题吗?

我不是在谈论可邮寄类,我可以通过withSwiftMessage()方法设置标题!

我也想保持使用MailMessage,一旦我使用linegreetings方法构建了大量的电子邮件!

有人有任何线索吗?

以下是我的代码,以防需要查看任何内容!

<?php

namespace PumpMyLead\Notifications\Tenants\Auth;

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

class AccountActivation extends Notification
{
    use Queueable;

    /**
     * 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)
    {
        return (new MailMessage)
            ->subject('My email subject')
            ->greeting('Just a greeting')
            ->line('Line 1')
            ->line('Line 2')
            ->action('CTA wanted', 'http://www.pumpmylead.com')
            ->line('Byebye');
    }
}

提前感谢您!

4个回答

9

实际上,我已经找到了2种添加标题的方法。

当通过邮件通道发送通知时,会触发Illuminate\Mail\Events\MessageSending事件。

附加一个侦听器。在handle()中,您将获得Swift_Message对象。

或者在AppServiceProviderregister()方法中重写MailChannel并在send()方法中添加标题。

$this->app->bind(
    \Illuminate\Notifications\Channels\MailChannel::class,
    MyMailChannel::class
);

2
谢谢,事件监听器似乎是添加标题并继续使用简单通知的最简单方法。 - chifliiiii

2
在./app/Notifications/myNotification.php文件中,在你的__construct()函数中添加以下代码:
$this->callbacks[]=( function($message){
    $message->getHeaders()->addTextHeader('x-mailgun-native-send', 'true');
});

将“x-mailgun-native-send”替换为任何您希望添加的标题, 并将“true”替换为所需值。

查看https://github.com/laravel/ideas/issues/475


这在5.7上似乎不起作用... 回调函数不会被调用。 - Spekkie
因为callbacksMailable的属性,而不是Notification的属性。 - Stalinko
链接的 Laravel 想法问题已经合并到 Laravel 5.8,而不是 5.7。 - cweiske

2
如果上述方法无效,这里有一个现代的2022年替代方案,在Laravel 8中测试过。
使用withSwiftMessage()
public function toMail($notifiable)
{
    return (new MailMessage)
        ->subject($subject)
        ->greeting($greeting)
        ->line($line1)
        ->line($line2)
        ->withSwiftMessage(function($message) use ($value1, $value2) {
            $message->getHeaders()->addTextHeader('X-MJ-CustomID', json_encode([
                'key1' => $value1,
                'key2' => $value2,
            ]));
        })
}

0

Debbie V的答案非常接近但不完全正确。她提到的问题很清楚,但她错过了解决方案所需的上下文。

Laravel中默认使用MailMessage发送通知,但您也可以返回Mailable。只有在您:a)创建自定义可邮寄项,并且b)使用该可邮寄项而不是MailMessage时,才会应用回调。

更完整的解决方案如下:

  1. 创建自定义可邮寄类php artisan make:mail MyMailable
  2. 更新您的public function toMail($notifiable)方法以利用新的Mailable
  3. 将回调添加到MyMailable类的构造函数中。

之后您就可以放心使用了。最难的部分只是将您当前使用的MailMessage适应Mailable的API。


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