2 回答
TA贡献1836条经验 获得超13个赞
在搞砸之后,我制作了两个函数,可以将plain和html内容类型传递给可能遇到困难的任何人的新电子邮件:
def get_all_parts(service, user_id, msg_id):
message = service.users().messages().get(userId=user_id, id=msg_id, format='raw').execute()
msg_bytes = base64.urlsafe_b64decode(message['raw'].encode('ASCII'))
b = email.message_from_bytes(msg_bytes)
body = []
if b.is_multipart():
for part in b.walk():
if part.get_content_maintype() != 'multipart':
this_part = []
this_part.append(part.get_payload(decode=True))
this_part.append(part.get_content_type())
body.append(this_part)
return body
def create_message(sender, to, subject, thread_id, message_id, message_text, service):
message = MIMEMultipart()
message['to'] = to
message['from'] = sender
message['subject'] = 'Re: %s' %subject
for part in message_text:
text = part[1].split('/') # 'text/plain' -> ['text', 'plain']
new_part = MIMEText(str(part[0]), text[1])
print(part[1])
message.attach(new_part)
raw = base64.urlsafe_b64encode(message.as_string().encode('UTF-8')).decode('ascii')
body = {'raw': raw, 'threadId': thread_id}
enter code here
这绝对不是所有电子邮件的详尽功能,但适用于alternative内容类型。
TA贡献1921条经验 获得超9个赞
添加回答
举报