本文介绍了使用Laravel的Mailgun驱动程序,您如何(优美地)随电子邮件发送自定义数据和标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用Laravel 5.1并尝试与Mailgun集成.好吧,这很容易,但是现在我正尝试发送自定义变量来自我的应用程序以及我的电子邮件.

So I'm using Laravel 5.1 and trying to integrate with Mailgun. Well, that's easy, but now I'm trying to send custom variables from my application along with my emails.

由于他们的新方向",我实际上正在从Mandrill切换我们的应用程序.有了它们,我可以通过电子邮件标题提供变量和标签,但是对于Mailgun,它仅在通过SMTP发送时才有效.在Laravel中,Mail::send()使用API​​调用,因此从理论上讲,我会在其中添加"v:my-custom-data" => "{"this_id": 123}"的元数据,但我想避免像这样更改核心类.

I'm actually switching our application over from Mandrill because of their "new direction" and such. With them, I could supply the variables and tags via the email headers, but with Mailgun, that only works when you send via SMTP. In Laravel, Mail::send() uses an API call, so in theory I'd add the metadata there with "v:my-custom-data" => "{"this_id": 123}", but I'd like to avoid altering core classes like that.

我还考虑过使用 Bogardo/Mailgun ,但随后我必须替换所有Mailgun::send(),然后我无法在本地发送电子邮件(基于环境的电子邮件驱动程序),然后该应用程序将嫁给" Mailgun.

I also considered using Bogardo/Mailgun, but then I'd have to replace all the Mail::send()s with Mailgun::send(), and then I couldn't send emails locally (environment based email driver), and then the application would be "married" to Mailgun.

有人做过吗?如果我在这里不清楚,请告诉我.

Anyone done this before? Please let me know if I'm not clear here.

推荐答案

我解决了自己的问题.我错了,您可以通过SMTP方法添加自定义变量:

I fixed my own problem. I was wrong, YOU CAN add custom variables via the SMTP method:

// Send email with custom variables and tags in Laravel
Mail::send('emails.blank',
    ['html' => 'This is a test of Mailgun. <strong>How did it work out?</strong>'],
    function($message) {
        $message->to('[email protected]');
        $message->subject('Mailgun API Test');

        $headers = $message->getHeaders();
        $headers->addTextHeader('X-Mailgun-Variables', '{"msg_id": "666", "my_campaign_id": 1313}');
        $headers->addTextHeader('X-Mailgun-Tag', 'test-tag');
    });

我的测试还不够.很高兴知道,但是我认为这是一个未记录的功能,因此建议谨慎使用.

My testing was just inadequate. Very good to know, however I think this is an undocumented feature, so I suggest using with caution.

这篇关于使用Laravel的Mailgun驱动程序,您如何(优美地)随电子邮件发送自定义数据和标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 18:09