SMTP library Python3 : Less secure app access

Viewed 450

As of May 30th Google has removed allowing less secure app access for G-Mail. I am using SMTP library to send emails from my flask website and as this method requires the feature that Google has just removed, I am stuck. I am looking for any work-arounds or alternate solutions to this problem.

1 Answers

The solution is simple and does not require much change:

  1. Turn on 2-Step Verification in your google account. This step is required as Google only allows generating passwords for apps on accounts that have 2-Step Verification enabled.
  2. Go to generate apps password (https://myaccount.google.com/apppasswords) and generate a password for your app.

  1. Simply use your gmail username (your_mail@gmail.com) and the password generated in your python application.

I have tested what I just attached to you with the following script:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
mail_content = '''
Hello Stack Overflow!!!
'''

sender_address = 'your_mail@gmail.com'
sender_pass = 'generated_password'
receiver_address = 'your_mail@gmail.com'

message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = 'A test mail sent by Python.'   

message.attach(MIMEText(mail_content, 'plain'))
session = smtplib.SMTP('smtp.gmail.com', 587)
session.starttls()
session.login(sender_address, sender_pass)
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
print('Mail Sent')

And it worked without problems: enter image description here

Related