php 如何使用mailgun发送HTML电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21786128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send out HTML email with mailgun?
提问by deepcell
after failed to find a solution for my problem in the mailgun documentation, I will explain what I'm looking for.
在 mailgun 文档中找不到我的问题的解决方案后,我将解释我在寻找什么。
Today I'm using phpList to send out my newsletter (It works perfect!), I have HTML pages that I just include in the phpList application to send it out. (I'm using SMTP method to send news out). I wonder if I can do the same with mailgun (for sure can, but how?), is it possible to just include the path of my HTML pages to send it out? (I do not have interest to type my html code in the script, it must be in the path otherwise I have no interest in use mailgun).
今天我使用 phpList 来发送我的时事通讯(它工作得很好!),我有 HTML 页面,我只是包含在 phpList 应用程序中来发送它。(我使用 SMTP 方法发送消息)。我想知道我是否可以用 mailgun 做同样的事情(当然可以,但是怎么做?),是否可以只包含我的 HTML 页面的路径来发送它?(我没有兴趣在脚本中输入我的 html 代码,它必须在路径中,否则我对使用 mailgun 没有兴趣)。
Take a look to my mailgun php code as follow:
看看我的mailgun php代码如下:
$result = $mgClient->sendMessage("$domain",
array('from' => 'My Business Name <[email protected]>',
'to' => '[email protected], [email protected], [email protected]',
'subject' => 'Issue Feb 2014',
'text' => 'Your mail do not support HTML',
'html' => '<html>Inline image: <img src="cid:Pad-Thai-1.jpg"></html>',
'recipient-variables' => '{"[email protected]": {"first":"Name-1", "id":1}, "[email protected]": {"first":"Name-2", "id": 2}}'),
array('inline' => 'Pad-Thai-1.jpg'));
I have the array element named 'html', I would like to include the path of my HTML page (if not possible, where can I put it?). I just can not include my whole HTML code in this html array element, cause it is so extensive.
我有一个名为 的数组元素'html',我想包括我的 HTML 页面的路径(如果不可能,我可以把它放在哪里?)。我只是不能在这个 html 数组元素中包含我的整个 HTML 代码,因为它是如此广泛。
But mailgun claim to be easy and great, that is the motive I want to change.
但是mailgun自称简单又好用,这就是我想要改变的动机。
回答by israr
I have used external html template in this way. It may be help you.
我以这种方式使用了外部 html 模板。它可能会帮助你。
$html = file_get_contents('my_template.html'); // this will retrieve the html document
and then:
进而:
$result = $mgClient->sendMessage("$domain",
array('from' => 'My Business Name <[email protected]>',
'to' => '[email protected], [email protected], [email protected]',
'subject' => 'Issue Feb 2014',
'text' => 'Your mail do not support HTML',
'html' => $html,
'recipient-variables' => '{"[email protected]": {"first":"Name-1", "id":1}, "[email protected]": {"first":"Name-2", "id": 2}}'),
array('inline' => 'Pad-Thai-1.jpg'));
Check this line:
检查这一行:
'html' => $html,
回答by Robin Dowling
Adding a link to Mailgun documentation. This helped me while constructing HTML and MIME messages. https://documentation.mailgun.com/api-sending.html#examples
添加指向 Mailgun 文档的链接。这在构建 HTML 和 MIME 消息时帮助了我。https://documentation.mailgun.com/api-sending.html#examples
As per documentation:
根据文档:
# Include the Autoloader (see "Libraries" for install instructions)
require 'vendor/autoload.php';
use Mailgun\Mailgun;
# Instantiate the client.
$mgClient = new Mailgun('YOUR_API_KEY');
$domain = "YOUR_DOMAIN_NAME";
# Make the call to the client.
$result = $mgClient->sendMessage($domain, array(
'from' => 'Excited User <YOU@YOUR_DOMAIN_NAME>',
'to' => '[email protected]',
'cc' => '[email protected]',
'bcc' => '[email protected]',
'subject' => 'Hello',
'text' => 'Testing some Mailgun awesomness!',
'html' => '<html>HTML version of the body</html>'
), array(
'attachment' => array('/path/to/file.txt', '/path/to/file.txt')
));

