How to Send Emails Using Python's smtplib and email Modules
This guide explains how to configure email account credentials, connect to an SMTP server, construct plain‑text, HTML, and attachment‑based messages using Python's smtplib and email libraries, and send them, with step‑by‑step code examples for each scenario.
SMTP (Simple Mail Transfer Protocol) is a protocol for sending email from a source address to a destination address. Python's smtplib library provides a convenient wrapper for SMTP.
1. Prepare the email account
Obtain the email address and its authorization code (example shown for QQ mail). The steps include logging into the webmail, navigating to the help center, and retrieving the authorization code.
2. Basic steps to send an email
The workflow mirrors manual email sending: log in, prepare content, and send.
2.1 Log in to the mailbox
import smtplib
# 1. Connect to the mail server
connection = smtplib.SMTP_SSL('smtp.163.com', 465)
# 2. Log in
connection.login('[email protected]', 'authorization_code')2.2 Prepare the message data
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
msg = MIMEMultipart()
subject = Header('Email Subject', 'utf-8').encode()
msg['Subject'] = subject
msg['From'] = 'Your Name <[email protected]>'
msg['To'] = '[email protected];[email protected]'
text = MIMEText('Email body text', 'plain', 'utf-8')
msg.attach(text)2.3 Send the email
connection.sendmail('[email protected]', '[email protected]', msg.as_string())
connection.quit()3. Example email types
3.1 Plain‑text email
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
con = smtplib.SMTP_SSL('smtp.163.com', 465)
con.login('[email protected]', 'password')
msg = MIMEMultipart()
subject = Header('Password Recovery', 'utf-8').encode()
msg['Subject'] = subject
msg['From'] = '[email protected] <[email protected]>'
msg['To'] = '[email protected]'
text = MIMEText('Forgot password, need to recover', 'plain', 'utf-8')
msg.attach(text)
con.sendmail('[email protected]', '[email protected]', msg.as_string())
con.quit()3.2 HTML email
from email.mime.text import MIMEText
html_content = """<h2>Title</h2><p>Paragraph 1</p><p>Paragraph 2</p><img src='https://www.baidu.com/img/bd_logo1.png'> <center>Baidu Image</center><a href='https://www.baidu.com'>Visit Baidu</a>"""
html = MIMEText(html_content, 'html', 'utf-8')
msg.attach(html)3.3 Email with attachments
from email.mime.text import MIMEText
file_obj = MIMEText(open('files/test.txt', 'rb').read(), 'base64', 'utf-8')
file_obj["Content-Disposition"] = 'attachment; filename="test.txt"'
msg.attach(file_obj)
# repeat for other files
con.sendmail('[email protected]', '[email protected]', msg.as_string())
con.quit()Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
