19

在 mailgun 文档中找不到我的问题的解决方案后,我将解释我在寻找什么。

今天我正在使用 phpList 发送我的时事通讯(它工作得很好!),我有 HTML 页面,我只是包含在 phpList 应用程序中来发送它。(我正在使用 SMTP 方法发送消息)。我想知道我是否可以对 mailgun 做同样的事情(当然可以,但是怎么做?),是否可以只包含我的 HTML 页面的路径以将其发送出去?(我没有兴趣在脚本中键入我的 html 代码,它必须在路径中,否则我对使用 mailgun 没有兴趣)。

看看我的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'));

我有一个名为 的数组元素'html',我想包含我的 HTML 页面的路径(如果不可能,我可以把它放在哪里?)。我只是不能在这个 html 数组元素中包含我的整个 HTML 代码,因为它是如此广泛。

但是mailgun声称简单而伟大,这就是我想要改变的动机。

4

2 回答 2

43

我以这种方式使用了外部 html 模板。它可能对你有帮助。

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

接着:

$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'));

检查这一行:

'html'    => $html,
于 2014-07-22T01:43:42.583 回答
4

添加指向 Mailgun 文档的链接。这有助于我构建 HTML 和 MIME 消息。https://documentation.mailgun.com/api-sending.html#examples

根据文档:

# 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')
));
于 2016-12-01T10:03:01.777 回答