2014-02-14 67 views
13

未能在mailgun文檔中找到我的問題的解決方案後,我會解釋我在找什麼。如何用mailgun發送HTML郵件?

今天我使用phpList發送我的通訊(它工作完美!),我有我剛包含在phpList應用程序發送出去的HTML頁面。 (我使用SMTP方法發送消息)。我想知道我是否可以用mailgun來做同樣的事情(當然可以,但怎麼做?),是否可以包含我的HTML頁面發送出去的路徑? (我沒有興趣在腳本中鍵入我的html代碼,它必須在路徑中,否則我對使用mailgun沒有興趣)。

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

我有數組元素命名爲'html',我想包括我的HTML頁面的路徑(如不可能,我在哪裏可以把它?)。我不能在HTML數組元素中包含我的整個HTML代碼,因爲它非常廣泛。

但是mailgun聲稱自己很容易和很棒,那就是我想改變的動機。

+0

確實堆放着mailgun!疼痛的屁股API! – B4NZ41

回答

29

我以這種方式使用了外部html模板。這可能對你有所幫助。

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

然後:

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

檢查這一行:

'html' => $html, 
+2

我和你一樣,現在工作正常! $ issue = file_get_contents('http://xxxx.html'); $ batchMsg-> setHtmlBody($ issue); 我會接受你的回答是正確的!謝謝。 – B4NZ41

+1

如何使用JavaScript實現此目的? – Anirudh

+0

你可以通過發送一個Ajax請求來獲得這個。 – israr

2

將鏈接添加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 <[email protected]_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') 
));