将标头添加到Laravel 5.4通知系统发送的电子邮件中

时间:2017-04-19 20:13:51

标签: php email laravel-5 notifications swiftmailer

有人知道如何将标题添加到通过Laravel Notification System发送的电子邮件中吗?

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

一旦我使用MailMessageline方法制作了大量电子邮件,我也希望继续使用greetings

任何人都有任何线索?

如果有人需要查看任何内容,我的代码就会出现!

<?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');
    }
}

提前致谢!

3 个答案:

答案 0 :(得分:5)

实际上,我找到了两种方法来附加标题。

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

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

或者在AppServiceProvider register()方法中使用您自己的MailChannel覆盖send(),并在$this->app->bind( \Illuminate\Notifications\Channels\MailChannel::class, MyMailChannel::class ); 方法中附加标题。

SELECT  Transaction_ID, CASE WHEN count(*)>0 THEN `Name` ELSE 'Not Found' END AS Name
FROM    `studentDetails`
WHERE   `Transaction_ID` IN('496018490c1d5d60beb5', 'b6836a07a3c49af6187f')
group by Transaction_ID

答案 1 :(得分:0)

在./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

答案 2 :(得分:0)

黛比V(Debbie V)的回答很接近,但不太正确。她所引用的问题很清楚,但是她错过了解决方案提供的必要上下文。

默认情况下,Laravel中的Notification使用MailMessage,但是您也可以让它返回Mailable。仅在以下情况下:a)创建一个自定义邮件,并且b)使用该邮件代替MailMessage,将应用回调。

一个更完整的解决方案是:

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

在那之后,你应该一切都很好。最困难的部分是调整当前使用的MailMessage以适合Mailable的API。