Every working day you repeat the same routine: finish the numbers, run the report, then copy the results one by one into an email, attach the file and send it to your boss or client. These steps are slow and error-prone — especially the days you forget the attachment or send the wrong version. Today, with a dozen lines of Python, you can turn the whole flow into a one-click auto-send, attachments and formatting included.
Why use Python to send email?
Python ships with a built-in module called smtplib. You don't need to install anything — it connects straight to your mail server and sends. Pair it with the email module and you can auto-build the subject, body and attachments. The whole thing runs in one shot, and you can combine it with the "scheduled task" trick from earlier articles to send mail automatically every day.
Preparation: get your email password
For security, Gmail, Outlook and most business email no longer accept your normal login password for scripts. Instead you enable an "App Password":
- Gmail: open "Google Account" → "Security" → "2-Step Verification" → "App passwords", choose "Mail" and "Other", then copy the 16-character password.
- Outlook / business email: enable an "App Password" in account security settings, or use the SMTP details your IT team gives you.
In the code below we store the password in an "environment variable" — never hard-code it, because if your code is ever seen by someone else, your password is exposed.
Step 1: send a basic email
Create a new file send_email.py and paste in this code:
Line by line
- import smtplib — Python's built-in mail sender; nothing to install.
- os.environ["EMAIL_PASSWORD"] — reads the password from an environment variable, so it never shows in the code.
- EmailMessage() — builds an email: subject, sender, recipient and body.
- SMTP_SSL("smtp.gmail.com", 465) — opens an encrypted connection to Gmail's server (465 is the SSL port).
- server.send_message(msg) — actually sends it.
Step 2: add attachments and multiple recipients
In practice a daily report usually needs an Excel/PDF attachment and several recipients. Tweak the code above:
That's a daily report with an attachment, done. Each run, just swap daily_report.xlsx for the latest file.
Step 3: schedule it so it runs on its own
Want it to send automatically at 9am every morning? On Windows, use Task Scheduler — same as the backup article: trigger "Daily", program python, and point the argument at the full path of your send_email.py.
Or, inside Python, use the schedule package — one install and a few lines give you an always-on scheduler:
Safety tips: three mistakes to avoid
- Never hard-code the password — use an environment variable or a separate config file.
- Don't send to the wrong people — manage recipients in a single list so it's easy to review.
- Test before going live — send a test to your own other email first, confirm the format and attachment, then go production.
Summary
Automating email with Python compresses your daily "finish the numbers → send the mail" routine into one click — and it can even run on a schedule. This is just the start: with the same smtplib approach you can auto-send monthly statements, payment reminders and event invitations — all the same pattern.