1. HTML格式邮件的发送
以下实例为发送HTML格式的邮件,我们只需要变更调用函数时候的参数,即可完成邮件的发送;
#!/usr/bin/env python# -*- coding: utf-8 -*-# author:Zhang Kai time:2020/10/12# 使用其他邮件服务商的 SMTP 访问import smtplibfrom email.mime.text import MIMETextfrom email.header import Headerdef sendMail(sender, mail_pass, receivers, mail_msg, mail_host='smtp.163.com'): # 定义发送邮件的函数;
'''
:param mail_user: 字符串格式:kai.zhang@unisemicon.com 发件人的邮箱
:param mail_pass: 字符串格式:发件人邮箱密码
:param receivers: 收件人列表;格式:['123@qq.com','234@qq.com','678@qq.com']
:param mail_msg: 邮件的内容,HTML 的字符串格式
:param mail_host: 设置服务器; 默认163邮箱; 举例:qq:smtp.qq.com; 163:smtp.163.com; 等
:return: 返回值如下:
1:代表邮件发送成功;
2:代表邮件发送失败;
'''
# 第三方 SMTP 服务
mail_host = "smtp.qq.com" # 设置服务器,qq:smtp.qq.com; 163:smtp.163.com; 等
sender = sender # 用户名
mail_pass = mail_pass # 密码
# 设置发件人 & 收件人
sender = sender # 发件人,这里设置成与用户名一致
receivers = receivers # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
# 邮件的内容,HTML 格式
mail_msg = mail_msg
message = MIMEText(mail_msg, 'HTML', 'utf-8')
# 三个参数:第一个为邮件内容;第二个 设置文本格式 plain为纯文本,HTML为HTML格式;第三个 utf-8 设置编码
message['From'] = sender
message['To'] = ','.join(receivers)
subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(sender, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
return 1 # 邮件发送成功
except Exception:
print(e.__class__.__name__) # 打印异常的名字
print(e) # 打印异常的详细信息
return 0 # "Error: 其他异常"# 定义邮件内容,HTML格式mail_msg = """
<p>Python 邮件发送测试...</p>
<p><a href="http://172.21.21.8:8000/">这是一个内部查询系统的链接</a></p>
"""# 调用函数res = sendMail(sender="794532995@qq.com", mail_pass="12345678", receivers=['794532995@qq.com'],
mail_msg=mail_msg, mail_host="smtp.unisemicon.com")# 打印返回值print(res)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
2. 文本格式邮件的发送
只需要将上述代码中的 message = MIMEText(mail_msg, 'HTML', 'utf-8')
中的HTML 变为plain ,则邮件内容变为纯文本;
即修改为:message = MIMEText(mail_msg, 'plain', 'utf-8')
3. 包含附件邮件的发送
发送带附件的邮件,首先要创建MIMEMultipart()实例,然后构造附件,如果有多个附件,可依次构造,最后利用smtplib.smtp发送;只有mail_msg这个参数需要重新构造,其余无变化;代码参考如下:
#!/usr/bin/env python# -*- coding: utf-8 -*-# author:Zhang Kai time:2020/10/12import smtplibfrom email.mime.text import MIMETextfrom email.header import Headerfrom email.mime.multipart import MIMEMultipart
mail_msg = """
<p>Python 邮件发送测试...</p>
<p><a href="http://172.21.21.8:8000/">这是一个内部查询系统的链接</a></p>
"""sender="794532995@qq.com"mail_pass="pyelx****lkbfhj"receivers=['kai.zhang@unisemicon.com']mail_msg=mail_msg
attach_file = 'test.txt'mail_host="smtp.qq.com"# 第三方 SMTP 服务mail_host = "smtp.qq.com" # 设置服务器,qq:smtp.qq.com; 163:smtp.163.com; 等sender = sender # 用户名mail_pass = mail_pass # 密码# 设置发件人 & 收件人sender = sender # 发件人,这里设置成与用户名一致receivers = receivers # 接收邮件,可设置为你的QQ邮箱或者其他邮箱# 创建一个带附件的邮件内容实例;message=MIMEMultipart()message['From'] = sender
message['To'] = ','.join(receivers)subject = 'Python SMTP 邮件测试'message['Subject'] = Header(subject, 'utf-8')# 增加邮件正文的内容,HTML 格式mail_msg = mail_msg
message.attach(MIMEText(mail_msg, 'HTML', 'utf-8')) # 三个参数:第一个为邮件内容;第二个 设置文本格式 plain为纯文本,HTML为HTML格式;第三个 utf-8 设置编码# 构造附件1,传入当前目录下的test.txt 文件,当前目录下必须要有test.txt文件;attach_file =attach_file
att1 = MIMEText(open(attach_file, 'rb').read(), 'base64', 'utf-8')att1["Content-Type"] = 'application/octet-stream'# 这里的filename可以任意写,写什么名字,邮件中显示什么名字att1["Content-Disposition"] = 'attachment; filename="test.txt"'message.attach(att1)# 构造附件2,按照附件1 的方式,可以添加很多个附件;try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(sender, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print('邮件发送成功')except Exception as e:
print(e.__class__.__name__) #
print(e) #
print('邮件发送失败')123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
备注:邮件一般在垃圾邮箱;
4. 包含附件邮件的发送
#!/usr/bin/env python# -*- coding: utf-8 -*-# author:Zhang Kai time:2020/10/13import smtplibfrom email.mime.image import MIMEImagefrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom email.header import Header
sender="794532995@qq.com"mail_pass="pyelxrvvgclkbfhj" # 密码receivers=['kai.zhang@unisemicon.com'] # 设置收件人attach_file = 'test.txt'mail_host="smtp.qq.com"# 创建一个带图片的邮件内容实例:message;message=MIMEMultipart() # msgRoot = MIMEMultipart('related')message['From'] = sender # 给实例添加发件人message['To'] = ','.join(receivers) # 给实例添加收件人subject = 'Python SMTP 邮件测试'message['Subject'] = Header(subject, 'utf-8') # 给实例添加标题msgAlternative = MIMEMultipart('alternative') # 创建带图片的内容message.attach(msgAlternative) # 将内容添加到实例mail_msg = """
<p>Python 邮件发送测试...</p>
<p><a href="http://172.21.21.8:8000/">这是一个内部查询系统的链接</a></p>
<p>图片如下:</p>
<p><img src="cid:image1"></p>
<p><img src="cid:image2"></p>
"""msgAlternative.attach(MIMEText(mail_msg, 'html', 'utf-8')) # 给内容增加邮件内容# 指定图片为当前目录fp = open('test.jpg', 'rb')msgImage = MIMEImage(fp.read()) # 将图片赋值给msgImage;fp.close()fp = open('test2.jpg', 'rb')msgImage2 = MIMEImage(fp.read()) # 将图片2赋值给msgImage;fp.close()# 定义图片 ID,在 HTML 文本中引用msgImage.add_header('Content-ID', '<image1>') # 给图片添加标题message.attach(msgImage) # 给实例添加图片msgImage2.add_header('Content-ID', '<image2>') # 给图片添加标题message.attach(msgImage2) # 给实例添加图片try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(sender, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print('邮件发送成功')except Exception as e:
print(e.__class__.__name__) #
print(e) #
print('邮件发送失败')
5. 发生邮件的步骤总结
5.1 导入对应的包;一般以下几个就完全够用了;
import smtplibfrom email.mime.image import MIMEImagefrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom email.header import Header12345
5.2 建立smtplib.SMTP()对象;
smtpObj = smtplib.SMTP() # 创建邮件对象1
5.3 连接服务器 & 登陆邮箱; QQ邮箱密码参考本章的 ‘3. QQ邮箱异常解决’
mail_host = "smtp.qq.com" # 取决于发送邮件的邮箱是什么;smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号smtpObj.login(sender, mail_pass) # sender:发件人的邮箱; mail_pass:邮箱对应的密码;QQ邮箱密码参考本章的 3. QQ邮箱异常解决
5.4 发送邮件;
发送邮件的语句如下,包括三个参数:sender, receivers, message.as_string();构建完成message参数后,使用以下语句就可以实现发送邮件的功能;
smtpObj.sendmail(sender, receivers, message.as_string())1
参数解释:
sender: 就是发件人,直接定义即可,例如:sender="794532995@qq.com"
receivers: 就是收件人,列表直接定义即可,例如:receivers=['kai.zhang@unisemicon.com','794532995@qq.com']
message: 就是邮件内容;邮件内容稍微复杂,需要先创建邮件对象实例,然后向实例中添加:发件人,收件人,主题,正文,附件;
5.4.0 创建邮件对象实例:
message=MIMEMultipart() # 创建邮件对象实例,然后向实例中添加发件人,收件人,主题,正文,附件;1
5.4.1 邮件对象中的发件人:
message['From'] = '794532995@qq.com'1
5.4.2 邮件对象中的收件人:
message['To'] = ','.join(receivers)# 其中:receivers=['kai.zhang@unisemicon.com','794532995@qq.com'] 是一个收件人列表;12
5.4.3 邮件对象中的主题:
subject = 'Python SMTP 邮件测试'message['Subject'] = Header(subject, 'utf-8') # 给实例添加标题12
5.4.4 邮件对象中的正文:
# 生成一个只有文本(或者HTML)的正文对象,然后添加到message中;mail_msg = """
<p>Python 邮件发送测试...</p>
<p><a href="http://172.21.21.8:8000/">这是一个内部查询系统的链接</a></p>
"""message.attach(MIMEText(mail_msg, 'HTML', 'utf-8'))123456
5.4.5 邮件对象中的包含图片的正文:
# 创建一个带图片的邮件内容,然后添加到message中;msgAlternative = MIMEMultipart('alternative') # 创建带图片的内容message.attach(msgAlternative) # 将内容添加到实例mail_msg = """
<p>Python 邮件发送测试...</p>
<p><a href="http://172.21.21.8:8000/">这是一个内部查询系统的链接</a></p>
<p>图片如下:</p>
<p><img src="cid:image1"></p>
<p><img src="cid:image2"></p>
"""msgAlternative.attach(MIMEText(mail_msg, 'html', 'utf-8')) # 给内容增加邮件HTML文本# 指定图片为当前目录,然后打开图片文件,赋值给 msgImage;fp = open('test.jpg', 'rb')msgImage = MIMEImage(fp.read()) # 将图片1 赋值给msgImage;fp.close()msgImage.add_header('Content-ID', '<image1>') # 给图片1添加标题message.attach(msgImage) # 给实例添加图片12345678910111213141516171819
5.4.6 邮件对象中的附件:构造附件对象,然后添加到message中;
# 构造附件1,传入当前目录下的test.txt 文件attach_file = 'test.txt'att1 = MIMEText(open(attach_file, 'rb
评论(0)