我有问题。我正在尝试使用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
问题是,我似乎无法找到正确解码的方法。永远的帮助表示赞赏。
答案 0 :(得分:2)
经过数小时的自我解决,我终于找到了解决方案(至少对于我的情况而言)。即使线程很旧,我也将其发布在这里,以便其他人以后可以找到它。在这里:
# 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的解码/编码问题的线索,但每种情况似乎都是唯一的。希望此解决方案也对您有所帮助!
答案 1 :(得分:0)
感谢Tuppitappi。适用于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 }