Python 3 介绍(二十七)--Python3 SMTP发送邮件

目录

1. 安装与导入

2. 基本用法

2.1 发送纯文本邮件

2.2 发送带 HTML 的邮件

2.3 发送带有附件的邮件

3. 高级用法

3.1 使用 TLS 加密

3.2 多个接收者

4. 注意事项

总结


 

SMTP(Simple Mail Transfer Protocol)是一种用于发送电子邮件的标准协议。在 Python 中,可以使用标准库中的 smtplib 模块来发送邮件。此外,还可以结合 email 模块来构建邮件内容,包括纯文本、HTML 格式、附件等。

1. 安装与导入

Python 的 smtplibemail 模块都是标准库的一部分,不需要额外安装。直接导入即可使用:

 

python

深色版本

1import smtplib
2from email.mime.text import MIMEText
3from email.mime.multipart import MIMEMultipart
4from email.mime.application import MIMEApplication

2. 基本用法

2.1 发送纯文本邮件

 

python

深色版本

1import smtplib
2from email.mime.text import MIMEText
3
4# 邮件发送者的邮箱地址
5sender_email = 'your_email@example.com'
6# 邮件接收者的邮箱地址
7receiver_email = 'receiver_email@example.com'
8# 发送者的密码
9password = 'your_password'
10
11# 创建 MIMEText 对象
12msg = MIMEText('这是一封测试邮件。', 'plain', 'utf-8')
13
14# 设置邮件头部信息
15msg['From'] = sender_email
16msg['To'] = receiver_email
17msg['Subject'] = '测试邮件'
18
19# 连接 SMTP 服务器
20server = smtplib.SMTP_SSL('smtp.example.com', 465)  # 使用 SSL 加密
21server.login(sender_email, password)
22
23# 发送邮件
24server.sendmail(sender_email, [receiver_email], msg.as_string())
25
26# 关闭连接
27server.quit()

2.2 发送带 HTML 的邮件

 

python

深色版本

1import smtplib
2from email.mime.text import MIMEText
3from email.mime.multipart import MIMEMultipart
4
5# 邮件发送者的邮箱地址
6sender_email = 'your_email@example.com'
7# 邮件接收者的邮箱地址
8receiver_email = 'receiver_email@example.com'
9# 发送者的密码
10password = 'your_password'
11
12# 创建 MIMEMultipart 对象
13msg = MIMEMultipart()
14msg['From'] = sender_email
15msg['To'] = receiver_email
16msg['Subject'] = '测试邮件'
17
18# 添加 HTML 内容
19html_content = """
20<html>
21  <body>
22    <p>这是 <b>HTML</b> 格式的邮件。</p>
23    <p><a href="http://example.com">链接</a></p>
24  </body>
25</html>
26"""
27html_part = MIMEText(html_content, 'html', 'utf-8')
28msg.attach(html_part)
29
30# 连接 SMTP 服务器
31server = smtplib.SMTP_SSL('smtp.example.com', 465)  # 使用 SSL 加密
32server.login(sender_email, password)
33
34# 发送邮件
35server.sendmail(sender_email, [receiver_email], msg.as_string())
36
37# 关闭连接
38server.quit()

2.3 发送带有附件的邮件

 

python

深色版本

1import smtplib
2from email.mime.multipart import MIMEMultipart
3from email.mime.text import MIMEText
4from email.mime.application import MIMEApplication
5
6# 邮件发送者的邮箱地址
7sender_email = 'your_email@example.com'
8# 邮件接收者的邮箱地址
9receiver_email = 'receiver_email@example.com'
10# 发送者的密码
11password = 'your_password'
12
13# 创建 MIMEMultipart 对象
14msg = MIMEMultipart()
15msg['From'] = sender_email
16msg['To'] = receiver_email
17msg['Subject'] = '测试邮件'
18
19# 添加 HTML 内容
20html_content = """
21<html>
22  <body>
23    <p>这是 <b>HTML</b> 格式的邮件。</p>
24    <p><a href="http://example.com">链接</a></p>
25  </body>
26</html>
27"""
28html_part = MIMEText(html_content, 'html', 'utf-8')
29msg.attach(html_part)
30
31# 添加附件
32with open('path/to/your/file.txt', 'rb') as f:
33    attachment = MIMEApplication(f.read(), _subtype="txt")
34    attachment.add_header('Content-Disposition', 'attachment', filename='file.txt')
35    msg.attach(attachment)
36
37# 连接 SMTP 服务器
38server = smtplib.SMTP_SSL('smtp.example.com', 465)  # 使用 SSL 加密
39server.login(sender_email, password)
40
41# 发送邮件
42server.sendmail(sender_email, [receiver_email], msg.as_string())
43
44# 关闭连接
45server.quit()

3. 高级用法

3.1 使用 TLS 加密

如果 SMTP 服务器使用 TLS 加密,可以使用 smtplib.SMTP 类,并调用 starttls 方法:

 

python

深色版本

1server = smtplib.SMTP('smtp.example.com', 587)
2server.starttls()
3server.login(sender_email, password)

3.2 多个接收者

如果需要向多个接收者发送邮件,可以将接收者的邮箱地址放在一个列表中:

 

python

深色版本

1receiver_emails = ['receiver1@example.com', 'receiver2@example.com']
2server.sendmail(sender_email, receiver_emails, msg.as_string())

4. 注意事项

  1. 安全性和认证:确保使用正确的用户名和密码登录 SMTP 服务器,并使用 SSL/TLS 加密来保护数据传输。
  2. 邮件服务器限制:某些邮件服务器可能对每天发送的邮件数量有限制,超出限制可能会导致发送失败。
  3. 邮件格式:确保邮件内容的格式正确,避免出现乱码或者格式错误。
  4. 测试:在正式使用之前,建议先发送几封测试邮件,确认一切正常后再投入使用。

总结

通过 smtplibemail 模块,你可以方便地在 Python 中发送各种类型的邮件,包括纯文本邮件、HTML 格式的邮件以及包含附件的邮件。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值