各位大神好,
小弟刚学python一个月,很多概念都不是很懂。
来这里向大家请教,希望会的人可以帮帮忙
以下python 3.5 code是抓gmail api网站上修改的
(网站https://developers.google.com/gmail/api/guides/sending)
def create_message_with_attachment(bcc, subject, message_text,
file,sender=sender ):
message = MIMEMultipart()
message['bcc'] = bcc
message['from'] = sender
message['subject'] = subject
msg = MIMEText(message_text)
message.attach(msg)
content_type, encoding = mimetypes.guess_type(file)
main_type, sub_type = content_type.split('/', 1)
fp = open(file, 'rb')
msg = MIMEBase(main_type, sub_type)
msg.set_payload(fp.read())
fp.close()
filename = os.path.basename(file)
msg.add_header('Content-Disposition', 'attachment', filename=filename)
message.attach(msg)
raw = base64.urlsafe_b64encode(message.as_bytes())
raw = raw.decode()
return {'raw':raw}
当我file给他一个doc文件或是pdf文件的路径时
最后都可以成功记出夹带档案的email 但是夹带的档案都是空白或毁损
请问上面生成message的程式码有哪里有问题吗?
感谢