当前位置: 动力学知识库 > 问答 > 编程问答 >

Python gmail api send email with attachment pdf all blank

问题描述:

I am using python 3.5 and below code is mostly from the google api page...

https://developers.google.com/gmail/api/guides/sending

slightly revised for python 3.x

i could successfully send out the email with the content text and an attachment pdf but the pdf attached is completely blank..

please help and thanks in advance

def create_message_with_attachment(bcc, subject, message_text, file,sender='me' ):

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}

分享给朋友:
您可能感兴趣的文章:
随机阅读: