Python gmail api send email with attachment pdf all blank

2024/10/6 20:28:33

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'] = bccmessage['from'] = sendermessage['subject'] = subjectmsg = 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}
Answer

you just forgot to encode the attachment. Replace two lines of your code with those:

  import email.encoders...msg.add_header('Content-Disposition', 'attachment',filename=filename)email.encoders.encode_base64(msg)message.attach(msg)...
https://en.xdnf.cn/q/70328.html

Related Q&A

how to find height and width of image for FileField Django

How to find height and width of image if our model is defined as followclass MModel:document = FileField()format_type = CharField()and image is saved in document then how we can find height and width o…

Given a pickle dump in python how to I determine the used protocol?

Assume that I have a pickle dump - either as a file or just as a string - how can I determine the protocol that was used to create the pickle dump automatically? And if so, do I need to read the entir…

Get First element by the recent date of each group

I have following model in djangoBusiness ID Business Name Business Revenue DateHere is the sample data:Business ID | Business Name | Business Revenue | Date 1 B1 1000 …

remote: ImportError: No module named gitlab

I wrote gitlab hook with python. And added to post-receive hooks in gitlab server. When i push to remote origin server from my laptop, i get following error. But it works when i run script manually in …

Using an Access database (.mdb) with Python on Ubuntu [duplicate]

This question already has answers here:Working with an Access database in Python on non-Windows platform (Linux or Mac)(5 answers)Closed 7 years ago.Im trying to use pyodbc to access a .mdb on Ubuntu. …

Pandas Grouper by weekday?

I have a pandas dataframe where the index is the date, from year 2007 to 2017.Id like to calculate the mean of each weekday for each year. I am able to group by year: groups = df.groupby(TimeGrouper(A)…

Can I move the pygame game window around the screen (pygame)

In the game Im making, Im trying to move the window around the screen for a mini game (dont ask) and Ive tried what I saw own threads and only found 1x = 100 y = 0 import os os.environ[SDL_VIDEO_WINDOW…

mocking a function within a class method

I want to mock a function which is called within a class method while testing the class method in a Django project. Consider the following structure: app/utils.py def func():...return resp # outcome i…

After resizing an image with cv2, how to get the new bounding box coordinate

I have an image of size 720 x 1280, and I can resize it to 256 x 256 like thisimport cv2 img = cv2.imread(sample_img.jpg) img_small = cv2.resize(img, (256, 256), interpolation=cv2.INTER_CUBIC)Say I hav…

convert a tsv file to xls/xlsx using python

I want to convert a file in tsv format to xls/xlsx..I tried usingos.rename("sample.tsv","sample.xlsx")But the file getting converted is corrupted. Is there any other method of doing…