0

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```
  • No it does not, I've read that post and I get the same error – Angela Rubchinksy Dec 20 '21 at 19:12
  • first errpr `attribute error` is because in `e.message` e dont have `message` as attribute, you can change to to `print(str(e))` and for second one you need to see for which sendgrid version you are using the correct api or right format or not – sahasrara62 Dec 20 '21 at 19:18
  • I"m using sendgrid v6 and the code I've written is following their documentation – Angela Rubchinksy Dec 20 '21 at 19:28
  • 1
    try to follow this https://github.com/sendgrid/sendgrid-python/blob/main/use_cases/attachment.md , add a full file name with path and see what you got – sahasrara62 Dec 20 '21 at 19:33

0 Answers0