3

I have created python file that can send email using less secure apps turned on, but I need it off. How can I send email with 2FA?

# import simple mail transfer protocol library
import smtplib 

# import EmailMessage method
from email.message import EmailMessage

contacts = ['<email@email.com>', '<email2@email.com>']
EMAIL_ADDRESS = '<my_gmail>'
EMAIL_PASSWORD = '<my_gmail_password>'

# Create empty Email Message object
msg = EmailMessage()
msg['Subject'] = 'Automated python email sender 5'
msg['From'] = EMAIL_ADDRESS
msg['To'] = contacts
msg.set_content('<sample_content>')

# contact manager we will make sure our connection is closed automatically without us doing it manually
# 465 is the port number for plain SMTP_SSL
# SMTP_SSL means you do not have to use ehlo() ans starttls()
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:

  smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
  smtp.send_message(msg)

What should I add to make 2FA work?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
thatnicholascoder
  • 161
  • 1
  • 2
  • 9

3 Answers3

5

I believe you would need to set up an App passwords let you sign in to your Google Account from apps on devices that don't support 2-Step Verification. Learn more

Its basically something thats setup in the users Google account.

Another thing to look at would be Display captcha

if that doesn't work you might need to look into Xoauth2

if you require 2fa

If you want to support 2fa then you should not be using the SMTP server and should be going though the Gmail API and using Oauth2 which will prompt a user to connect via their google account and google will control the 2fa themselves. SMTP servers are not designed to handel 2fa

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • If you want to support 2fa then you should not be using the SMTP server and should be going though the Gmail API and using Oauth2 which will prompt a user to connect via their google account and google will control the 2fa themselves. – Linda Lawton - DaImTo Sep 09 '20 at 06:01
  • Thanks for this idea. I will try to create python that uses gmail API and Oauth2 not SMTP. Thank you – thatnicholascoder Sep 09 '20 at 06:03
  • You should check the cost of verification of an app using a gmail scope. It can be quite a time consuming and expensive process this should be considered when budgeting your new app. – Linda Lawton - DaImTo Sep 09 '20 at 06:26
  • I did not know it will cost much. I am really starting to create python file that sends email that uses 2FA from scratch and really have 0 to no idea if what I am doing will work. I hope to have documentation on this. Thanks for that comment – thatnicholascoder Sep 09 '20 at 06:30
  • 1
    https://support.google.com/cloud/answer/7454865?hl=en there is a lot of documentation on it. If you intend for others to use this and its not just for your use only then your really should checkup on how much it will cost you. – Linda Lawton - DaImTo Sep 09 '20 at 06:32
1

Under security you have to generate a password for your application:

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('demo@gmail.com', 'password generated by google')

server.sendmail('demo@gmail.com', 'demo@icloud.com', 'Mail sent from program')
print('Mail Sent')

as you can see, you enter your email but instead of typing your own password you enter the password generated by google for your application

Josue Gisber
  • 356
  • 2
  • 5
0

Try setting up App Password for your Gmail. and used that app password for smtp login.

This will help Set Up APP-Password

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32588210) – n4321d Sep 02 '22 at 16:11