Php Laravel通知:NotificationSent事件的邮件(Mailgun)响应
我正在使用Mailgun作为我的laravel应用程序中的邮件驱动程序,以及用于SMS目的的nexmo 我试图实现的是维护通过Mailgun或Nexmo发送的通知的传递状态。在使用Nexmo的情况下,我能够实现这一点,因为我在处理通知后激发的NotificationSent事件中获取Nexmo MessageId 但是,在电子邮件的事件实例中,响应为空 你知道我遗漏了什么,或者我如何检索mailgun消息id吗?当查看代码()时,它将执行以下操作Php Laravel通知:NotificationSent事件的邮件(Mailgun)响应,php,laravel,laravel-5,mailgun,laravel-notification,Php,Laravel,Laravel 5,Mailgun,Laravel Notification,我正在使用Mailgun作为我的laravel应用程序中的邮件驱动程序,以及用于SMS目的的nexmo 我试图实现的是维护通过Mailgun或Nexmo发送的通知的传递状态。在使用Nexmo的情况下,我能够实现这一点,因为我在处理通知后激发的NotificationSent事件中获取Nexmo MessageId 但是,在电子邮件的事件实例中,响应为空 你知道我遗漏了什么,或者我如何检索mailgun消息id吗?当查看代码()时,它将执行以下操作 $this->client->
$this->client->post($this->url, $this->payload($message, $to));
$this->sendPerformed($message);
return $this->numberOfRecipients($message);
因为Laravel合同要求实现发回发送的电子邮件数量
即使您能够进入邮件传输,它也不会存储来自的响应,因此无法捕获邮件id
您可以做的是实现您自己的(或查看PackageGist)以适应邮件客户端,但这不是一个完美的解决方案,需要一些丑陋的
实例进行检查 我找到了一个暂时解决问题的方法。虽然没有我想要的那么整洁,但是如果有人需要的话,我会把它贴出来以备将来参考
我已经创建了一个自定义通知通道,扩展了illumb\Notifications\Channels\MailChannel
class EmailChannel extends MailChannel
{
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return void
*/
public function send($notifiable, Notification $notification)
{
if (! $notifiable->routeNotificationFor('mail')) {
return;
}
$message = $notification->toMail($notifiable);
if ($message instanceof Mailable) {
return $message->send($this->mailer);
}
$this->mailer->send($message->view, $message->data(), function ($m) use ($notifiable, $notification, $message) {
$recipients = empty($message->to) ? $notifiable->routeNotificationFor('mail') : $message->to;
if (! empty($message->from)) {
$m->from($message->from[0], isset($message->from[1]) ? $message->from[1] : null);
}
if (is_array($recipients)) {
$m->bcc($recipients);
} else {
$m->to($recipients);
}
if ($message->cc) {
$m->cc($message->cc);
}
if (! empty($message->replyTo)) {
$m->replyTo($message->replyTo[0], isset($message->replyTo[1]) ? $message->replyTo[1] : null);
}
$m->subject($message->subject ?: Str::title(
Str::snake(class_basename($notification), ' ')
));
foreach ($message->attachments as $attachment) {
$m->attach($attachment['file'], $attachment['options']);
}
foreach ($message->rawAttachments as $attachment) {
$m->attachData($attachment['data'], $attachment['name'], $attachment['options']);
}
if (! is_null($message->priority)) {
$m->setPriority($message->priority);
}
$message = $notification->getMessage(); // I have this method in my notification class which returns an eloquent model
$message->email_id = $m->getSwiftMessage()->getId();
$message->save();
});
}
}
我仍然在寻找一个解决方案,通过NotificationSend事件来实现这一点。谢谢@ken,我实际上通过扩展邮件通道来解决这个问题。稍后会将其作为答案发布。