Is it possible to get Mail response in Laravel after Mail:to a method called? Where is the best place to do that?
Mail::to($order->email)
->send(new ThankYouMail($order->fresh()));
I am using Sendgrid as a Laravel Mail driver and want to get messageID in order to use for afterward Sendgrid hooks (get email delivery status etc).
public function build()
{
$from = 'info@domain.com';
$subject = 'Thank You for Ordering';
$name = 'Name XYZ';
$order_id = (string)$this->order->id;
$headerData = [
'category' => 'Order',
'unique_args' => [
'OrderID' => $order_id
]
];
$header = $this->asString($headerData);
$this->withSwiftMessage(function ($message) use ($header) {
$message->getHeaders()
->addTextHeader('X-SMTPAPI', $header);
});
return $this->view('mails.thank-you')
->from($from, $name)
->replyTo($from, $name)
->subject($subject);
}
答案 0 :(得分:3)
在发送邮件后分派Illuminate\Mail\Events\MessageSent
[1]
此事件与swift消息实例的实例一起调度。 [2]
您可以收听此事件并获取消息ID。
可以通过在EventServiceProvider
的启动方法中注册事件侦听器来预订此事件。 [3]
protected $listen = [
'Illuminate\Mail\Events\MessageSent' => [
'App\Handlers\Events\MessageIdTranscript',
],
];
MessageIdTranscript
是带有handle
方法的纯PHP类,该方法随事件调用。 Swift_Message
的实例通过名为getId
的公共获取方法公开消息ID。
namespace App\Handlers\Events;
use Illuminate\Mail\Events\MessageSent;
class MessageIdTranscript {
/**
* Handle the event.
*
* @param MessageSent $event
* @return void
*/
public function handle(MessageSent $event)
{
$messageId = $event->message->getId();
// continue handling...
}
}
答案 1 :(得分:0)
Attach a listener to the sendgrid api response not the build.