Automate your email workflow with Python. You'll use the built-in smtplib library to send plain text emails, styled HTML emails, and emails with file attachments — all without installing anything extra.
A Python script that sends a professional HTML email from your Gmail account — with a subject line, formatted body, and an attached PDF or image. Ready to plug into any automation pipeline.
Email automation powers everything from welcome emails to weekly reports to alert systems. Python's smtplib is built-in — no pip install needed. Let's go.
Gmail requires an App Password instead of your regular password when using SMTP. Here's how to set it up:
.env file.Create email_sender.py. Here's the minimum working email script:
Run this with python email_sender.py. Check your inbox — it should arrive within seconds.
Plain text works, but HTML emails look professional. Use MIMEMultipart to send both versions (email clients pick the best one):
The 'alternative' type tells email clients to show the HTML version if they support it, or fall back to plain text.
Send a file along with the email using MIMEBase:
.csv, .xlsx, .png or any other file type. The octet-stream MIME type works for everything.Loop through a list of recipients to send personalised bulk emails:
Never hardcode passwords in your script. Use environment variables instead:
Set them in your terminal before running:
.env file with the python-dotenv library (pip install python-dotenv) to load variables automatically.Use Python's schedule library to send emails on a timer:
Install it first: pip install schedule. Run the script and it will send your email automatically at 8am every day while it's running.
You can now send plain text emails, styled HTML emails, file attachments, and bulk personalised emails — all from Python. This is the foundation of automated reports, alert systems, and marketing tools.
Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.