在我的Node.js应用程序中,我遵循Mailgun文档https://documentation.mailgun.com/en/latest/quickstart-sending.html#send-with-smtp-or-api发送类似于以下内容的电子邮件:
mailgun.messages().send(data, (error, body) => {
console.log(body);
// body is {id: some_mailgun_built_id, message: 'Queued. Thank You'}
// which I store the body.id in my database
});
我面临的问题是,在使用Laravel发送电子邮件时,如何访问相同的Mailgun响应? Mailgun文档未提供任何示例来说明如何检索该数据。
这是我通过Laravel发送电子邮件的方式:
\Mail::to($recipients)->send(
// this just renders my blade file which formats my email
new SendEmail($email);
);
// ?? How to get Message was sent object here
如果有人知道任何解决方案,将不胜感激!
您好,欢迎光临!
Laravel有两个电子邮件事件,如official documentation中所述:MessageSending
和MessageSending
您可以按照MessageSent
来收听这些特定事件:
MessageSent
您将收到events official documentation作为输入,其中包含一个标题,该标题是您要查找的Mailgun ID。让我们看一下/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'Illuminate\Mail\Events\MessageSending' => [
'My\Email\Listener',
],
'Illuminate\Mail\Events\MessageSent' => [
'My\Other\Listener',
],
];
源代码,以了解幕后情况:
Swift_message
在侦听器中寻找相同的密钥,您可以恢复mailgun分配给您电子邮件的消息ID。不幸的是,您无法访问整个Mailgun响应,但是使用API可以轻松地检索您的消息。