2

我有个问题。我正在尝试使用 gmail api 阅读来自 gmail 的电子邮件。我按照这里的说明https://developers.google.com/gmail/api/v1/reference/users/messages/get

我做了一些更改以在 python 3 上运行它,所以我最终得到了以下代码:

def GetMimeMessage(service, user_id, msg_id):
try:
  message = service.users().messages().get(userId=user_id, id=msg_id,
                                         format='raw').execute()

  print ('Message snippet: %s' % message['snippet'])
  msg_str = base64.urlsafe_b64decode(message['raw'].encode('utf8'))
  mime_msg = email.message_from_bytes(msg_str)
  print(mime_msg)

  return mime_msg
except errors.HttpError as error:
  print ('An error occurred: %s' % error)

现在输出非常接近我想要的,但是有一个问题是输出中的匈牙利重音字符很奇怪: G=C3=A1bor而不是Gábor

而且 html 标签也被破坏了:

Follow us:          =09=09=09=09=09=09=09=09=09<a href=3D"http=

我已经发现这与消息的编码方式有关,请参阅电子邮件的标题:

Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

问题是,我似乎无法找到正确解码它的方法。感谢您的帮助。

4

2 回答 2

4

经过几个小时的努力解决同样的问题,我终于找到了解决方案(至少对我来说是这样)。即使线程很旧,我也将其发布在这里,以便其他人以后可以找到它。它是这样的:

# Get your message
message = service.users().messages().get(userId=user_id, id=msg_id, format='full').execute()

# Get the right body data item (this part was my Heureka-moment as there are several 'data' keys)
content = message['payload']['parts'][0]['parts'][1]['body']['data']

# Encode
msg_body = base64.urlsafe_b64decode(content).decode('utf-8')

这个序列使解码/编码对我有用。有许多不同的解决方案建议,因为有关于 base64 和 Google Gmail API 的解码/编码问题的线程,但似乎每种情况都是独一无二的。希望此解决方案也对您有所帮助!

于 2018-07-24T19:43:44.423 回答
0

谢谢图皮塔皮。Python 的 Gmail API 文档可能会更清晰一些。上述解决方案也适用于我。我可以通过以下方式检索内容:

 msgs = message['payload']['parts']
 msg = msgs[1]['body']['data']

MessagePart 可能由不同的部分组成:

"parts": [
      {
        "partId": "0",
        "mimeType": "text/plain",
        "filename": "",
        "headers": [
          { "name": "Content-type", "value": "text/plain;charset=utf-8" },
          { "name": "Content-Transfer-Encoding", "value": "quoted-printable" }
        ],
        "body": {
          "size": 1887,
          "data": "ascii string to be decoded"
        }
      },
      {
        "partId": "1",
        "mimeType": "text/html",
        "filename": "",
        "headers": [
          { "name": "Content-type", "value": "text/html;charset=utf-8" },
          { "name": "Content-Transfer-Encoding", "value": "quoted-printable" }
        ],
        "body": {
          "size": 66970,
          "data": "longer ascii string to be decoded"
        }
      }
    ]

从Doc中提取完整的有效负载:

Resource representations An email message.

{   "id": string,   "threadId": string,   "labelIds": [
    string   ],   "snippet": string,   "historyId": unsigned long,   "internalDate": long,   "payload": {
    "partId": string,
    "mimeType": string,
    "filename": string,
    "headers": [
      {
        "name": string,
        "value": string
      }
    ],
    "body": users.messages.attachments Resource,
    "parts": [
      (MessagePart)
    ]   },   "sizeEstimate": integer,   "raw": bytes }
于 2020-03-08T18:19:51.610 回答