My program that sends an e-mail isn't working ;(

Viewed 64

So here is my code:


import smtplib

gmail_user = 'GMAIL'
gmail_password = 'PASSWORD'

sent_from = gmail_user
to = ['GMAIL2']
subject = 'E-Mail sent from Python'
body = 'This is an E-Mail sent from Python. VERY COOL!'

email_text = """\
From: %s
To: %s
Subject: %s

%s
""" % (sent_from, ", ".join(to), subject, body)

try:
    smtp_server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    smtp_server.ehlo()
    smtp_server.login(gmail_user, gmail_password)
    smtp_server.sendmail(sent_from, to, email_text)
    smtp_server.close()
    print ("Email sent successfully!")
except Exception as ex:
    print ("Something went wrong….",ex)

And after I run it, the program shows this:

Something went wrong…. (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials n18-20020a056a0007d200b004fdac35672fsm3949185pfu.68 - gsmtp')

I tried following the instructions on the link it outputs, but it shows the same thing. Can someone help me out?

1 Answers

You have to put your gmail and your gmail password on the gmail_user and gmail_password, and put your gmail on the to = [yourgmail].

Related