0

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)
Jdahern
  • 1,196
  • 9
  • 14
  • I know nothing about php/mailgun and something about email construction. Are you able to produce and email with the above code that "doesn't" work, or are you simply looking for a way to call php curl code with a string instead of a file? If it's the former, check the email's source file against the examples in [rfc2183](http://www.ietf.org/rfc/rfc2183.txt), in section 3. Either way, what format is your image string in? For example, is it base64 encoded, or do you expect mailgun to encode it? – n0741337 Apr 26 '13 at 22:33
  • A little more explanation is that I am switching from our current SMPT servers to mailGun. We use an old version of phpmailer to send the files which do base64 encode. So I have the raw and the base64 version. Also its a PNG file and the above code sample is what I am trying to execute. – Jdahern Apr 27 '13 at 01:22
  • What errors are you receiving? According to several stackoverflow questions, php curl can handle multiple input forms for data eg. [CURL PHP send image](http://stackoverflow.com/questions/3433542/curl-php-send-image). You could skip the array technique and build the complete urlencoded string as in the manual like `param1=val1&param2=val2&...`. If that doesn't work, maybe mailgun's api only likes inline= images posted to them a special way. You could send a working example to a webserver that just echos the input back to you so you could learn to mimic that format. – n0741337 Apr 27 '13 at 22:06
  • The main problem is sending a file via string. If it was one file, I can manually set the parameters and send it that way. If i had a file location, i could use the @ notation like the ones in your example. But neither are available to me. There is not errors also, as it send the string as a param when it needs to send a file with the mime type, name, ect. – Jdahern Apr 29 '13 at 21:28
  • I apologize that I'm not useful here. If you don't want php curl's automagic mime-type etc wrapping and want to send your string, you might have make the complete body of the request yourself. Something like [this example](http://comments.gmane.org/gmane.comp.web.curl.general/11965) but where you wouldn't have to read in a file in the `$pld0200request`/`$pldfile` on that page. You'd have to manage contents lengths etc "yourself" ( there's probably a library for that ). Also, perhaps you could try curl ( no php ) and it's --trace-ascii flag to "see" what's going on. – n0741337 Apr 29 '13 at 23:22

1 Answers1

4

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"

israr
  • 1,155
  • 15
  • 28
  • Throws "Notice: Array to string conversion in..." on the line where it uses the array -> 'inline' => array($image) Anyone faced this? – Souvik Ghosh Dec 24 '14 at 12:49