【问题标题】:mailgun send inline image not working?mailgun发送内联图像不起作用?
【发布时间】:2014-10-02 10:32:50
【问题描述】:

我使用 mailgun 发送邮件。 我尝试使用 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})

但它无法发送图像。

之后,当我打印print open(pwd).read() 时,我尝试仅显示 png 文件:

 �PNG


 none

但是当我尝试print open('path-to-image/a.txt')时,我得到了文件内容:

all content of a.text
none

为什么没有读取png 文件?

【问题讨论】:

  • “它可以发送图像”是什么意思?它会产生错误消息吗?邮件发不出去吗?邮件是否已发送,但数据有误?
  • 它发送消息,但图像不能显示,因为它是空的!。我尝试在发送之前打印图像,我得到�PNG none

标签: python mailgun inline-images


【解决方案1】:

打开图片必须是:

open(pwd,"rb")

以二进制模式读取。

【讨论】:

    【解决方案2】:
    open(pwd,"rb")
    

    您可以使用此链接:https://stackoverflow.com/a/23566951/3726821

    【讨论】:

      【解决方案3】:

      回答这个问题有点晚了,但我也在寻找解决方案,但在网上找不到任何解决方案。我自己编写了代码,我教我在这里分享。

      当 mailgun 将新消息发布到端点时,它会将内联图像解析为附件。这是使用 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. 
         }
      } 
      

      你去吧,一种使用 mailgun 解析内联附件的简单方法。

      【讨论】:

        猜你喜欢
        • 2014-11-27
        • 1970-01-01
        • 2014-07-26
        • 2013-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-10
        相关资源
        最近更新 更多