使用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()

相关文章

python实现屏保计时器的示例代码

python实现屏保计时器的示例代码

什么都不说先上图吧,Python初学者实现屏保计时器 原理:利用Python turtle库实现快速画图,每隔一秒钟擦除屏幕,然后获得电脑实时时间,再次画图,呈现动态时间。 关于数字如...

python分数表示方式和写法

Fraction函数是python中实现分数的一个模块(module),模块是由别人写的,并且可以被拿来直接使用的代码程序,包括类、函数以及标签的定义,是python标准函数库的一部分。...

用Python解决x的n次方问题

我考虑到了x的所有n次的情况,下面的代码有可能是不完美的,但是肯定是对的。 def aaa(x,n): A=isinstance(x,(int,float)) #这是考虑x和n...

Python中类的定义、继承及使用对象实例详解

本文实例讲述了Python中类的定义、继承及使用对象的方法。分享给大家供大家参考。具体分析如下: Python编程中类的概念可以比作是某种类型集合的描述,如“人类”可以被看作一个类,然后...

Django框架模板注入操作示例【变量传递到模板】

Django框架模板注入操作示例【变量传递到模板】

本文实例讲述了Django框架模板注入操作。分享给大家供大家参考,具体如下: 1.HTML模板如何解析变量? <h1>这是一个html页面</h1> <...