I am trying to send email using python. My code was working fine before Google disabled 'less secure apps'. My email address and password are both correct.
server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
serverEmail = "EMAILADDRESS"
serverPw = "QWERTY"
server.login(serverEmail, serverPw)
subject = "Rejection"
body = "Hi! You've been unfortunately declined access to our system."
message = f'Subject: {subject}\n\n{body}'
server.sendmail("EMAILADDRESS", doctorEmail['email'], message)
server.quit()
I get this error now:
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted.
I get this error when i use server.starttls()
:
smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server.
CodePudding user response:
This is working for me. You need to generate an app password for this. See https://support.google.com/accounts/answer/185833?hl=en
import smtplib as smtp
connection = smtp.SMTP_SSL('smtp.gmail.com', 465)
email_addr = '[email protected]'
email_passwd = 'app_password_generated_in_Google_Account_Settings'
connection.login(email_addr, email_passwd)
connection.sendmail(from_addr=email_addr, to_addrs='[email protected]', msg="Sent from my IDE. Hehe")
connection.close()
For some reason, all of my emails are ending up in SPAM folder of the recipient account though.