13

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 = 'user@gmail.com'
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','user@gmail.com',['recipient@gmail.com'],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.

Community
  • 1
  • 1
barlaso
  • 291
  • 2
  • 8
  • 1
    Possible duplicate of http://stackoverflow.com/questions/10384657/django-send-mail-not-working – Chirag Bhatia - chirag64 Apr 18 '15 at 17:32
  • 1
    In my case the email is successfully sent when using python shell. Thus this is unlikely to be a duplicate. I'm guessing a nginx permission or config issue. – barlaso Apr 18 '15 at 19:34
  • 1
    What does `send_mail` return in the view? – tjati Apr 26 '15 at 14:34
  • Just to follow through on @meinusch train of thought - it seems (at least for Django 1.7), that send_mail calls `EmailMessage.send`, which in turn calls `get_connection().send_messages()` which instantiates an email backend (default is `django.core.mail.backends.smtp.EmailBackend`) and then calls `EmailBackend.send_messages` which should return a number of messages sent or `None` (and in some case `0`) if something went wrong. – tutuDajuju Jun 19 '15 at 11:53
  • 2
    On that note, does your local environment settings differ from your production settings? If so, please update answer with differences. – tutuDajuju Jun 19 '15 at 11:54
  • 2
    Did you try using Email Message [Django Documentation](https://docs.djangoproject.com/en/1.8/topics/email/#the-emailmessage-class)? As send_mail are just wrappers around Email Message try using it. Also set `fail_silently=False` in the send command. If all else fails try this [link](http://stackoverflow.com/a/17338755/4359237) – sk1pro99 Jul 04 '15 at 08:20
  • There can be issues with the proxy setting on your production system. Is that a GAI error you are getting ? – cafebabe1991 Jul 17 '15 at 18:35
  • The issue could be connected with different settings being used. It could be that when you start django with uwsgi you use different settings from ones you use when you do `python manage.py shell`. I suggest you to check them on prod view. Usually it's changed in uwsgi.py – Yuri Kriachko Aug 28 '15 at 20:16
  • 1
    Also: is uWSGI running as www-data? – Árni St. Steinunnarson Sep 16 '15 at 14:33
  • I've got the same issue like you, it turns out that my uwsgi was installed in both vurtualenv and global, so it messed up my configuration, here is an [useful instruction](http://stackoverflow.com/questions/23073829/uwsgi-wont-reload-restart-or-let-me-run-service) for you to install uwsgi correctly. – pqteru Oct 12 '16 at 09:25
  • Exact same issue here. One can just exclude nginx & uwsgi issues via `python manage.py runserver`. However the problem remains, which means this is not relative to nginx or uwsgi. Moreover my `wsgi.py` doesn't introduce any new settings at all, same `settings.py` file – stelios May 28 '17 at 22:07

1 Answers1

1

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

stelios
  • 2,679
  • 5
  • 31
  • 41