Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Error 530 5.7.0 Authentication Required when sending email using Django Python

Tags:

I am trying to send emails with G-Suite account using python in Django. As Google stoped the less secure App option for the new applications, I have to use Oauth2. But when I start to send emails via smtplib, the ERROR:

smtplib.SMTPSenderRefused: (530, b'5.7.0 Authentication Required. Learn more at\n5.7.0  https://support.google.com/mail/?p=WantAuthError q4sm8418287pfl.175 - gsmtp'

And after looking up the reference, it means "530, "5.7.0", Must issue a STARTTLS command first." However, I have added "server.starttls()". Could someone help me? Many thanks.

server = smtplib.SMTP('smtp.gmail.com', port=587)
server.ehlo('test')
server.starttls()
server.docmd('AUTH', 'XOAUTH2 ' + base64.b64encode(auth_string.encode()).decode("utf-8"))
server.sendmail(from_addr, to_addr, msg.as_string())
server.quit()
like image 446
gkcsjk Avatar asked Jan 28 '20 10:01

gkcsjk


1 Answers

Per this https://stackabuse.com/how-to-send-emails-with-gmail-using-python/#:~:text=As%20for%20the%20actual%20Python,com'%2C%20465)%20server., you probably have 2-step verification turned on.

To enable your web apps to send SMTP's from/to your gmail, you will need to create an app-specific password for less secure apps.

  1. Go to your Google Account.
  2. Select Security.
  3. Under "Signing in to Google," select App Passwords. You may need to sign in. If you don’t have this option, it might be because: 2-Step Verification is not set up for your account. 2-Step Verification is only set up for security keys. Your account is through work, school, or other organization. You turned on Advanced Protection.
  4. At the bottom, choose Select app and choose the app you are using and then Select device and choose the device you’re using and then Generate.
  5. Take this 16-character code in the yellow bar and place it in your environment variables as your "EMAIL_HOST_PASSWORD"
like image 143
deesolie Avatar answered Sep 27 '22 21:09

deesolie