Mailgun send inline image not working?

I am using mailgun to send mail. I am trying to use python api

pwd = "path-to-image/logo.png"
return requests.post(
     "https://api.mailgun.net/v2/sandboxsomething.mailgun.org/messages",
     auth=("api", "key-something"),
     files=[("inline", open(pwd)),],
     data={"from": src,
           "to": des,
           "subject": sub,
           "html": message})

      

but it cannot send the image.

after that i try to show only png file when printing print open(pwd).read()

i get:

  PNG


 none

      

but when i try print open('path-to-image/a.txt')

i get the content of the file:

all content of a.text
none

      

why is the file png

not being read?

+3


source to share


3 answers


the image should be:

open(pwd,"rb")

      



for reading in binary mode.

+3


source


open(pwd,"rb")

      



you can use this link: fooobar.com/questions/61025 / ...

+1


source


A bit late to answer this, but I was also looking for a solution and couldn't find any online. I coded my own and I taught it to share here.

When mailgun sends a new message to an endpoint, it parses inline images as attachments. Below is a fix to keep images inline using PHP.

//Handling images
if(!empty($_FILES)){

   //Remove <> from string to set proper array key
   $inline_images=str_replace(array('<', '>'), '', $_POST['content-id-map']);

   //Get inline images
   $inline_images=json_decode($inline_images, true);

   if(!empty($inline_images)){

       foreach($inline_images as $key=>$i){
          if(isset($_FILES[$i])){

             //Now we have the inline images. You upload it to a folder or encode it base64.

            //Here is an example using base64
            $image=file_get_contents(base64_encode($_FILES[$i]['tmp_name']));

            //Now, we will str_replace the image from the email body with the encoded 6ase64 image. 

           $_POST['body-html']=str_replace('cid:'.$key, 'data:image/png;base64,'.$image, $_POST['body-html']);
        }
  }


   //Parsing actual attachments

      //Unset all inline images from attachments array
      foreach($inline_images as $i){
         unset($_FILES[$i]);
      }

       //Now, since we have removed all inline images from $_FILES. You can add your code to parse actual attachments here. 
   }
} 

      

There you go, an easy way to parse inline attachments using mailgun.

+1


source







All Articles