简体   繁体   English

如何使用mailgun发送HTML电子邮件?

[英]How to send out HTML email with mailgun?

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. 今天我使用phpList发送我的新闻通讯(它工作得很完美!),我有HTML页面,我只是包含在phpList应用程序中发送出去。 (I'm using SMTP method to send news out). (我正在使用SMTP方法发送消息)。 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? 我想知道我是否可以对mailgun做同样的事情(肯定可以,但是如何?),是否可以只包含我的HTML页面的路径来发送它? (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). (我没有兴趣在脚本中输入我的html代码,它必须在路径中,否则我对使用mailgun没兴趣)。

Take a look to my mailgun php code as follow: 看看我的mailgun php代码如下:

$result = $mgClient->sendMessage("$domain",
           array('from'    => 'My Business Name <me@samples.mailgun.org>',
                 'to'      => 'name-1@gmail.com, name-3@gmail.com, name-3@gmail.com',
                 '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' => '{"name-1@gmail.com": {"first":"Name-1", "id":1}, "name-2@gmail.com": {"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?). 我有一个名为'html'的数组元素,我想包含我的HTML页面的路径(如果不可能,我可以把它放在哪里?)。 I just can not include my whole HTML code in this html array element, cause it is so extensive. 我只是不能将我的整个HTML代码包含在这个html数组元素中,因为它是如此广泛。

But mailgun claim to be easy and great, that is the motive I want to change. 但是,mailgun声称自己很容易,也很棒,这就是我想改变的动机。

I have used external html template in this way. 我用这种方式使用了外部html模板。 It may be help you. 它可能对你有所帮助。

$html  = file_get_contents('my_template.html'); // this will retrieve the html document

and then: 然后:

$result = $mgClient->sendMessage("$domain",
       array('from'    => 'My Business Name <me@samples.mailgun.org>',
             'to'      => 'name-1@gmail.com, name-3@gmail.com, name-3@gmail.com',
             'subject' => 'Issue Feb 2014',
             'text'    => 'Your mail do not support HTML',
             'html'    => $html,
             'recipient-variables' => '{"name-1@gmail.com": {"first":"Name-1", "id":1}, "name-2@gmail.com": {"first":"Name-2", "id": 2}}'), 
       array('inline' => 'Pad-Thai-1.jpg'));

Check this line: 检查这一行:

'html'    => $html,

Adding a link to Mailgun documentation. 添加Mailgun文档的链接。 This helped me while constructing HTML and MIME messages. 这有助于我构建HTML和MIME消息。 https://documentation.mailgun.com/api-sending.html#examples 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'      => 'foo@example.com',
    'cc'      => 'baz@example.com',
    'bcc'     => 'bar@example.com',
    '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')
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM