Home > Back-end >  Getting this error while trying to set SendGrid's email API with Django - AttributeError: '
Getting this error while trying to set SendGrid's email API with Django - AttributeError: '

Time:06-20

I am attempting to set up SendGrid with Django so that my website can send automated emails through SendGrid. So far, I haven't been able to send any emails.

My settings are configured like this:

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_BACKEND = 'sgbackend.SendGridBackend'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
with open(os.path.join(BASE_DIR, 'SENDGRID_API_KEY.txt')) as f:
    SENDGRID_API_KEY = f.read().strip()

EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = '[email protected]'
SENDGRID_SANDBOX_MODE_IN_DEBUG=True

I'm attempting to run this code to send an email:

import os
import sendgrid
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import *

message = Mail(
    from_email='[email protected]',
    to_email='[email protected]',
    subject='Sending with Twilio SendGrid is Fun',
    content='test123')
    try:
        sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
        response = sg.send(message)
        print(response.status_code)
        print(response.body)
        print(response.headers)
    except Exception as e:
        print(e.message)

And I'm getting this error:

2022-06-19 15:42:18,726: Internal Server Error: /register/
Traceback (most recent call last):
  File "/home/ryanctaylor/.virtualenvs/cts-virtualenv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/ryanctaylor/.virtualenvs/cts-virtualenv/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/ryanctaylor/.virtualenvs/cts-virtualenv/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/ryanctaylor/CTS/Registration/views.py", line 173, in register_view
    message = Mail(
  File "/home/ryanctaylor/.virtualenvs/cts-virtualenv/lib/python3.8/site-packages/sendgrid/helpers/mail/mail.py", line 31, in __init__
    personalization.add_to(to_email)
  File "/home/ryanctaylor/.virtualenvs/cts-virtualenv/lib/python3.8/site-packages/sendgrid/helpers/mail/mail.py", line 319, in add_to
    self.tos.append(email.get())
AttributeError: 'str' object has no attribute 'get'

Anyone know what I could be doing wrong here? Any help would be greatly appreciated.

CodePudding user response:

I would not only import, but use the helper classes. As discussed in the comments, the to_email parameter is called to_emails. I wonder if the indentation in your code might cause an issue here too. Here's the code I'd use:

import os
import sendgrid
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import *

message = Mail(
    from_email=From('[email protected]'),
    to_emails=To('[email protected]'),
    subject=Subject('Sending with Twilio SendGrid is Fun'),
    plain_text_content=Content('test123', Mime.text)
)
try:
    sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
    response = sg.send(message)
    print(response.status_code)
    print(response.body)
    print(response.headers)
except Exception as e:
    print(e.message)

There are other ways to build a mail which you can check out in this example code.

CodePudding user response:

Thanks for your help! I got it to work!

Turned out that I had installed an older version of sendgrid without knowing it. I think installing sendgrid-django created a dependency which caused me to end up with the older version of sendgrid.

  • Related