smtplib模块

作为SMTP的构造函数,功能是与smtp服务器建立连接,在连接成功后,就可以向服务器发送相关请求比如登录、校验、发送、退出等。

SMTP类定义:smtplib.SMTP([host[,port[,local_hostname[,timeout]]]])

host参数为远程smtp主机地址,比如smtp.163.com;port为连接端口,默认为25;local_hostname的作用是在本地主机的FQDN(完整的域名)发送 HELO/EHLO(标识用户身份)指令,timeout为连接或尝试在多少秒超时。

SMTP类具有如下方法:

  • SMTP.connect([host[,port]):连接远程smtp主机方法,host为远程主机地址。port为远程主机,smtp端口,默认25,也可以直接使用host:port形式来表示,例如 SMTPconnect(“smtp.163com”,“25”)
  • SMTP.login(user,password):远程smtp主机的校验方法,参数为用户名与密码,如SMTP.login(“test@163com”,“123456”)
  • SMTP.sendmail(from_addr,to_addrs,msg[,mail_options,rcpt_options]):实现邮件的发送功能,参数依次为是发件人、收件人、邮件内容,例如:SMTP.sendmai(“test@163com”,“[email protected]”,body),body为邮件内容
  • SMTP.starttls(keyfile[,certfile]):启用TLS(安全传输)模式,所有SMTP指令都将加密传输,例如使用gmail的smtp服务时需要启动此项才能正常发送邮件。如 SMTP.starttls()。
  • SMTP.quit():断开smtp服务器的连接。

发送文本格式邮件

import smtplib
from email.mime.text import MIMEText


#发送邮箱
From="[email protected]"
#接收邮箱
To="[email protected]"
#邮箱账户
user="[email protected]"
#邮箱密码
password="xxxxxxxxxxxxx"
#邮件内容
content = 'emal from python test'
#content:文本内容,plain:文本格式,utf-8:设置编码
msg = MIMEText(content, 'plain', "utf-8")
#定义邮件主题
msg['Subject'] = "python mail test"
#发件人
msg['From']=From
#收件人
msg["To"]=To
#第一参数是smtp服务器,第二参数是端口
smtp = smtplib.SMTP_SSL("smtp.163.com", port=994)
#使用用户名和授权码登录
smtp.login(user, password)
#发件人、收件人、as_string()是将msg(MIMEText或MIMEMultipart对象)变为str
smtp.sendmail(From,To, msg.as_string())
#关闭smtp连接
smtp.quit()

发送HTML格式邮件

import smtplib
from email.mime.text import MIMEText


#发送邮箱
From="[email protected]"
#接收邮箱
To="[email protected]"
#邮箱账户
user="[email protected]"
#邮箱密码
password="xxxxxxxx"
#邮件内容
content = '''
<p>This is a html email test!</p>
<p>Hello World!</p>
<a href="https://www.baidu.com">网页链接</a>
'''
#content:文本内容,html:html格式,utf-8:设置编码
msg = MIMEText(content, 'html', "utf-8")
#定义邮件主题
msg['Subject'] = "python mail test"
#发件人
msg['From']=From
#收件人
msg["To"]=To
#第一参数是smtp服务器,第二参数是端口
smtp = smtplib.SMTP_SSL("smtp.163.com", port=994)
#使用用户名和授权码登录
smtp.login(user, password)
#发件人、收件人、as_string()是将msg(MIMEText或MIMEMultipart对象)变为str
smtp.sendmail(From,To, msg.as_string())
#关闭smtp连接
smtp.quit()

发送图文邮件

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


#发送邮箱
From="[email protected]"
#接收邮箱
To="[email protected]"
#邮箱账户
user="[email protected]"
#邮箱密码
password="xxxxxxxxx"
#邮件内容
content = '''
<p>这是一个图文邮件的测试!</p>
<p>Hello World!</p>
<a href="https://www.baidu.com">网页链接</a>
<img src="cid:test"> #引用图片id
'''
#添加图片函数(图片路径,图片ID)
def addimg(src,imagid):
    fp=open(src,'rb') #打开文件
    msgImage=MIMEImage(fp.read()) #创建MIMEImage对象,读取图片内容并作为参数
    fp.close()  #关闭文件
    msgImage.add_header('Content-ID',imagid) #指定图片ID,网页src引用
    return msgImage
#content:文本内容,html:html格式,utf-8:设置编码
recontent = MIMEText(content, 'html', "utf-8")
#创建MIMEMultipart对象,采用related定义内嵌资源的邮件体
msg=MIMEMultipart('related')
msg.attach(recontent) #给MIMEMultipart对象附加MIMEText内容
msg.attach(addimg("test.png","test")) #给MIMEMultipart对象附加MIMEImage内容
#定义邮件主题
msg['Subject'] = "python mail test"
#发件人
msg['From']=From
#收件人
msg["To"]=To
#第一参数是smtp服务器,第二参数是端口
smtp = smtplib.SMTP_SSL("smtp.163.com", port=994)
#使用用户名和授权码登录
smtp.login(user, password)
#发件人、收件人、as_string()是将msg(MIMEText或MIMEMultipart对象)变为str
smtp.sendmail(From,To, msg.as_string())
#关闭smtp连接
smtp.quit()

发送附件邮件

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


#发送邮箱
From="[email protected]"
#接收邮箱
To="[email protected]"
#邮箱账户
user="[email protected]"
#邮箱密码
password="xxxxxxxxx"
#邮件内容
content = '''
<p>This is a html email test!</p>
<p>Hello World!</p>
<a href="https://www.baidu.com">网页链接</a>
'''
#附件路径
enclosurepath="test.txt"
#自定义附件名称
enclosurename="enclosuretest.txt"
#content:文本内容,html:html格式,utf-8:设置编码
#创建带附件的邮件实例
msg = MIMEMultipart()
msg.attach(MIMEText(content, 'html', "utf-8"))
#构造附件,传送当前目录下的 test.txt 文件
att = MIMEText(open(enclosurepath, 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att.add_header("Content-Disposition", "attachment", filename=(enclosurename))
msg.attach(att)
#定义邮件主题
msg['Subject'] = "python mail test"
#发件人
msg['From']=From
#收件人
msg["To"]=To
#第一参数是smtp服务器,第二参数是端口
smtp = smtplib.SMTP_SSL("smtp.163.com", port=994)
#使用用户名和授权码登录
smtp.login(user, password)
#发件人、收件人、as_string()是将msg(MIMEText或MIMEMultipart对象)变为str
smtp.sendmail(From,To, msg.as_string())
#关闭smtp连接
smtp.quit()

List of learning reference documents

  • 《Python自动化运维技术与最佳实践-刘天斯》
  • https://blog.51cto.com/u_13661275/3223219