I would like to send email from a django view. I got it to work on my local machine with django development server with the following settings:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
My view has
from django.core.mail import send_mail
def my_view(request):
send_mail('subject','body','[email protected]',['[email protected]'],fail_silently=False)
return render(request, 'index.html')
When I run send_mail() from manage.py shell on my production server, the email is successfully sent. However, when the view is invoked in production (nginx + uwsgi + django), no email is sent, even though the view runs without error and returns the expected response. I see no error in error.log.
Please help me set correct permissions and configurations for nginx so that this works.
NOTE: This question is similar to Send_mail in Django, works in shell, works locally, not in view, which was never resolved.
EDIT: When I do
sudo -u www-data python manage.py shell
to run as the nginx user, i can still successfully send mail, which confuses me even more.
If the email is printed in the console then you have probably set
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
which prints the email instead of sending it.
If this line is included in an alternative (to the default) settings file then it could somehow be used by your deployment setup, whereas the shell continues to use the default settings file where the backend is either set to:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
or not set at all, in which case the default value is smtp
as well
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With