I've been trying for some time to be able to receive emails with Django from a website I'm developing. Now, I haven't deployed it yet, and I am using the Django Development Server, and I don't know if this affects it.
Here is my settings.py configuration:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWROD = 'my_password'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
DEFAULT_TO_EMAIL = EMAIL_HOST_USER
Then, through the root folder of the project I run python manage.py shell
, and type the following:
>>> import django
>>> from django.conf import settings
>>> from django.core.mail import send_mail
>>> send_mail('test', 'test', settings.EMAIL_HOST_USER, [settings.EMAIL_HOST_USER], fail_silently=False)
And what I receive back is the following error:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/paulo/anaconda3/envs/MyDjangoEnv/lib/python3.6/site-packages/django/core/mail/__init__.py", line 62, in send_mail
return mail.send()
File "/home/paulo/anaconda3/envs/MyDjangoEnv/lib/python3.6/site-packages/django/core/mail/message.py", line 348, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/paulo/anaconda3/envs/MyDjangoEnv/lib/python3.6/site-packages/django/core/mail/backends/smtp.py", line 111, in send_messages
sent = self._send(message)
File "/home/paulo/anaconda3/envs/MyDjangoEnv/lib/python3.6/site-packages/django/core/mail/backends/smtp.py", line 127, in _send
self.connection.sendmail(from_email, recipients, message.as_bytes(linesep='\r\n'))
File "/home/paulo/anaconda3/envs/MyDjangoEnv/lib/python3.6/smtplib.py", line 866, in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (530, b'5.5.1 Authentication Required. Learn more at\n5.5.1 https://support.google.com/mail/?p=WantAuthErrorv11sm8729581qkl.88 - gsmtp', '[email protected]')
I have enabled the option to allow less secure applications.
I tried, when using 2-step verification, to input, instead of EMAIL_HOST_PASSWORD = "my_password", to input EMAIL_HOST_PASSWORD = 'myapplicationpassword' provided by google. It didn't work.
Then I have disabled the 2-step verification and changed from the application password back to my actual password, and the error remains.
Am I doing something wrong? Has Google somehow blocked these types of application? Would I do better if I ran my own mail server?
Make sure that you do the following two things:
Make sure the settings imported or password set and/or retrieved is in the same terminal shell:
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASSWORD')
export EMAIL_PASSWORD="xxxx xxxx xxxx xxxx"
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