【问题标题】:Gmail API: how to fetch message body without attachmentsGmail API:如何获取没有附件的邮件正文
【发布时间】:2016-12-16 04:39:28
【问题描述】:

根据https://developers.google.com/gmail/api/v1/reference/users/messages/get

似乎选择“完整”和“原始”格式是获取电子邮件正文的选项。

有没有办法获取电子邮件正文,而不是附件?

【问题讨论】:

    标签: gmail-api


    【解决方案1】:

    raw 格式将为您提供邮件中的所有内容(包括附件)。但是,full 格式会为您提供附件的 id 而不是数据,因此 full 是您要使用的格式。

    以下是如何从full 格式响应中获取正文的示例:

    var response = {
     "payload": {
      "parts": [
       {
        "mimeType": "multipart/alternative",
        "filename": "",
        "headers": [
         {
          "name": "Content-Type",
          "value": "multipart/alternative; boundary=001a1142e23c551e8e05200b4be0"
         }
        ],
        "body": {
         "size": 0
        },
        "parts": [
         {
          "partId": "0.0",
          "mimeType": "text/plain",
          "filename": "",
          "headers": [
           {
            "name": "Content-Type",
            "value": "text/plain; charset=UTF-8"
           }
          ],
          "body": {
           "size": 9,
           "data": "V293IG1hbg0K"
          }
         },
         {
          "partId": "0.1",
          "mimeType": "text/html",
          "filename": "",
          "headers": [
           {
            "name": "Content-Type",
            "value": "text/html; charset=UTF-8"
           }
          ],
          "body": {
           "size": 30,
           "data": "PGRpdiBkaXI9Imx0ciI-V293IG1hbjwvZGl2Pg0K"
          }
         }
        ]
       },
       {
        "partId": "1",
        "mimeType": "image/jpeg",
        "filename": "feelthebern.jpg",
        "headers": [
         {
          "name": "Content-Type",
          "value": "image/jpeg; name=\"feelthebern.jpg\""
         },
         {
          "name": "Content-Disposition",
          "value": "attachment; filename=\"feelthebern.jpg\""
         },
         {
          "name": "Content-Transfer-Encoding",
          "value": "base64"
         },
         {
          "name": "X-Attachment-Id",
          "value": "f_ieq3ev0i0"
         }
        ],
        "body": {
         "attachmentId": "ANGjdJ_2xG3WOiLh6MbUdYy4vo2VhV2kOso5AyuJW3333rbmk8BIE1GJHIOXkNIVGiphP3fGe7iuIl_MGzXBGNGvNslwlz8hOkvJZg2DaasVZsdVFT_5JGvJOLefgaSL4hqKJgtzOZG9K1XSMrRQAtz2V0NX7puPdXDU4gvalSuMRGwBhr_oDSfx2xljHEbGG6I4VLeLZfrzGGKW7BF-GO_FUxzJR8SizRYqIhgZNA6PfRGyOhf1s7bAPNW3M9KqWRgaK07WTOYl7DzW4hpNBPA4jrl7tgsssExHpfviFL7yL52lxsmbsiLe81Z5UoM",
         "size": 100446
        }
       }
      ]
     }
    };
    
    function decode(string) {
      return decodeURIComponent(escape(atob(string.replace(/\-/g, '+').replace(/\_/g, '/'))));
    }
    
    function getText(response) {
      var result = '';
      // In e.g. a plain text message, the payload is the only part.
      var parts = [response.payload];
    
      while (parts.length) {
        var part = parts.shift();
        if (part.parts) {
          parts = parts.concat(part.parts);
        }
        if (part.mimeType === 'text/plain') {
          // Continue to look for a 'text/html' part.
          result = decode(part.body.data);
        } else if (part.mimeType === 'text/html') {
          // 'text/html' part found. No need to continue.
          result = decode(part.body.data);
          break;
        }
      }
      
      return result;
    }
    
    
    var text = getText(response);
    console.log(text);

    【讨论】:

      猜你喜欢
      • 2021-04-20
      • 2018-05-06
      • 2016-12-12
      • 1970-01-01
      • 2020-10-12
      • 2011-04-23
      • 2015-01-23
      • 2023-03-06
      • 1970-01-01
      相关资源
      最近更新 更多