Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python django email set correct sender gunicorn

Tags:

python

django

This is my settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com' # mail service smtp
EMAIL_HOST_USER = '[email protected]' # email id
EMAIL_HOST_PASSWORD = 'sdjlfkjdskjfdsjkjfkds' #password
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = '[email protected]'

I am sending password reset email It works fine on localhost but on production server as sender I get webmaster@localhost

What do I do?

This is my urls.py

 url(r'^password_reset/$', auth_views.PasswordResetView.as_view(
        template_name='accounts/password_reset.html',
        email_template_name = 'accounts/email/password_reset.html',
        html_email_template_name = 'accounts/email/password_reset.html',
        subject_template_name = 'accounts/email/password_reset_subject.html'),
like image 782
user2950593 Avatar asked Sep 22 '18 17:09

user2950593


1 Answers

First I want to explain why you get specifically webmaster@localhost email.

Explanation

django.contrib.auth.forms.PasswordResetForm.save() function uses None as a from email address, webmaster@localhost is used by default, so you should just need to add DEFAULT_FROM_EMAIL property to your settings.py.

Solution

Connect to your production enviroment and run the django shell with python manage.py shell.

Now run the following commands.

# Check your enviroment, should say something like production.
import os
os.environ.get('DJANGO_SETTINGS_MODULE')

# Check if the DEFAULT_FROM_EMAIL property is set.
from django.conf import settings
settings.DEFAULT_FROM_EMAIL

With os.environ.get('DJANGO_SETTINGS_MODULE') you will get the settings file you are using for production, so if settings.DEFAULT_FROM_EMAIL isn't set just edit that file and add it with your email.

Recomendations

Use another email backend (mandrill, sendgrid, etc...) to ensure that your mails will reach the inbox and not the spam folder.

For this you can use django-anymail package.

References

How to create a password reset view in Django - simpleisbetterthancomplex.com

like image 198
GudarJS Avatar answered Sep 29 '22 18:09

GudarJS