Setting up Gmail SMTP with Django for email delivery issues

I’m facing an issue with sending emails using Django with Gmail’s SMTP service. It always returns a status of 0, indicating that the email hasn’t been sent successfully.

I’ve tried looking up various solutions on Stack Overflow and followed the instructions for setting up the SMTP server correctly, but it still doesn’t work.

Here are my email settings in the Django configuration:

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'my_password'
DEFAULT_FROM_EMAIL = '[email protected]'
DEFAULT_TO_EMAIL = '[email protected]'

And this is the code I am using to send the email:

from django.conf import settings
from django.core.mail import send_mail

print("Initiating email sending")
subject = 'Email Test'
message_body = 'This is just a test email.'
sender = settings.DEFAULT_FROM_EMAIL
recipients = [settings.DEFAULT_TO_EMAIL]

result = send_mail(subject, message_body, sender, recipients)
print(f"Email sending result: {result}")
print("Email process finished")

I am operating this on an EC2 instance with Ubuntu and running Apache as my web server. The send_mail function continues to return 0, which means it isn’t sending the email.

What extra configurations do I need to consider for using Gmail’s SMTP with Django? I would really appreciate any guidance on this.

Your EC2 instance is probably blocking outbound SMTP on port 587. AWS blocks email sending by default to prevent spam. You’ll need to submit a support case asking them to lift the email restrictions. Also check that your security groups allow outbound traffic on port 587. I ran into this exact same thing - worked fine locally but died on EC2 until I got the email restrictions lifted. That status 0 you’re seeing is what happens when the connection gets blocked at the network level, not an auth problem.

u might be using ur regular gmail password instead of an app password. google killed basic auth for smtp, so u need an app-specific password in ur google account security settings. enable 2fa first; u can’t create app passwords without it. that’s why it’s failing silently.

I’ve hit this exact issue debugging email problems on production. Status 0 usually means Django can’t connect to SMTP at all - not an auth failure, which would throw an exception. Wrap your send_mail in try-except to catch SMTPException or ConnectionError that might be getting swallowed silently. Add EMAIL_TIMEOUT = 10 to your settings so connections don’t hang. If you’re still getting status 0 after proper exception handling, it’s probably network issues. Test SMTP connectivity directly with telnet or openssl s_client from your EC2 instance - see if you can reach Gmail’s servers before Django tries.