-2

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;
        server_name 172.17.110.205;
        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 / {
                proxy_pass http://172.17.110.205:8080;
        }
}

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

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=msrb_db
Group=users
WorkingDirectory=/home/msrb_db/msrb_db_application/
ExecStart=/usr/bin/gunicorn --bind 172.17.110.205:8080 --workers 3 --pythonpath /home/msrb_db/msrb_db_application/ msrb_db_application.wsgi:application

[Install]
WantedBy=multi-user.target
~

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_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 443
EMAIL_HOST_USER = 'abc@abc.com
EMAIL_HOST_PASSWORD = 'plplplp'
~

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.

manugupt1
  • 109
  • 2
  • 9

1 Answers1

5

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

Diamond
  • 8,791
  • 3
  • 22
  • 37
  • 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. – Diamond 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. – Diamond Nov 20 '15 at 07: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. – Diamond Nov 21 '15 at 18:57