Python SMTP邮件发送教程
更新时间:2024-01-02Python SMTP邮件发送教程 - 简介
SMTP是指Simple Mail Transfer Protocol,即简单邮件传输协议,也称为电子邮件传输的标准协议。Python作为一种现代的高级编程语言,可以轻松地为开发人员提供SMTP邮件发送功能。本教程将介绍如何使用Python SMTP库发送电子邮件。 首先,您需要设置SMTP服务器和您的电子邮件的信息,然后您可以使用Python来发送邮件。
import smtplib
smtp_server = "smtp.gmail.com"
port = 587 # For starttls
sender_email = "my@gmail.com"
receiver_email = "your@gmail.com"
password = input("Type your password and press enter: ")
message = """\
Subject: Hi there
This message is sent from Python."""
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
server.starttls(context=context)
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
Python SMTP邮件发送教程 - 获得访问权限
要使用SMTP发送电子邮件,您需要获得SMTP服务器的权限。这通常需要提供 SMTP服务器名称,端口号和用户名和密码。 对于此示例,我们将使用Google SMTP服务器。要使用Google SMTP服务器,您需要访问Google账户,然后从其中激活SMTP访问。
import smtplib
smtp_server = "smtp.gmail.com"
port = 587 # For starttls
sender_email = "my@gmail.com"
receiver_email = "your@gmail.com"
password = input("Type your password and press enter: ")
message = """\
Subject: Hi there
This message is sent from Python."""
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
server.starttls(context=context)
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
Python SMTP邮件发送教程 - 发送文本和HTML消息
在电子邮件中,您可以发送普通文本消息,也可以发送HTML消息。在Python中,您可以使用HTML标记为电子邮件添加格式。要在Python中添加HTML标记,请使用MIMEText模块,并指定消息的格式为“html”。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
msg = MIMEMultipart()
msg['To'] = "whomsoever@example.com"
msg['Subject'] = "Test Email with attachment by Python."
body = MIMEText("Python SMTP Mail or Email with an attachment.
Find the Attachment.
Regards,
Your Name", "html")
msg.attach(body)
filename = "hello.txt"
filePath = "/File/Path/On/Your/OS/hello.txt"
with open(filePath, 'rb') as f:
file_data = f.read()
attachment = MIMEApplication(file_data, name=filename)
attachment['Content-Disposition'] = 'attachment; filename="%s"' % filename
msg.attach(attachment)
with smtplib.SMTP(host=server['SMTP_HOSTNAME'], port=server['SMTP_PORT']) as smtp:
smtp.starttls()
smtp.login(username, password)
smtp.send_message(msg)
Python SMTP邮件发送教程 - 打开多个附件
您可以在电子邮件中添加多个附件。为此,请在消息主体的附件部分中添加另一个文件。为了在电子邮件消息中添加多个附件,我们可以按以下方式修改前面示例中的Python代码。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
msg = MIMEMultipart()
msg['To'] = "whomsoever@example.com"
msg['Subject'] = "Test Email by Python."
body = MIMEText("Python SMTP Mail or Email with multiple attachments or files.
Find the attachments.
Regards,
Your Name", "html")
msg.attach(body)
attachments = ['file1.txt', 'file2.png', 'file3.docx']
for attachment in attachments:
_, extension = attachment.split('.')
file_path = os.path.abspath(attachment)
with open(file_path, 'rb') as f:
file_data = f.read()
attachment = MIMEApplication(file_data, _subtype=extension)
attachment.add_header('content-disposition', 'attachment', filename=attachment)
msg.attach(attachment)
with smtplib.SMTP(host=server['SMTP_HOSTNAME'], port=server['SMTP_PORT']) as smtp:
smtp.starttls()
smtp.login(username, password)
smtp.send_message(msg)