How to Send Emails with Python: Step‑by‑Step SMTP Tutorial
This guide explains how to use Python's smtplib and email modules to compose and send plain‑text emails via an SMTP server, covering imports, message construction, header settings, error handling, and optional SSL login for services like QQ mail.
Overview
In many practical scenarios you may need to verify that the results of a Python script are delivered via email. This article demonstrates how to send emails using Python.
Required Libraries
smtplib
email.mime.text (MIMEText)
email.header (Header)
If any of these libraries are missing, install them with pip install.
Code
import smtplib
from email.mime.text import MIMEText
from email.header import Header
subject = "Python E-Mail Test"
sender = "<the email address of the sender>"
to_receiver = ["<the email address of the receiver>"]
cc_receiver = ["<the email address of the CC receiver>"]
receiver = to_receiver + cc_receiver
message = MIMEText("Hi,
This is Python Challenge test email, please ignore it.
Regards,
Aaron", "plain", "UTF-8")
message["Subject"] = Header(subject, "UTF-8")
message["From"] = sender
message["To"] = ";".join(to_receiver)
message["Cc"] = ";".join(cc_receiver)
try:
smtpObj = smtplib.SMTP("<smtp server address>")
smtpObj.sendmail(sender, receiver, message.as_string())
print("Email sent successfully")
except smtplib.SMTPException:
print("Error")Explanation
The import and from … import … statements load the required third‑party modules.
Variables subject, sender, to_receiver, and cc_receiver define the email's subject, sender address, and recipient lists. The receiver list combines direct and CC recipients. MIMEText creates the email body; the second argument specifies the format ( plain for plain text, can be changed to html), and the third argument sets the encoding ( UTF-8).
The Header object adds the subject header, while the From, To, and Cc fields are populated with the corresponding addresses, joining multiple addresses with a semicolon.
The try … except block handles potential SMTP errors; on success it prints a confirmation message, otherwise it prints "Error".
Result
Running the script prints "Email sent successfully" and the email appears in the recipient's inbox.
Additional Notes
If you do not have your own SMTP server, you can use third‑party services such as QQ mail. In that case you need to provide authentication credentials:
mail_user = "[email protected]" # username
mail_password = "xxxxxxxxxxx" # password or authorization codeQQ mail requires SSL, so use SMTP_SSL on port 465 instead of the default SMTP on port 25.
try:
smtpObj = smtplib.SMTP_SSL("<smtp server address>", 465)
smtpObj.login(mail_user, mail_password)
smtpObj.sendmail(sender, receiver, message.as_string())
print("Email sent successfully")
except smtplib.SMTPException:
print("Error")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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
