[英]How to send out HTML email with mailgun?
在找不到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聲稱自己很容易,也很棒,這就是我想改變的動機。
我用這種方式使用了外部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,
添加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')
));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.