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.
EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = '[email protected]' EMAIL_HOST_PASSWORD = 'your-password' EMAIL_PORT = 587 EMAIL_USE_TLS = True
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
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!
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.
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 ...
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.
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]'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With