Django send_mail SMTPSenderRefused 530 with gmail

99封情书 提交于 2020-07-19 08:29:07

问题


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 = 'my_email@gmail.com'
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', 'my_email@gmail.com')

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?


回答1:


If you are using Google SMTP, Google has introducted a new security feature which blocks use of SMTP directly. In order to use Google SMTP, you need to sign into your Google account and enable access to less secure app. This option os present under Security Checkup under My Account. See the screenshot below.




回答2:


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.

  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"
    


来源:https://stackoverflow.com/questions/45805056/django-send-mail-smtpsenderrefused-530-with-gmail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!