使用Python发送各种形式的邮件的方法汇总

yipeiwu_com5年前Python基础

我们平时需要使用 Python 发送各类邮件,这个需求怎么来实现?答案其实很简单,smtplib 和 email 库可以帮忙实现这个需求。smtplib 和 email 的组合可以用来发送各类邮件:普通文本,HTML 形式,带附件,群发邮件,带图片的邮件等等。我们这里将会分几节把发送邮件功能解释完成。
smtplib 是 Python 用来发送邮件的模块,email 是用来处理邮件消息。

发送 HTML 形式的邮件
发送 HTML 形式的邮件,需要 email.mime.text 中的 MIMEText 的 _subtype 设置为 html,并且 _text 的内容应该为 HTML 形式。

import smtplib
from email.mime.text import MIMEText

sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

msg = MIMEText(u'''<pre>
<h1>你好</h1>
</pre>''','html','utf-8')

msg['Subject'] = subject

smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

注意:这里的代码并没有把异常处理加入,需要读者自己处理异常。

发送带图片的邮件
发送带图片的邮件是利用 email.mime.multipart 的 MIMEMultipart 以及 email.mime.image 的 MIMEImage:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'test message'

msgText = MIMEText(
  '''<b> Some <i> HTML </i> text </b > and an image.<img alt="" src="cid:image1"/>good!''', 'html', 'utf-8')
msgRoot.attach(msgText)

fp = open('/Users/1.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()

msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)

smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()

发送带附件的邮件
发送带附件的邮件是利用 email.mime.multipart 的 MIMEMultipart 以及 email.mime.image 的 MIMEImage,重点是构造邮件头信息:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

msgRoot = MIMEMultipart('mixed')
msgRoot['Subject'] = 'test message'

# 构造附件
att = MIMEText(open('/Users/1.jpg', 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="1.jpg"'
msgRoot.attach(att)

smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()

相关文章

Pytorch 实现focal_loss 多类别和二分类示例

我就废话不多说了,直接上代码吧! import numpy as np import torch import torch.nn as nn import torch.nn.func...

如何更优雅地写python代码

前言 Python 这门语言最大的优点之一就是语法简洁,好的代码就像伪代码一样,干净、整洁、一目了然。但有时候我们写代码,特别是 Python 初学者,往往还是按照其它语言的思维习惯来写...

django 发送手机验证码的示例代码

django 发送手机验证码的示例代码

一、流程分析: 1.用户在项目前端,输入手机号,然后点击【获取验证码】,将手机号发到post到后台。 2.后台验证手机号是否合法,是否已被占用,如果通过验证,则生成验证码,并通过运行脚本...

Python使用字典实现的简单记事本功能示例

本文实例讲述了Python使用字典实现的简单记事本功能。分享给大家供大家参考,具体如下: from time import sleep, strftime user = "Cytus...

python元组操作实例解析

本文实例讲述了python元组操作方法,分享给大家供大家参考。具体分析如下: 一般来说,python的函数用法挺灵活的,和c、php的用法不太一样,和js倒是挺像的。 在照着操作时,可以...