Sendgrid-python: Get AttributeError: 'str' object has no attribute 'get' when sending email with Django

Created on 31 May 2018  路  2Comments  路  Source: sendgrid/sendgrid-python

I am sending an email to regenerate password to newly created Users.
The argument user takes an instance of User.

def password_mail(user):
    user_email = user.email
    first_name = user.first_name
    token = token_generator.make_token(user)
    uid = urlsafe_base64_encode(force_bytes(user.pk))
    domain = Site.objects.get_current().domain
    sg = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY)
    from_email = Email('[email protected], 'My Domain')
    to_email = user_email
    subject = 'Account Creation'

    ctx = {
        'first_name': first_name,
        'domain': domain,
        'uid': uid,
        'token': token,
        'validlink': True
    }

     html_content = render_to_string('accounts/user_mail_password.html', ctx)

    content = Content("text/html", html_content)
    mail = Mail(from_email, subject, to_email, content)
    response = sg.client.mail.send.post(request_body=mail.get())

I get no mail sent and this error message. Thanks.

  File "/Users/aureliendebord/Python/myapp/app/utils.py", line 76, in password_mail
    mail = Mail(from_email, subject, to_email, content)
  File "/Users/aureliendebord/Python/myapp/myvenv/lib/python3.6/site-packages/sendgrid/helpers/mail/mail.py", line 31, in __init__
    personalization.add_to(to_email)
  File "/Users/aureliendebord/Python/myapp/myvenv/lib/python3.6/site-packages/sendgrid/helpers/mail/mail.py", line 523, in add_to
    self._tos.append(email.get())
AttributeError: 'str' object has no attribute 'get'

Technical details:

  • Python Version: 3.6.5
question

Most helpful comment

My bad. Forget to use the Email() on user_email.

Closed.

All 2 comments

My bad. Forget to use the Email() on user_email.

Closed.

Thanks for providing an update @aureliendebord!

Was this page helpful?
0 / 5 - 0 ratings