Search code examples
imapgmail-apiemail-attachmentsmime

Is there a way to distingush inline picture and picture attachment via Gmail API


I fetched two emails from Gmail via their APIs, one contains an inline picture, the other contains an picture attachment. See their HTTP response below.

By comparing these two sections, I am not able to tell which picture is attached and which is inline. "Content-Disposition" shows that both are attachments, though one of them is actually an inline photo.

Is there a way to identify inline pictures with the response from Gmail API?

// inline
      {
        "partId": "2",
        "mimeType": "image/heic",
        "filename": "image_50410497.JPG",
        "headers": [
          {
            "name": "Content-Type",
            "value": "image/heic; name=\"image_50410497.JPG\""
          },
          {
            "name": "Content-Disposition",
            "value": "attachment; filename=\"image_50410497.JPG\""
          },
          {
            "name": "Content-Transfer-Encoding",
            "value": "base64"
          },
          {
            "name": "X-Attachment-Id",
            "value": "18334b929992fd46a211"
          },
          {
            "name": "Content-ID",
            "value": "\u003c18334b929992fd46a211\u003e"
          }
        ],
        "body": {
          "attachmentId": "ANGjdJ8FshN6fd_2OoZEttwPYHk_8q1mVOJevilskBM-6yOZZ6aMMSMblU3Vo5pw-V1_SeDzxkVx0zOg5R-9fGkaSGvGzd6Wi9yVBe4dAn03HDCghyUWFC2jyodeWYmttzzaXyCNRUVPdVxmO7l8yTaeEsQ4Ep1Ze7Nc3bnLNozWHeKZQHQLqGyfLKDdEI1GKjT8X6OuyEY6EWMo8djE30c-BvYjuY95vmomjkjzfoIqTFfpUlMMktNUfvC1SZMHL0arymXmTTM6uVg5N0U2TngVfbKNx0x8hI0bhccB-AiIhwrSqCxM_CZkyXrGRcY",
          "size": 2607632
        }
      },
// attached
      {
        "partId": "1",
        "mimeType": "image/jpeg",
        "filename": "unnamed.jpeg",
        "headers": [
          {
            "name": "Content-Type",
            "value": "image/jpeg; name=\"unnamed.jpeg\""
          },
          {
            "name": "Content-Disposition",
            "value": "attachment; filename=\"unnamed.jpeg\""
          },
          {
            "name": "Content-Transfer-Encoding",
            "value": "base64"
          },
          {
            "name": "Content-ID",
            "value": "\u003cf_l83jtl4x0\u003e"
          },
          {
            "name": "X-Attachment-Id",
            "value": "f_l83jtl4x0"
          }
        ],
        "body": {
          "attachmentId": "ANGjdJ_6KFGMzvKW6XFwD4BaSjCDSQGWPEMpL97DE1Lx31cKi2cSzSTOMDIdTEV8wyvnLiB8iqg5_1CVlDOOofl4NiEll2IwrxDuE-IdDXP9PmryOXbMp0pFgIQ961UQWQk8yhObqPcx8xWnqQaPWI3pwirH6hhoe3JtswoLXQ1NDs7FjJZ2iivLZHoTvMlh-i4VQIK6JVaEdIcQBejI6WruTi7DuC_ZpRwewfReZ2JsPKtncVCFwOkb0Ov1vElLS7Y1BTATiRzurXQw1A4lYOn5-XDKqXk90p_HxEQO5zEsvdz2MigVSa803-mIvK8RFTyOOuA4iVWEzad0I3mEFVE6bxTfFQ_YnAYm1FLKImnMEkJf5MuyZofZleyu8fjTGGqzvWmjikDGLvAzdM2O",
          "size": 1553427
        }
      }

UPDATE:

Max mentioned 'Content-ID', I can confirm it works for some emails I have, here is an example:

// header of the attachment part
{
  "name": "Content-ID",
  "value": "\u003cii_l83jb7dh0\u003e"
}

// decoded HTML body
<img src="cid:ii_l83jb7dh0" alt="20190906-2P2A3622.jpeg" width="361" height="542"><br>

Solution

  • Sometimes the only thing that distinguishes them is whether the Content-ID is referenced in the HTML body of the message. More precisely, by any reasonable definition, that image is an attachment, but it may also be referenced as a cid: url in an <img> tag in an HTML mime part. The header information alone cannot tell you that.