简体   繁体   中英

Getting this error while trying to set SendGrid's email API with Django - AttributeError: 'str' object has no attribute 'get'

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 = 'dac@projectado.com'
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='dac@projectado.com',
    to_email='taylor.ryanc@gmail.com',
    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.

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('dac@projectado.com'),
    to_emails=To('taylor.ryanc@gmail.com'),
    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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM