Automating Email with Python
Published on October 31, 2025 • 11 min read • Guide
Email automation can save you hours every week. Whether you're sending newsletters, organizing your inbox, or processing attachments, Python makes it easy to automate these repetitive email tasks.
Why Automate Email?
Email automation helps you:
- Send bulk emails without manual effort
- Automatically respond to common inquiries
- Organize and filter incoming messages
- Extract and process email attachments
- Schedule emails to send at specific times
Sending Emails with Python
Python's built-in smtplib library makes sending emails straightforward:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Email configuration
sender_email = "
[email protected]"
receiver_email = "
[email protected]"
password = "your_app_password"
# Create message
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = "Automated Email from Python"
body = "This is an automated email sent using Python!"
msg.attach(MIMEText(body, 'plain'))
# Send email
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
server.login(sender_email, password)
server.send_message(msg)
print("Email sent successfully!")
🔒 Security Tip: Use app-specific passwords instead of your actual email password. Never hardcode passwords in your scripts!
Reading Emails
Use the imaplib library to read and process incoming emails:
import imaplib
import email
# Connect to Gmail
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('
[email protected]', 'your_app_password')
# Select inbox
mail.select('inbox')
# Search for emails
status, messages = mail.search(None, 'UNSEEN')
# Get email IDs
email_ids = messages[0].split()
for email_id in email_ids:
status, msg_data = mail.fetch(email_id, '(RFC822)')
msg = email.message_from_bytes(msg_data[0][1])
print(f"From: {msg['From']}")
print(f"Subject: {msg['Subject']}")
Sending HTML Emails
Create professional-looking emails with HTML formatting:
from email.mime.text import MIMEText
html_content = """
Welcome to Python Email Automation!
This is a formatted email.
"""
msg.attach(MIMEText(html_content, 'html'))
Sending Emails with Attachments
from email.mime.base import MIMEBase
from email import encoders
# Attach file
filename = "document.pdf"
with open(filename, "rb") as attachment:
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
f"attachment; filename= {filename}",
)
msg.attach(part)
Bulk Email Sending
Send emails to multiple recipients:
recipients = [
"
[email protected]",
"
[email protected]",
"
[email protected]"
]
for recipient in recipients:
msg['To'] = recipient
# Send email
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
server.login(sender_email, password)
server.send_message(msg)
print(f"Email sent to {recipient}")
⚠️ Important: Add delays between bulk emails to avoid being flagged as spam!
Common Email Providers
SMTP settings for popular email services:
Gmail
- SMTP: smtp.gmail.com
- Port: 465 (SSL) or 587 (TLS)
Outlook
- SMTP: smtp-mail.outlook.com
- Port: 587
Yahoo
- SMTP: smtp.mail.yahoo.com
- Port: 465
Best Practices
- Always use app-specific passwords
- Store credentials securely (use environment variables)
- Add error handling for failed sends
- Respect email provider rate limits
- Include unsubscribe options for bulk emails
- Test with small batches first
Conclusion
Email automation with Python can dramatically improve your productivity. Start with simple tasks like sending status updates, then gradually build more complex workflows for managing your inbo