Chan Jing Hong
Chan Jing Hong

Reputation: 2461

Unable to log in gmail smtp server in python

I following through a tutorial to send an email through python, but it returns an SMTPAuthentication error. Below is my source code:

import smtplib, getpass

# Connect to smtp server
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
print("Successfully connected to gmail.")

# User login
print("Please Login")
gmail_user = str(raw_input("Enter your email: "))
gmail_pwd = getpass.getpass("Enter your password: ")
smtpserver.login(gmail_user, gmail_pwd)

# Destination email
to = str(raw_input("Enter the email you would like to send to: \n"))

# Message contents
header = "To:" + to + "\n" + "From: " + gmail_user + "\n" + "Subject:testing \n"
print header
msg = header + '\n this is test message\n\n'

# Begin sending email
smtpserver.sendmail(gmail_user, to, msg)
print "Success!"

# Close smtpserver
smtpserver.close()

Can anyone tell me what's wrong? I'm pretty sure I typed in the correct email and password. Thanks!

Upvotes: 2

Views: 1377

Answers (1)

PascalVKooten
PascalVKooten

Reputation: 21451

I'm guessing this is the error:

SMTPAuthenticationError: Application-specific password required

You can also try the standard settings of yagmail:

import yagmail
yag = yagmail.Connect(gmail_user, gmail_pwd)
yag.send(to, 'testing', 'this is test message')

yagmail's goal is to make it easy to send gmail messages without all the hassle. It also provides a list of common errors in the documentation.

It also suggests holding the user account in keyring so you never have to write username/pw again (and especially not write it in a script); just run once yagmail.register(gmail_user, gmail_pwd)

Hope to have helped, feel free to check the documentation

Upvotes: 1

Related Questions