使用smtplib模块发送邮件
在使用django/flask时,框架本身已经为我们封装好了发送邮件的函数,python的smtplib提供了一种很方便的途径发送电子邮件。它对smtp协议进行了简单的封装。
对于smtp的使用相对来说比较简单,代码如下:
一、发送文本内容
直接使用smtplib发送文本内容,以下两步是使用smtplib发送任何形式邮件都可以遵循的。
1. 准备工作
构建邮件内容, From(发件人)、To(收件人)、Subject(邮件标题)和Text(邮件内容)。
import smtplib
import stringsmtpserver = 'smtp.163.com' # 邮件smtp的地址
subject = '这是邮件标题' # 定义邮件的标题
sender = 'youremail@163.com' # 发件人
receiver = 'toemail@qq.com' # 收件人
msg = '这是一封测试邮件,由<%s>发出'%From # 发送的邮件文本内容
msg_content = '\r\n'.join(['From:%s'%sender, 'To:%s'%receiver, 'Subject:%s'%subject, '', msg])
2. 构造SMTP对象发送邮件
smtp_server = smtplib.SMTP() # 构造smtp服务对象,可以在构造对象时将host和port传入直接连接服务器
smtp_server.set_debuglevel(1) # 开启发送debug模式,把发送邮件的过程显示出来
smtp_server.connect(host=smtpserver, port='25') # 连接邮箱服务器,端口可以不写,默认为25
smtp_server.starttls() # 启动安全传输模式
smtp_server.login(FROM, 'yourpassword') # 登录邮箱服务器
smtp_server.sendmail(from_addr=sender, to_addrs=receiver, msg=msg_content) # 发送邮件
smtp_server.quit() # 关闭smtp服务器连接
二、使用email模块发送多样化的内容
email模块配合smtplib进行邮件的发送和接收,包含自定义邮件的中文、主题、日期、附件等信息,具体概念及其函数参考官方文档,链接奉上https://docs.python.org/3/library/email.html#module-email
如果要使用email模块的功能,可以参考网上的以下7个例子:
1. 文件形式的邮件
import smtplib
from email.mime.text import MIMEText
from email.header import Header sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***' msg = MIMEText('你好','text','utf-8') # 中文需参数‘utf-8',单字节字符不需要
msg['Subject'] = Header(subject, 'utf-8') smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
2. html形式的邮件
import smtplib
from email.mime.text import MIMEText sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***' msg = MIMEText('</pre>
<h1>你好</h1>
<pre>','html','utf-8') msg['Subject'] = subject smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
3. 带图片的html邮件
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('h:\\python\\1.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close() msgImage.add_header('Content-ID', '')
msgRoot.attach(msgImage) smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
4. 带附件的邮件
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' #构造附件
att = MIMEText(open('h:\\python\\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('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
5. 群邮件
import smtplib
from email.mime.text import MIMEText sender = '***'
receiver = ['***','****',……]
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***' msg = MIMEText('你好', 'text', 'utf-8') msg['Subject'] = subject smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
6. 包含各种元素的邮件
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 = '***' # Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link" # Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\ Hi! How are you? Here is the <a href="http://www.python.org">link</a> you wanted. """ # Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html') # Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
#构造附件
att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="1.jpg"'
msg.attach(att) smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
7. 基于ssl的邮件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***' msg = MIMEText('你好','text','utf-8') # 中文需参数‘utf-8',单字节字符不需要
msg['Subject'] = Header(subject, 'utf-8') smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.set_debuglevel(1)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
使用smtplib模块发送邮件相关推荐
- smtplib python教程_python使用smtplib模块发送邮件
使用smtplib模块发送邮件,供大家参考,具体内容如下 1)使用smtplib模块发送简单邮件 步骤: 1.连接SMTP服务器,并使用用户名.密码登陆服务器 2.创建EmailMessage对象,该 ...
- smtplib python_python:利用smtplib模块发送邮件
自动化测试中,测试报告一般都需要发送给相关的人员,比较有效的一个方法是每次执行完测试用例后,将测试报告(HTML.截图.附件)通过邮件方式发送. 一.python对SMTP的支持 SMTP(Simpl ...
- python smtplib模块_Python smtplib模块详解:发送邮件
使用 Python 的 smtplib 模块来发送邮件非常简单,大部分底层的处理都由 smtplib 进行了封装,开发者只需要按照如下 3 步来发送邮件即可: 连接 SMTP 服务器,并使用用户名.密 ...
- python 发送邮件正文字体设置_python 文字 坐标python smtplib模块发送SSL/TLS安全邮件实例...
python的smtplib提供了一种很方便的途径发送电子邮件.它对smtp协议进行了简单的封装. smtp协议的基本命令包括: HELO 向服务器标识用户身份 MAIL 初始化邮件传输 mail f ...
- foxmail 怎么把邮件格式默认为html_Python SMTP发送邮件-smtplib模块
在进入正题之前,我们需要对一些基本内容有所了解:常用的电子邮件协议有SMTP.POP3.IMAP4,它们都隶属于TCP/IP协议簇,默认状态下,分别通过TCP端口25.110和143建立连接. Pyt ...
- Python 发送邮件 email 模块、smtplib 模块
____tz_zs SMTP email 模块:负则构造邮件 smtplib 模块:负则发送邮件 一.发送纯文本邮件 from email.header import Header from emai ...
- [转载]Python SMTP发送邮件-smtplib模块
在进入正题之前,我们需要对一些基本内容有所了解:常用的电子邮件协议有SMTP.POP3.IMAP4,它们都隶属于TCP/IP协议簇,默认状态下,分别通过TCP端口25.110和143建立连接. Pyt ...
- 3.python 发送邮件之smtplib模块
SMTP(Simple Mail Transfer Protocol)是简单邮件传输协议,它是一组用于由源地址到目的地址的邮件传输规则. python中对SMTP进行了简单的封装,可以发送纯文本邮件, ...
- python安装email模块_Python使用SMTP模块、email模块发送邮件
一.smtplib模块: 主要通过SMTP类与邮件系统进行交互.使用方法如下: 1.实例化一个SMTP对象: s = smtplib.SMTP(邮件服务地址,端口号) s = smtplib.SMTP ...
最新文章
- TitanDB GC详细实现原理 及其 引入的问题
- 精读《手写 SQL 编译器 - 文法介绍》
- 根据长文本拆分至内表
- 数学建模之运筹学问题
- python之网络编程 --- TCP编程
- 锐捷官方提供122套实验题.
- Mysql字符串数据插入转义处理
- 爬虫中如何获取页面编码类型
- git为私有仓库设置密码_真香!在局域网下行云流水般使用git
- VS Code 的 Java 七月更新,新的重构特性
- 马云:遇见好老板很重要;锤子上海法人变更;摩拜更名美团单车 | 极客头条...
- 趋势发布SecureCloud云安全技术架构
- 最优二叉搜索树(动态规划)
- 强化学习离轨策略:从失败中获得成功经验 - 以追女孩为例 | 采样率的数学意义
- 【UOJ】#37. 【清华集训2014】主旋律
- 基于springboot小区物业管理系统
- C语言-输入任意多个数字,数字间用空格隔开,然后将数字求和。
- DRM-X 4.0加密保护与Widevine DRM平台的区别
- 纠错码 - 海明码/汉明码
- 知乎 live 记录