Search code examples
djangosmtpsendgrid

Django / SendGrid - Password Reset - "The from address does not match a verified Sender Identity. "


I started integrating SendGrid into a django project.

The integration seems to work, as when a user registers a welcome email is sent to the user with [email protected] as the from_email

However, if a user request a new password, this produces a 500 error: "The from address does not match a verified Sender Identity."

The sender identify is obvioulsy verified because the other email signals work.

Questionning SendGrid on the question, they say that the email is blocked because its coming from webmaster@localhost. Which I find a bit odd.

I am confused, where in Django this default email address could be?

Here is my current settings.py for email:

EMAIL_FROM = "[email protected]"

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.sendgrid.net"
EMAIL_HOST_USER = "apikey
EMAIL_HOST_PASSWORD = config('SENDGRID_API_KEY', default='')
EMAIL_PORT = 587
EMAIL_USE_TLS = True

I dont have a specific view setup for password change except the following:

class PasswordsChangeView(PasswordChangeView):
    form_class = PasswordChangingForm
    success_url = reverse_lazy('home')

and, just in case this is an example of of the signals.py that triggers the welcome email:

@receiver(post_save, sender=UserProfile)
def created_user_profile(sender, instance, created, **kwargs):
    if created:
        subject = "title"
        message = render_to_string("main/email/welcome_email.html",{
            'username':instance.user,
            'email': instance.user.email,
               
        })
        from_email = settings.EMAIL_FROM
        print(f'from_email - {from_email} ')
        to_email = [instance.user.email]
        print(f'to_email - {to_email} ')
        email = EmailMessage(subject, message, from_email, to_email)
        print(f'email - {email} ')
        email.content_subtype = "html"
        email.send()

Solution

  • Add DEFAULT_FROM_EMAIL inside your settings.py. This will fix the issue.

    DEFAULT_FROM_EMAIL = "[email protected]"
    

    Also make sure from_email is set properly from sendgrid system and use the same email that setup there.