Reputation: 351
I am getting a bad request error from sendgrid while trying to send out an email with attachments
import base64
import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import (
Mail, Attachment, FileContent, FileName,
FileType, Disposition, ContentId)
import urllib.request as urllib
import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
message = Mail(
from_email='[email protected]',
to_emails='[email protected]',
subject='Sending with Twilio SendGrid is Fun',
html_content='<strong>and easy to do anywhere, even with Python</strong>')
file_path = 'C:\\Users\\xxx\\OneDrive\\Desktop\\xxx.pdf'
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/pdf')
attachment.file_name = FileName('test_filename.pdf')
attachment.disposition = Disposition('attachment')
attachment.content_id = ContentId('PDF Document file')
message.attachment = attachment
try:
sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
response = sendgrid_client.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e.message)
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "C:\Program Files (x86)\python\lib\site-packages\sendgrid\sendgrid.py", line 98, in send
response = self.client.mail.send.post(request_body=message.get())
File "C:\Program Files (x86)\python\lib\site-packages\python_http_client\client.py", line 262, in http_request
self._make_request(opener, request, timeout=timeout)
File "C:\Program Files (x86)\python\lib\site-packages\python_http_client\client.py", line 178, in _make_request
raise exc
python_http_client.exceptions.BadRequestsError: HTTP Error 400: Bad Request
I am assuming it has something to do with the encoding of my pdf. How do I make sure it is base64 encoded?
Upvotes: 8
Views: 8674
Reputation: 226
Unfortunately, I can't just comment even though this isn't an actual "solution," per se. SendGrid is likely sending a more helpful message along with the BadRequest, which you can view by using HTTPError
like this:
from python_http_client.exceptions import HTTPError
sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
try:
response = sendgrid_client.send(message)
except HTTPError as e:
print(e.to_dict)
from: https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#error
Hopefully, this will help you actually fix the issue! The full list of error codes can be found here: https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html
I don't see anything particularly wrong with your code shown. I came across this question today trying to solve my own issue (definitely unrelated to what you're experiencing), but base64.b64encode(data).decode()
is exactly the same as what I have in my working example with a pdf.
Upvotes: 21