sendgrid / sendgrid-python

The Official Twilio SendGrid Python API Library
https://sendgrid.com
MIT License
1.55k stars 714 forks source link

Corrupted Attachment (Excel) #886

Closed rriley99 closed 4 years ago

rriley99 commented 4 years ago

Issue Summary

Hello folks, I'm attempting to attach an Azure Blob to an email for automated reporting. I thought I'd start from the bottom and send the excel sheet from my local before bring Azure into the situation. Taken the steps here and imported my information into it. I've gotten a file to attach and an email to send no problem, but when I select the preview: image And when I open directly: image

Steps to Reproduce

  1. Use-case
  2. Opened a local xlsx and ecoded
  3. Changed file_path, attachment.file_type, and attachment.file_name

Code Snippet

import base64
import os
from sendgrid.helpers.mail import (
    Mail, Attachment, FileContent, FileName,
    FileType, Disposition, ContentId)
from sendgrid import SendGridAPIClient

message = Mail(
    from_email='rriley@workforcelogiq.com',
    to_emails='rriley@workforcelogiq.com',
    subject='Sending with Twilio SendGrid is Fun',
    html_content='<strong>and easy to do anywhere, even with Python</strong>')

file_path = os.path.join('.','Accrual Sample.xlsx')
with open(file_path, 'rb') as f:
    data = f.read()
    f.close()

encoded = base64.b64encode(data).decode()

attachment = Attachment()
attachment.file_content = FileContent(encoded)
attachment.file_type = FileType('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
attachment.file_name = FileName('Accrual Sample.xlsx')
attachment.disposition = Disposition('attachment')
attachment.content_id = ContentId('Example Content ID')
message.attachment = attachment

sendgrid_client = SendGridAPIClient(sg_key)
response = sendgrid_client.send(message)

print(response.status_code)
print(response.body)
print(response.headers)

Technical details:

rriley99 commented 4 years ago

Marking as closed per user error.