Reputation: 71
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()
Upvotes: 7
Views: 45891
Reputation: 1062
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.
Upvotes: 5
Reputation: 449
EMAIL_BACKEND="django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST_USER ='[email protected]'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_PASSWORD = 'yourpassword'
EMAIL_USE_TLS = True
Add this on your settings. After that :
python manage.py shell
You will enter to Python shell. Then:
from django.core.mail import send_mail
Write this command on shell and after that:
send_mail(
"django test mail",
"this is django test mail",
"[email protected]",
"[email protected]",
fail_silently=False
)
If output is equal to:
1
, then you send email through your Gmail.
Upvotes: 0