问题描述
在我的Node.js应用程序中,我遵循了Mailgun文档 https://documentation.mailgun.com/en/latest/quickstart-sending.html#send-with-smtp-or-api 发送类似于以下内容的电子邮件:
In my Node.js application, I followed the Mailgun docs https://documentation.mailgun.com/en/latest/quickstart-sending.html#send-with-smtp-or-api for sending an email similar to the following:
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文档没有提供任何示例来说明如何检索该数据.
The issue I am faced with is how can I access that same Mailgun response when I send an email with Laravel? The Mailgun docs don't provide any examples showing how to retrieve that data.
这就是我通过Laravel发送电子邮件的方式:
This is how I am sending emails with 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
如果有人知道任何解决方案,将不胜感激!
If anyone knows of any solution it would be greatly appreciated!
推荐答案
您好,欢迎光临!
Laravel有两个电子邮件事件,如官方文档所述: MessageSending
和 MessageSent
Laravel has two events for the emails as explained in the official documentation: MessageSending
and MessageSent
您可以按照事件官方文档进行操作,以收听这些特定事件:
You can follow the events official documentation in order to listen for these specific events:
/**
* 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.让我们看一下MailgunTransport@send
源代码,以了解幕后发生的事情:
You will receive as input the Swift_message
which contains a header that is the Mailgun ID you're looking for. Let's have a look at the MailgunTransport@send
source code in order to understand what's going on behind the scenes:
/**
* {@inheritdoc}
*/
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
{
// [...]
$response = $this->client->request(
'POST',
"https://{$this->endpoint}/v3/{$this->domain}/messages.mime",
$this->payload($message, $to)
);
$message->getHeaders()->addTextHeader(
'X-Mailgun-Message-ID', $this->getMessageId($response) // <-- HERE LARAVEL SETS THE MESSAGE ID
);
// [...]
}
在您的侦听器中寻找相同的密钥,您可以恢复mailgun分配给您的电子邮件的消息ID.不幸的是,您无法访问整个Mailgun响应,但是使用API可以轻松检索邮件.
Looking for the same key in your listener you can recover the message ID that mailgun assigned to your e-mail. Unfortunately you can't access the entire Mailgun response but with the API you can easily retrieve your message.
这篇关于如何在Laravel中检索Mailgun传递的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!