Python has excellent email tooling at every level, from standard library basics to high-level automation frameworks. Here's the complete map.
Standard Library — smtplib + email
Built-in, no install required. Verbose but full control:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart('alternative')
msg['From'] = 'you@example.com'
msg['To'] = 'recipient@example.com'
msg['Subject'] = 'Hello'
msg.attach(MIMEText('Hello World
', 'html'))
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as s:
s.login('you@example.com', 'app_password')
s.send_message(msg)
yagmail — Gmail Simplicity
import yagmail
yag = yagmail.SMTP('you@gmail.com', 'app_password')
yag.send('recipient@example.com', 'Subject', 'Body text')
imaplib + imapclient — Reading Email
import imapclient
with imapclient.IMAPClient('imap.gmail.com', ssl=True) as srv:
srv.login(user, password)
srv.select_folder('INBOX')
for uid, data in srv.fetch(srv.search(['UNSEEN']), ['ENVELOPE']).items():
print(data[b'ENVELOPE'].subject)
aiosmtplib — Async Sending
import asyncio, aiosmtplib
from email.message import EmailMessage
async def send():
msg = EmailMessage()
msg['From'] = 'you@example.com'
msg['To'] = 'them@example.com'
msg['Subject'] = 'Async email'
msg.set_content('Hello')
await aiosmtplib.send(msg, hostname='smtp.gmail.com', port=465, use_tls=True,
username='you@example.com', password='app_password')
asyncio.run(send())
Choosing
- One-off sends: yagmail
- Full control + attachments: smtplib + email
- Reading inbox: imapclient
- High-concurrency sending: aiosmtplib
- Complete automation: build on imaplib + smtplib
For full-stack email automation, see ZeroPhantom's AI Mailer →