I\'m trying to attach a PDF file to my email sent with sendgrid.
Here is my code :
sg = sendgrid.SendGridAPIClient(apikey=os.environ.get(\'SENDGRID_A
I found a solution. I replaced this line :
pdf = open(pdf_path, "rb").read().encode("base64")
By this :
with open(pdf_path, 'rb') as f:
data = f.read()
encoded = base64.b64encode(data)
Now it works. I can send encoded file in the set_content :
attachment.set_content(encoded)
Note: The answer above works for Sendgrid v2 or lower. For v3 and up use:
encoded = base64.b64encode(data).decode()