【发布时间】:2020-11-26 23:28:20
【问题描述】:
我在使用queue 时遇到了打开邮件的问题。如果我使用带有send() 的邮件,一切正常。
控制器:
Mail::to($order_data->client_email)
->cc([
['email' => $order_data->seller->email],
['email' => auth()->user()->email]
])
->queue(new SendOrderConfirmation($order_data));
可邮寄:
class SendOrderConfirmation extends Mailable
{
use Queueable, SerializesModels;
/**
* Defines a public variable $order_data that we will be using to pass in parameters from our controller.
*/
public $order_data;
/**
* Create a new message instance.
*/
public function __construct($data)
{
// set email data
$this->order_data = $data;
// Set Reply to address
// Basically, the name and email from who's sending this email
$this->replyto(auth()->user()->email, auth()->user()->name);
// Set from
$this->from(auth()->user()->email, auth()->user()->name);
// set email subject
$this->subject('Laminar - Confirmação da Encomenda N.º '.$this->order_data->order_nr);
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('send_emails.Lamimail.SendOrderConfirmation');
}
}
如果我使用 queue() 触发电子邮件,我会收到有关望远镜作业的错误消息:
Trying to get property 'name' of non-object
(View: path\resources\views\send_emails\Lamimail\SendOrderConfirmation.blade.php)
但是,在邮件视图中,名称是一个简单的auth()->user()->name。
有人知道我在队列中失踪了吗?
问候
解决方案,基于@Ersoy 的反馈。注意public $sender_name;
可邮寄:
{
use Queueable, SerializesModels;
/**
* Defines a public variable $order_data that we will be using to pass in parameters from our controller.
*/
public $order_data;
/**
* Will be used to save the sender name
*/
public $sender_name;
/**
* Create a new message instance.
*/
public function __construct($data)
{
// set email data
$this->order_data = $data;
// Set sender name to be used on mail view
$this->sender_name = auth()->user()->name;
// Set Reply to address
// Basically, the name and email from who's sending this email
$this->replyto(auth()->user()->email, auth()->user()->name);
// Set From
$this->from(auth()->user()->email, auth()->user()->name);
// set email subject
$this->subject('subject...');
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('view...');
}
}
【问题讨论】: