1 Add "App password"
- Go to Google account "Security", navigate to "Signing in to Google" and click "App passwords".
- Then select a type, here we use "Mail" and custom the device name.
- Type device name as "airflow", or you can choose any other name.
- Then you will get the app password here, COPY it.
2 Turn on IMAP
Login to your email, click "Settings" icon at the right top, and go to "Forwarding and POP/IMap", Click "Enable IMAP".
3 Python code
import smtplib
import ssl
from email.message import EmailMessage
def send_email():
msg = EmailMessage()
port = 587
host = "smtp.gmail.com"
sender = "<you gmail email address>"
password = "<app password>"
msg['From'] = sender
msg['To'] = ["<receiver1>", "<receiver2>"]
msg['Subject'] = "Test email subject"
msg.set_content("Test email content")
with smtplib.SMTP_SSL(host, port, timeout=5) as server:
server.login(sender, password)
server.send_message(msg)
Every thing will work good.
Reference: