2

I am attempting to send an email, but I run into this error: smtplib.SMTPAuthenticationError: (534, b'5.7.9 Application-specific password required. Learn more at\n5.7.9 https://support.google.com/mail/?p=InvalidSecondFactor d2sm13023190qkl.98 - gsmtp')

In the web URL i dont see anything super useful, would anyone have any tips? For SO purposes I left the email account passwords as test versus sharing my person info..

import smtplib
import ssl


# User configuration
sender_email = 'test@gmail.com'
receiver_email = 'test@gmail.com'
password = 'test'



# Email text
email_body = '''
    This is a test email sent by Python. Isn't that cool?
'''

# Creating a SMTP session | use 587 with TLS, 465 SSL and 25
server = smtplib.SMTP('smtp.gmail.com', 587)
# Encrypts the email
context = ssl.create_default_context()
server.starttls(context=context)
# We log in into our Google account
server.login(sender_email, password)
# Sending email from sender, to receiver with the email body
server.sendmail(sender_email, receiver_email, email_body)
print('Email sent!')

print('Closing the server...')
server.quit()
bbartling
  • 3,288
  • 9
  • 43
  • 88
  • "Application-specific password required" – do you have one? – mkrieger1 Mar 31 '20 at 17:44
  • 1
    You should consider using the formal [gmail API](https://developers.google.com/gmail/api/quickstart/python) – Cory Kramer Mar 31 '20 at 17:45
  • i runned the example in this page in my gmail account and it worked: https://julien.danjou.info/sending-emails-in-python-tutorial-code-examples/ – Jasar Orion Mar 31 '20 at 17:47
  • currently Gmail is more restricted then other mail servers and every application needs own password to access mails. – furas Mar 31 '20 at 18:29

2 Answers2

7

I tried my best... I think this should work!

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

email = "test@gmail.com" # the email where you sent the email
password = "yourPassword"
send_to_email = "yourEmail@gmail.com" # for whom
subject = "Gmail"
message = "This is a test email sent by Python. Isn't that cool?!"

msg = MIMEMultipart()
msg["From"] = email
msg["To"] = send_to_email
msg["Subject"] = subject

msg.attach(MIMEText(message, 'plain'))

server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, send_to_email, text)
server.quit()
ŁeZard
  • 101
  • 1
  • 4
1

You must allow the "Less secure apps" in Google configurations.

Here is the link of another thread about it : Link

Syb
  • 39
  • 3