如何解决在laravel通知上调用未定义的方法Illuminate \ Notifications \ Messages \ MailMessage :: bcc()?

时间:2017-10-18 03:04:26

标签: laravel email laravel-5 notifications laravel-5.3

我使用Laravel 5.3

我尝试cc,它有效

但我尝试使用bcc,存在这样的错误:

Call to undefined method Illuminate\Notifications\Messages\MailMessage::bcc()

我的代码是这样的:

<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class ConfirmOrder extends Notification implements ShouldQueue, ShouldBroadcast
{
    use Queueable;
    private $data;
    public function __construct($data)
    {
        $this->data = $data;
    }
    public function via($notifiable)
    {
        return ['mail'];
    }
    public function toMail($notifiable)
    {
        $mail_myshop = explode(',',config('app.mail_myshop'));
        return (new MailMessage)
                    ->bcc($mail_myshop)
                    ->subject('Thanks')
                    ->greeting('Hi '.$notifiable->name.',')
                    ->line('....')
                    ->line('...');
    }
}

似乎Laravel 5.3不支持bcc

如何解决错误?

更新

我找到了解决方案

在我的控制器中这样:

Mail::to(auth()->user())->send(new ConfirmOrder($data, auth()->user()));

在我这样的邮件中:

<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class ConfirmPaymentMail extends Mailable
{
    use Queueable, SerializesModels;
    public $data;
    public $user;
    public function __construct($data, $user)
    {
        $this->data = $data;
        $this->user = $user;
    }
    public function build()
    {
        $mail_myshop = explode(',',config('app.mail_myshop'));
        return $this->view('vendor.notifications.mail.email-confirm-order',['data'=>$this->data, 'name' => $this->user->name])
                    ->bcc($mail_myshop)
                    ->subject('Test');
    }
}

它有效

1 个答案:

答案 0 :(得分:2)

正如您所见,MailMessage对象在Laravel 5.3中没有bcc()方法。它是在5.4中添加的。

但是,在Laravel 5.3中,当您向一组收件人发送MailMessage时,它会在后台自动使用密件抄送。因此,您可以创建一个包含原始地址以及bcc人员列表的数组,然后使用to()方法,每个人都将成为bcc&#39; d。然而,这将使&#34;离开&#34;地址为空。

// get the bcc list
$mail_myshop = explode(',',config('app.mail_myshop'));

// add the original recipient
array_unshift($mail_myshop, $notifiable->routeNotificationFor('mail'));

return (new MailMessage)
    ->to($mail_myshop) // if $mail_myshop is an array, will auto convert to bcc in 5.3
    ->...

最好在此处做一个说明,但是,如果您确实将应用程序升级到Laravel 5.4+,则不再是这种情况,并且所有收件人都将添加到to()字段中。

最后一个解决方案是创建一个Mailable对象。可以从Mailable方法而不是toMail()方法返回此MailMessage对象。 Mailable对象支持bcc(),当您从Mailable函数返回toMail()对象时,它只会调用send()方法。

使用更新中提供的代码,您的toMail()方法如下所示:

public function toMail($notifiable)
{
    $data = /*whatever data is*/;
    $mail_myshop = explode(',', config('app.mail_myshop'));
    return (new ConfirmPaymentMail($data, $notifiable))
        ->to($notifiable->routeNotificationFor('mail'))
        ->bcc($mail_myshop)
        ->subject('Thanks');
}