I am trying to send an email from my django application.

However, this does not work on my NGINX (used for static resources) + GUNICORN Server.

But, the same works on django's default webserver. I have not setup a mail server per se as of now.

I am not sure what the problem is.

Let me explain the entire situation as the question was ill explained.

If I send an email from my web app which is in Django using the default debugging WSGI,

and the following code

from django.core.mail import send_mail

send_mail('Subject here', 'Here is the message.', 'from@example.com',
    ['to@example.com'], fail_silently=False)

I am able to send the email.

However, I have setup an nginx server and a gunicorn wsgi together. I am not able to send mails.

My nginx.conf looks like this

server {
        listen 80;
        listen 443;
        listen 587;
        client_max_body_size 0M;

        access_log /home/msrb_db/msrb_db_application/access.log;
        error_log /home/msrb_db/msrb_db_application/error.log;

        location /static {
                root /home/msrb_db/msrb_db_application;

        location / {

And I have a gunicorn server as a systemd file which works as follows

Description=gunicorn daemon

ExecStart=/usr/bin/gunicorn --bind --workers 3 --pythonpath /home/msrb_db/msrb_db_application/ msrb_db_application.wsgi:application


I have also tried this using the other api for gmail using the EmailMessageClass

My settings.py are as follows

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'abc@abc.com

I am not sure what I am missing over here. Any help is appreciated.

Django Logs:

Hi, so I took out the logs from Django and I found no exception related to Email

/post/test_csv.zip' already exists
Nov 19 10:06:10 msrbdb gunicorn[58290]: sending email
Nov 19 10:06:10 msrbdb gunicorn[58290]: email sent
Nov 19 10:06:10 msrbdb gunicorn[58290]: Exception SystemExit: 0 in <module 'threading' from '/usr/lib64/python2.7/threading.pyc'> ignored

And, NGINX has no errors

Thanks a lot.

  • Please try with: EMAIL_PORT = 587 – Diamant Nov 19 '15 at 10:31

The port number (EMAIL_PORT = 443) you are using to connect to smtp host is wrong. It won't ever work. TCP Port 443 is reserved for HTTPS. Please try using Port 465 (SSL required) or Port 587 (TLS required) as prescribed by google.

You are also using a standard SMTP port 587 listen 587; in your nginx.conf. You should remove this.

See the google reference here for details: https://support.google.com/a/answer/176600?hl=en

  • Ok.. I missed it. I have changed the port, but I it does not work. – manugupt1 Nov 19 '15 at 15:48
  • 1
    You are also using a standard SMTP port 587 listen 587; in your nginx.conf. You need to remove this. – Diamant Nov 19 '15 at 21:32
  • @manugupt1, If you are sure that mails are sent but not recieved, can have a look at spam folder at reciever address too. – Diamant Nov 20 '15 at 7:58
  • Hi so I fiured it out. Since I was using Gmail,I had to turn on less secure option, so that my mails could be sent and received. – manugupt1 Nov 21 '15 at 18:41
  • @manugupt1, Are you using port 25? Well, that's the most commonly used smtp port. Will also work as documentd in the link. – Diamant Nov 21 '15 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.