Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django send_mail() works from shell but not in nginx production

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.

like image 646
barlaso Avatar asked Apr 18 '15 00:04

barlaso


1 Answers

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

like image 191
stelios Avatar answered Oct 13 '22 01:10

stelios