Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot set Django to work with smtp.gmail.com

Tags:

I've been trying to get django to work with gmail's smtp server to send mails but I always get this traceback. Any help will be most appreciated.

----- settings.py -----

EMAIL_HOST = 'smtp.gmail.com'  EMAIL_HOST_USER = '[email protected]'  EMAIL_HOST_PASSWORD = 'your-password'  EMAIL_PORT = 587  EMAIL_USE_TLS = True 

---- python shell -----

from django.core.mail import EmailMessage

email = EmailMessage('Mail Test', 'This is a test', to=['[email protected]'])

email.send()

Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/fiodorovich/Envs/fdict/lib/python2.7/site-packages/django/core/mail/message.py", line 251, in send return self.get_connection(fail_silently).send_messages([self]) File "/home/fiodorovich/Envs/fdict/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 86, in send_messages sent = self._send(message) File "/home/fiodorovich/Envs/fdict/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 104, in _send email_message.message().as_string()) File "/usr/local/lib/python2.7/smtplib.py", line 701, in sendmail raise SMTPSenderRefused(code, resp, from_addr) SMTPSenderRefused: (530, '5.7.0 Must issue a STARTTLS command first. z15sm10449686anl.15', 'webmaster@localhost') 

Edit: New errors when made the modification suggested by unni. The shell won't execute and I'm getting this error message

**EMAIL_HOST_USER  = '[email protected]'**  ^ SyntaxError: invalid syntax 
like image 645
la_f0ka Avatar asked Oct 24 '11 14:10

la_f0ka


People also ask

Why SMTP Gmail is not working?

In Google Mail, you must allow "less secure" apps access in order for your SMTP settings to work. There are two places this setting must be enabled: The first is here: https://myaccount.google.com/ under “Connected apps & sites.” Once enabled in both places, you're good to go!

How do I use Django to send an email from Gmail?

send_mail() method is imported from django. The send_mail() method handles the transmission of Emails. There are some important parameters of send_mail(), which we have defined here: subject: Contains the subject part of the Email. message: Contains the Email body.

Can I use Gmail for SMTP relay?

If your organization uses Microsoft Exchange or another SMTP service or server, you can set up the SMTP relay service to route outgoing mail through Google. You can use it to: Filter messages for spam and viruses before they reach external recipients. Apply email security and advanced Gmail settings to outgoing ...


2 Answers

Change your settings like this :

EMAIL_HOST = 'smtp.gmail.com'  EMAIL_HOST_USER = 'user'  EMAIL_HOST_PASSWORD = 'your-password'  EMAIL_PORT = 587  EMAIL_USE_TLS = True 

Then try:

python manage.py shell >>> from django.core.mail import EmailMessage >>> email = EmailMessage('Mail Test', 'This is a test', to=['[email protected]']) >>> email.send() 

This should return with the status 1, which means it worked.

like image 161
unni Avatar answered Oct 04 '22 00:10

unni


I have recently set this up and had a slightly different settings.py config.

Move:

EMAIL_USE_TLS = True  

to the top above EMAIL_HOST

Add:

DEFAULT_FROM_EMAIL = '[email protected]' SERVER_EMAIL = '[email protected]' 
like image 22
Mike Davies Avatar answered Oct 04 '22 00:10

Mike Davies