Home > Back-end >  Sending and email using python - Problem causes by last Google policy update (on less secure apps)
Sending and email using python - Problem causes by last Google policy update (on less secure apps)

Time:06-11

First of all I am a beginner in python and I am trying to learn how to send an email using python. All the tutorials on the web that i have red explain how to do it using Gmail. But, from 30/05/2022 (despite the fact that everybody is free to do whatever he wants with his account) google has a new politic that states:

To help keep your account secure, starting May 30, 2022 , Google will no longer support the use of third-party apps or devices that only ask for your username and password for you. sign in to your Google account.

source: enter image description here

So my question is there any other way to send an email using python (include to an email account belonging to an other company)?

Btw here my function to send an email:

def send_email_fct(filename, filepath, fromaddr, mdpfrom, toaddr):
"""" filename: file name to be sent with extension
     filepath: file path of the file to be sent
     fromaddr: sender email address
     mdpfrom: password of sender email address
     toaddr: receiver email address"""

msg = MIMEMultipart()  # instance of MIMEMultipart
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "data file"

body_email = "Body_of_the_mail"
msg.attach(MIMEText(body_email, 'plain'))

attachment = open(filepath, 'rb')  # open the file to be sent

p = MIMEBase('application', 'octet-stream')  # instance of MIMEBase
p.set_payload(attachment.read())
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)

msg.attach(p)  # attach the instance 'p' to instance 'msg'

s = smtplib.SMTP('smtp.gmail.com', 587)  # SMTP
s.starttls()
s.login(fromaddr, mdpfrom)

text = msg.as_string()

s.sendmail(from_email_addr, toaddr, text)  # sending the email

s.quit()  # terminating the session

And I get this error:

smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials c12-20020aa7d60c000000b0042be14040c1sm2612116edr.86 - gsmtp')

To fix this problem, i think that the only line that need to be change is this one:

s = smtplib.SMTP('smtp.gmail.com', 587)

If you know by what i can change it or if you see any other error, it will help me a lot! :-)

Thank you for your help

CodePudding user response:

Solved it by creating App password. You must got to Google account. Security tab, active 2 Step Verification. After this new option under "Signing in to Google" the "App passwords" option will be actived. Just create one app password and use as password to authenticate

CodePudding user response:

Here a more precise answer with all the main steps. I hope it will help other people.

1/Log in into your email account: https://myaccount.google.com

2/Then go to the security part

enter image description here

Be sure that you have turn on two steps verification and click on "App password"

enter image description here

3/ After select email and the corresponding device

enter image description here

4/It will generate a password that will look like this, it is this password that you have to use in your python script.

enter image description here

5/ This password will appear here and in this place too you will have the option to erase it (so it will be no more usefull to connect to your email account).

enter image description here

Hope it will help other people

  • Related