Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

I am trying to send email from Django by setting up Gmail smtp. But every time it is returning me 0 status. I have searched different relevant answers in StackOverflow and I am setting up the smtp server the same way but still, it is not sending an email. Below is my setting file

EMAIL_USE_TLS = True

EMAIL_HOST = 'smtp.gmail.com'

EMAIL_PORT = 587

EMAIL_HOST_USER = 'my gmail account'

EMAIL_HOST_PASSWORD = 'my gmail account password'

DEFAULT_FROM_EMAIL = 'my gmail account'

DEFAULT_TO_EMAIL = 'to email'

Below is my code

from django.conf import settings

from django.core.mail import send_mail

print "Sending Email"

mail_title = 'Test Email'

message = 'This is a test email.' 

email = settings.DEFAULT_FROM_EMAIL

recipients = [settings.DEFAULT_TO_EMAIL]

print send_mail(mail_title, message, email, recipients, settings.EMAIL_HOST_USER, settings.EMAIL_HOST_PASSWORD) 

print "Email Sent"

But every time it prints status 0 which means email is not sent. About the environment, I am running this code on Amazon EC2 instance which has ubuntu as an OS and Apache as a server.

Do I need to do additional setups for sending email through Gmail smtp?? Much appreciate your help Thanks in advance

1 Answer

0 votes
by (44.4k points)

The gmail.smtp setup looks correct but the calling function send_email function correctly. That is the reason why it is failing. Try this code:

import django

from django.conf import settings

from django.core.mail import send_mail

send_mail('Mail Subject', 'Mail content', settings.EMAIL_HOST_USER, ['[email protected]'], fail_silently=False)

Related questions

0 votes
4 answers
Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...