20

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

devasia2112
  • 5,844
  • 6
  • 36
  • 56

2 Answers2

44

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 <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,
israr
  • 1,155
  • 15
  • 28
  • 2
    I did the same as you and now work fine! $issue = file_get_contents('http://xxxx.html'); $batchMsg->setHtmlBody( $issue ); I will accept your answer as correct! Thanks. – devasia2112 Aug 04 '14 at 00:47
  • 1
    How to achieve this using JavaScript? – Anirudh Jan 03 '16 at 10:59
  • You can got this by sending ajax request. – israr Mar 04 '16 at 10:26
  • I am wanting to do something similar, but instead of using file_get_contents, I want to use an HTML string so that I can concatenate data into the html string. I tried taking this code and running it and it ran fine, but when I put a string in place of HTML, it broke. Any help? –  Sep 01 '16 at 20:28
  • 1
    If you are sending text email instead of html email then you should set the text property. `'text' => 'Your mail do not support HTML', 'html' => $html` Instead of html you should set text – israr Sep 02 '16 at 10:57
4

Adding a link to Mailgun documentation. This helped me while constructing HTML and MIME messages. 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')
));
Robin Dowling
  • 360
  • 4
  • 9