Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django send_mail SMTPSenderRefused 530 with gmail

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?

like image 652
Paulo Soares Avatar asked Mar 08 '23 06:03

Paulo Soares


1 Answers

Make sure that you do the following two things:

  1. Go to App passwords for 2-factor authentication, or look into "Less secure app access" if you're not using MFA. The password google provides when setting up as is a 16 digit code that includes spaces, they count.

Custom app generated password

  1. 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"
    
like image 118
paulywill Avatar answered Mar 15 '23 11:03

paulywill