Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending email with python. Google disables less secure apps

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.

like image 247
Ipha Avatar asked Jun 07 '26 13:06

Ipha


2 Answers

2-step verification turn on then head over to App password

enter image description here

After is generated passwords

import smtplib 
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as connection:  
    email_address = '[email protected]'
    email_password = 'App_Passwords_is_generated'
    connection.login(email_address, email_password )
    connection.sendmail(from_addr=email_address, to_addrs='[email protected]', 
    msg="subject:hi \n\n this is my message")
like image 196
h sha Avatar answered Jun 10 '26 03:06

h sha


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.

like image 27
Firelord Avatar answered Jun 10 '26 01:06

Firelord



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!