4

I'm working with mailgun and want to add images to my newsletters. Now I did this:

$mg->sendMessage($domain, array('from'    => 'developer@mijnprojectgroep.eu', 
                                'to'      => 'developer@mijnprojectgroep.eu', 
                                'subject' => 'Developers Mail Test MijnProjectgroep batch #1', 
                                'text'    => 'Hallo %recipient_fname%,


                'html'    => '<html>
<img style="display:block;" class="img1" src="cid:header-clip.png" width="600" height="64" />
</html>',
array('inline' => '@.././images/newsletter/header-clip.png'),

'o:tracking-opens' => 'yes'));

But no images are loading while I receive the newsletter. The document with the script above is in:

Root --> /MailGun/

The images are in:

Root --> /images/newsletter/

Also tried: @../../images/newsletter/header-clip.png

The documentation is here:

http://documentation.mailgun.com/user_manual.html?highlight=html#sending-via-api

What did I do wrong?

Shiela
  • 500
  • 1
  • 7
  • 20
Rick A.
  • 213
  • 2
  • 4
  • 11

4 Answers4

4

You did not do wrong. Actually there is an issue in the API documentation.

You need to use an array instead of string path in inline image path. It will solve the issue. You can add it like this:

$mg->sendMessage($domain, array('from'    => 'developer@mijnprojectgroep.eu',
                                    'to'      => 'developer@mijnprojectgroep.eu', 
                                    'subject' => 'Developers Mail Test MijnProjectgroep batch #1',
                                    'text'    => 'Hallo %recipient_fname%,
                                    'html'    => '<html><img style="display:block;" class="img1" src="cid:header-clip.png" width="600" height="64" /></html>',
    array('inline' => array('@.././images/newsletter/header-clip.png') 
),
    'o:tracking-opens' => 'yes'));

Please check this line:

array('inline' => array('@.././images/newsletter/header-clip.png') 
israr
  • 1,155
  • 15
  • 28
  • i can send single embedded image. but i have multiple embedded image then how can do it. and i have some text content then image then some text and then image. how can i dynamically embed image. – Dhara Sep 12 '16 at 06:39
  • 2
    You can follow this link to view how to send complete html: http://stackoverflow.com/questions/21786128/how-to-send-out-html-email-with-mailgun/24877620#24877620 – israr Sep 16 '16 at 09:49
1

The images to be attached need to be passed in as the 3rd argument to the sendMessage method:

    $mgClient->sendMessage("$domain",
              array('from'    => 'Mailgun Sandbox <postmaster@sandbox.mailgun.org>',
                    'to'      => 'mr awesome <mrawesome@web.com>',
                    'subject' => 'Hello Mr',
                    'html' => '<html><img style="display:block;" class="img1" src="cid:header-clip.png" width="600" height="64" /></html>'
              ),
              array (
                'inline' => array(dirname(__FILE__).'/images/newsletter/header-clip.png')
              )
      );

Also note the file path to the file: dirname(__FILE__). You may need to change this to suit.

An example is also found in the Mailgun docs, under the heading "Sending Inline Images" - https://documentation.mailgun.com/user_manual.html#sending-via-api

zumek
  • 608
  • 6
  • 14
0

To embed an image in your html code, you need to set an inline property to parameters array. This inline property expects a filePath and a fileName. Example:

$mgClient->messages()->send($domain, array(
    'from'    => 'developer@mijnprojectgroep.eu',
    'to'      => 'developer@mijnprojectgroep.eu',
    'subject' => 'Developers Mail Test MijnProjectgroep batch #1',
    'text'    => 'Hallo %recipient_fname%',
    'html'    => '<html><img style="display:block;" class="img1" src="cid:header-clip.png" width="600" height="64" /></html>',
    'inline' => array(
        array(
            'filePath' => 'path/to/images/newsletter/header-clip.png',
            'filename' => 'header-clip.png'
        )
    ),
    'o:tracking-opens' => 'yes'
));

If you need to add more than one image, just add an inline property for each image.

$mgClient->messages()->send($domain, array(
    'from'    => 'developer@mijnprojectgroep.eu',
    'to'      => 'developer@mijnprojectgroep.eu',
    'subject' => 'Developers Mail Test MijnProjectgroep batch #1',
    'text'    => 'Hallo %recipient_fname%',
    'html'    => '
    <html>
       <img style="display:block;" class="img1" src="cid:image1.png" width="600" height="64" />
    </html>
    <html>
       <img style="display:block;" class="img1" src="cid:image2.png" width="600" height="64" />
    </html>',
    'inline' => array(
        array(
            'filePath' => 'path/to/images/newsletter/image1.png',
            'filename' => 'image1.png'
        )
    ),
    'inline' => array(
        array(
            'filePath' => 'path/to/images/newsletter/image2.png',
            'filename' => 'image2.png'
        )
    ),
    'o:tracking-opens' => 'yes'
));

Notice that I've added a new img tag for new image. Make sure you have a cid:image_name added to src attribute for each image.

abendevs
  • 136
  • 1
  • 11
-1

For this you have to just enter the details as:

$header = FCPATH."public_html/assets/img/newsletter_header.png";

$mg->messages()->send($domain, [
  'from'    => 'xxxxxxx@gmail.com',
  'to'      => 'yyyyy <yyyyyyyy@gmail.com>',
  'subject' => 'The PHP SDK is awesome!',
  'html'    =>  $html,
  'inline' => array(
    array('filePath' => $header)
  )
]);

And in your HTML code:

$imageName = "newsletter_header.png";
<img src="cid:'.$imageName.'" width="408" height="100" alt="Pedul" border="0"
class="em_w150" />

For this you have to take care of 2 points:

  1. in inline tag add the full path of your image
  2. In your HTML code add only image name of file.
h3t1
  • 1,126
  • 2
  • 18
  • 29
  • Please format your answer and explain how it differs from the other 6-year-old answers for the question. – miken32 Dec 12 '20 at 22:17