无法使用 Gmail API + rails 在 HTML 中显示嵌入的图像 [英] Not able to display embedded images in HTML using Gmail API + rails

查看:27
本文介绍了无法使用 Gmail API + rails 在 HTML 中显示嵌入的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用,在该应用中我使用 看起来像这样:

Content-ID:

Gmail API 应该给你一个看起来像这样的响应:

<代码>{部分": [//... 其他部分 ...{"partId": "1","mimeType": "图像/png",文档名称": "",标题":[{"name": "内容 ID","值": ""},{"name": "内容类型",值":图像/png"},{"name": "内容传输编码",值":base64"}],身体": {大小":9999,"data": "R0lGODlhGAGgAPEAAP...(原始base64编码图像)",}}//... 其他部分 ...]//... 其余响应 ...}

为了显示图像,您需要返回响应对象并找到 headernameContent-IDii_jfi5vwc30_1628627122d12121 的一个 value,然后解析对象以读取其 body.data,即 base64 编码的图像你想要.

如果回复不包含这部分内容,则表示原始电子邮件格式不正确或相关图像未以某种方式正确附加到电子邮件中,然后您就走运了,无法呈现原图.

I'm building an app, in which I'm fetching emails from Gmail using Gmail API.

In an email there is 1 image embedded directly in email body(Inline image not attachment). I'm able to extract text/html part and it's displaying properly on browser but in case of inline image, it's showing broken image.

In image tag it's showing as

<img src=\"cid:ii_jfi5vwc30_1628627122d12121\" width=\"454\" height=\"255\">

It's giving content id instead of image url in src. Does anyone know about how should I display inline image using cid in browser page. How should I get image in base64 format from cid?

解决方案

cid: is used as a way to refer to an image attached to a multi-part email, so that the image is an attachment on the email instead of being embedded inline in the email. This means to display your image, you have to look up which attachment contains the referenced image.

Your cid URL corresponds to a Content-ID MIME header that looks like this:

Content-ID: <ii_jfi5vwc30_1628627122d12121>

The Gmail API should have given you a response that looks something like this:

{
  "parts": [
    // ... other parts ...
    {
      "partId": "1",
      "mimeType": "image/png",
      "filename": "",
      "headers": [
        {
          "name": "Content-ID",
          "value": "<ii_jfi5vwc30_1628627122d12121>"
        },
        {
          "name": "Content-Type",
          "value": "image/png"
        },
        {
          "name": "Content-Transfer-Encoding",
          "value": "base64"
        }
      ],
      "body": {
        "size": 9999,
        "data": "R0lGODlhGAGgAPEAAP...(the original base64 encoded image)",
      }
    }
    // ... other parts ...
  ]
  // ... rest of response ...
}

In order to display the image, you'll need to go back through the response object and find the header with a name of Content-ID and a value of ii_jfi5vwc30_1628627122d12121, and then parse the object in order to read its body.data, which is the base64 encoded image you desire.

If the response does not include this part, then it means the original email was malformed or the associated image was not correctly attached to the email in some way, and then you're out of luck and there is no way to render the original image.

这篇关于无法使用 Gmail API + rails 在 HTML 中显示嵌入的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆