Send HTML and attachment emails from Python using Gmail SMTP with TLS encryption.
pip install secure-smtplib # stdlib smtplib is included in PythonNo external packages needed — uses Python's built-in smtplib, email, and ssl modules.
Gmail requires an App Password (not your regular account password):
- Enable 2-Factor Authentication on your Google account
- Go to Google Account → Security → App Passwords
- Generate a password for "Mail" → copy it
- Paste it as
SENDER_PASSWORDin the script
SENDER_EMAIL = "[email protected]"
SENDER_PASSWORD = "your_16_char_app_password"
RECEIVER_EMAIL = "[email protected]"Run:
python email.py- Connects to
smtp.gmail.com:587 - Upgrades to TLS via
STARTTLSwith a secure SSL context - Attaches an image (
imgName.jpg) alongside the text body