Send Emails Automatically Using Python (Gmail Example)


Tired of writing the same emails over and over? Want to send daily reports, reminders, or follow-ups without touching your inbox?
Python can do that — in seconds.
In this post, you’ll learn how to send emails using your Gmail account, with just a few lines of code.
🛠️ What You’ll Learn
✅ How to connect to Gmail with Python
✅ How to send a basic email using
smtplib
✅ How to automate and customize your messages
💻 Full Python Code
📝 Tip: To send to multiple recipients, you can change
receiver_email
to a list like:
receiver_email = ["person1@example.com", "person2@example.com"]
Python will send the same message to everyone in that list.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Replace with your actual email and app password
sender_email = "your_email@gmail.com"
receiver_email = "recipient@example.com" # or use a list of emails
password = "your_gmail_app_password"
# Create the email
message = MIMEMultipart("alternative")
message["Subject"] = "Automated Email from Python"
message["From"] = sender_email
message["To"] = receiver_email
text = "Hi there,\nThis email was sent automatically using Python!"
html = "<html><body><p>Hi there,<br>This email was <b>sent automatically</b> using Python!</p></body></html>"
# Attach both plain text and HTML
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")
message.attach(part1)
message.attach(part2)
# Send the email
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message.as_string()) # sends to one or many
print("✅ Email sent successfully!")
🔍 How It Works (Line by Line)
Below is a detailed explanation for each line of the Python script:
import smtplib
➡️ Imports the smtplib
library, which allows Python to send emails using the SMTP (Simple Mail Transfer Protocol).
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
➡️ These are classes from the email.mime
module that help build email content — including both plain text and HTML formats.
sender_email = "your_email@gmail.com"
receiver_email = "recipient@example.com"
password = "your_gmail_app_password"
➡️ Set up the sender’s email, the recipient’s email, and your Gmail App Password (used instead of your real password).
message = MIMEMultipart("alternative")
➡️ Creates a multi-part email. "alternative" allows both plain text and HTML versions of the message.
message["Subject"] = "Automated Email from Python"
message["From"] = sender_email
message["To"] = receiver_email
➡️ Defines the email’s metadata: subject, sender, and recipient.
text = "Hi there,
This email was sent automatically using Python!"
➡️ The plain text version of your email message.
html = "<html><body><p>Hi there,<br>This email was <b>sent automatically</b> using Python!</p></body></html>"
➡️ The HTML version — used by most modern email clients to show rich formatting.
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")
➡️ These wrap your text and HTML in MIME-compatible email objects.
message.attach(part1)
message.attach(part2)
➡️ Attaches both plain text and HTML to the message. The email client will show the best version it can.
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
➡️ Opens a connection to Gmail’s SMTP server using SSL on port 465.
server.login(sender_email, password)
➡️ Logs into your Gmail account using the App Password.
server.sendmail(sender_email, receiver_email, message.as_string())
➡️ Sends the actual email from your account to the recipient.
print("✅ Email sent successfully!")
➡️ Prints a success message to let you know the script worked!
import smtplib
This is the Python module that lets you send emails using the SMTP protocol.
MIMEMultipart
, MIMEText
These let you send both text and HTML versions of your message (so it looks good in any inbox).
server = smtplib.SMTP_SSL(...)
This connects securely to Gmail’s servers using your credentials.
You must enable 2FA and use an App Password from your Google Account.
🧪 How to Run This Script
✅ Step 1: Make Sure Python Is Installed
Open your terminal (macOS/Linux) or Command Prompt (Windows)
Type:
python --version
If it shows something like Python 3.10.8
, you're good.
If not, download it from python.org/downloads.
✅ Step 2: Enable Gmail App Password
Log into your Google Account
Go to Security → 2-Step Verification → Turn it on
Then go to App Passwords
Choose "Mail" and your device (e.g. “Windows Computer”)
Copy the 16-digit App Password Google gives you
✅ Step 3: Create the Script
Open VS Code, Thonny, or Notepad
Paste the Python code from above
Replace these lines with your actual data:
sender_email = "your_email@gmail.com"
receiver_email = "recipient@example.com"
password = "your_gmail_app_password"
- Save the file as:
send_email.py
✅ Step 4: Run the Script
Open your terminal or command prompt
Navigate to the folder where the script is saved:
cd path/to/your/script
- Run the script:
python send_email.py
✅ If everything’s correct, you’ll see:
✅ Email sent successfully!
Check your inbox to confirm it worked 🎉
🔐 Important: Gmail Security Setup
To make this script work with Gmail:
Turn on 2-step verification in your Google Account
Create an App Password here
Use that App Password instead of your real Gmail password in the script
🧪 Customize It!
You can:
Change the subject line
Add attachments
Send to multiple recipients
Schedule it to run daily or weekly with
cron
or Task Scheduler
⚠️ Safety Tip
Never share your Gmail password or app password in public code.
Store credentials securely using environment variables or .env
files.
🎓 Takeaway
With just a few lines of Python, you can automate one of the most common tasks — sending emails.
Whether it's for reports, reminders, or notifications, this script saves time and keeps you productive.
📎 Save & Share
Try it with your Gmail — and don’t forget to test on yourself first 😉
💬 Question for You
What kind of emails would you automate with Python?
Subscribe to my newsletter
Read articles from Eugene Cul directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
