SendGrid, Error Sending First eMail using Python– AttributeError: ‘TypeError’ object has no attribute ‘message’

This blog post is brought to you by the developer of BitBudget. BitBudget is an automated budgeting app for Android and iOS which syncs with your bank account and helps you avoid overspending. If you’d like to quit living paycheck-to-paycheck and get a better handle on your finances, download it today! https://bitbudget.io

Just wanted to post a quick remedy/solution to an annoying bug I recently encountered getting started with SendGrid using their Python Library. The error message I received following their quickstart guide went something like…

AttributeError: ‘TypeError’ object has no attribute ‘message’

Okay, so here’s what I figured out. There are two things probably going on here if you are encountering this error and can’t figure out what the problem is. First, you need to remember that your first SendGrid emails are likely going to end up in your Spam folder! Especially when using Gmail. So make sure you check your spam folder as there is a good chance your emails have actually been sending despite the error message.

Second, there appears to be a bug in the try-except block posted in the official SendGrid Quickstart Guide for Python. The solution is to simply comment-out/disable the response and error message code. Here is what the quickstart guide should look like with the buggy parts commented-out:

 
# using SendGrid's Python Library
# https://github.com/sendgrid/sendgrid-python
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

message = Mail(
    from_email='from_email@example.com',
    to_emails='to@example.com',
    subject='Sending with Twilio SendGrid is Fun',
    html_content='<strong>and easy to do anywhere, even with Python</strong>')
try:
    sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
    response = sg.send(message)
    # Comment out the response, and print something else instead
    # print(response.status_code)
    # print(response.body)
    # print(response.headers)
    # print('SUCCESS!')
except Exception as e:
    # Comment out the e.message, and print something else instead
    # print(e.message)
    print("ERROR: PC LOAD LETTER") # Cryptic Error Message

PLEASE NOTE, the WordPress plugin which I’m currently using to post code snippets here on the blog has some issues so the line numbers and what not appear to be off on the example above. Will need to look for a better solution here in the future. Maybe Github Gist Embeds?

 

topherPedersen