How to send out HTML email with mailgun?

deepcell picture deepcell · Feb 14, 2014 · Viewed 25.6k times · Source

after failed to find a solution for my problem in the mailgun documentation, I will explain what I'm looking for.

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).

Take a look to my mailgun php code as follow:

$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.

But mailgun claim to be easy and great, that is the motive I want to change.

Answer

israr picture israr · Jul 22, 2014

I have used external html template in this way. 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 <[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,