php - Laravel 通知 : Mail (Mailgun) Response on NotificationSent event

标签 php laravel laravel-5 mailgun laravel-notification

我在 Laravel 应用程序中使用 Mailgun 作为邮件驱动程序,并使用 nexmo 来发送短信。

我想要实现的是维护通过 Mailgun 或 Nexmo 发送的通知的传递状态。在 Nexmo 的情况下,我能够实现这一点,因为我在处理通知后触发的 NotificationSent 事件中获取了 nexmo MessageId。

但是,在电子邮件的事件实例中,响应为空。

知道我缺少什么,或者如何检索 mailgun 消息 ID?

最佳答案

我已经找到了目前可以完成这项工作的解决方法。不像我想要的那么整洁,但发布以供将来引用,以防有人需要。

我创建了一个扩展 Illuminate\Notifications\Channels\MailChannel 的自定义通知 channel

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();
        });
    }
}

我仍在寻找通过NotificationSent事件实现此目的的解决方案。

关于php - Laravel 通知 : Mail (Mailgun) Response on NotificationSent event,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41896368/

相关文章:

php - 在一列中存储另一个表中的多个 ID?

javascript - jQuery 验证有效,但如果条目不正确,仍然能够提交表单

php - 修改 Laravel 中的用户表

php - Laravel 错误 - stdClass 类的对象无法转换为字符串

javascript - 如何从 cytoscape.js 中的 mysql 数据库中检索节点和元素?

php - 使用 Laravel 进行正确的测试流程?

php - Laravel @can Blade 策略检查

php - Laravel Eloquent Sum of relationship 的列

php - 在 Guzzle 中复制 cURL 帖子

php - Javascript 表单验证不起作用?