I'm trying to figure out how to add attachments to my email I am sending through sendgrid. I have an .xlsx file in the same directory however it won't get attached. I currently have my code like this:
with open(f'name-{filenumber}.xlsx', 'rb') as fd:
encoded = base64.b64encode(fd.read())
attachment = Attachment()
attachment.content = (encoded, 'utf-8')
attachment.filename = f'name-{filenumber}.xlsx'
message = Mail(
from_email='email.@email.com',
to_emails= os.getenv("RECIPIENT_EMAIL"),
subject='Sending with Twilio SendGrid is Fun',
html_content='<strong>and easy to do anywhere, even with Python</strong>')
message.add_attachment(attachment)
try:
sg = SendGridAPIClient('SENDGRID_API_KEY')
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e.message)
But I keep getting this error AttributeError: 'BadRequestsError' object has no attribute 'message'
Along with this:
python_http_client.exceptions.BadRequestsError: HTTP Error 400: Bad Request```