Question

I am using mailGun web API and ran into the issue of adding inline files. Our software creates an image and passes it around as a string. I want to inline that image The problem that I have is that php curl takes in a file pointer, and not an actual file. I want to avoid writing a tmp file if possible as we have many process that work on the server and would not want to send a bad email

Thanks in advance

MailGun inline Sample:http://documentation.mailgun.net/user_manual.html#inline-image Code sample that I am using:

function send_inline_image($image) {
  $ch = curl_init();

  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($ch, CURLOPT_USERPWD, 'api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/samples.mailgun.org/messages');
  curl_setopt($ch,
              CURLOPT_POSTFIELDS,
              array('from' => 'Excited User <me@samples.mailgun.org>',
                    'to' => 'sergeyo@profista.com',
                    'subject' => 'Hello',
                    'text' => 'Testing some Mailgun awesomness!',
                    'html' => '<html>Inline image: <img src="cid:test.jpg"></html>',
                    'inline' => $image))

  $result = curl_exec($ch);
  curl_close($ch);

  return $result;
 }
$image = 'Some image string that we have generated'
send_inline_image($image)
Was it helpful?

Solution

You need to change only the inline parameter of array. I have done it and its works. Inline parameter should be an array instead of string image path. You can do it like this:

function send_inline_image($image) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($ch, CURLOPT_USERPWD, 'api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
 curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/samples.mailgun.org/messages');
curl_setopt($ch,
          CURLOPT_POSTFIELDS,
          array('from' => 'Excited User <me@samples.mailgun.org>',
                'to' => 'sergeyo@profista.com',
                'subject' => 'Hello',
                'text' => 'Testing some Mailgun awesomness!',
                'html' => '<html>Inline image: <img src="cid:test.jpg"></html>',
                'inline' => array($image)//Use array instead of $image
))

   $result = curl_exec($ch);
  curl_close($ch);
  return $result;
}
$image = 'Some image string that we have generated'
send_inline_image($image)

Look at comment "Use array instead of $image"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top