python 七种邮件内容发送方法实例


Posted in Python onApril 22, 2014

一、文件形式的邮件

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Headersender = '***'
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()

二、HTML形式的邮件

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMETextsender = '***'
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()

三、带图片的HTML邮件

#!/usr/bin/env python3
#coding: utf-8
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()

四、带附件的邮件

#!/usr/bin/env python3
#coding: utf-8
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()

五、群邮件

#!/usr/bin/env python3
#coding: utf-8
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()

六、各种元素都包含的邮件
#!/usr/bin/env python3
#coding: utf-8
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()

七、基于SSL的邮件

#!/usr/bin/env python3
#coding: utf-8
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()
Python 相关文章推荐
使用py2exe在Windows下将Python程序转为exe文件
Mar 04 Python
Python实现购物系统(示例讲解)
Sep 13 Python
python 调用c语言函数的方法
Sep 29 Python
十分钟利用Python制作属于你自己的个性logo
May 07 Python
Python计算一个给定时间点前一个月和后一个月第一天的方法
May 29 Python
python实现抖音视频批量下载
Jun 20 Python
树莓派+摄像头实现对移动物体的检测
Jun 22 Python
Python使用itchat模块实现简单的微信控制电脑功能示例
Aug 26 Python
python3连接kafka模块pykafka生产者简单封装代码
Dec 23 Python
基于Python获取照片的GPS位置信息
Jan 20 Python
Python定时从Mysql提取数据存入Redis的实现
May 03 Python
Pandas DataFrame求差集的示例代码
Dec 13 Python
sqlalchemy对象转dict的示例
Apr 22 #Python
用pywin32实现windows模拟鼠标及键盘动作
Apr 22 #Python
python实现linux服务器批量修改密码并生成execl
Apr 22 #Python
python中精确输出JSON浮点数的方法
Apr 18 #Python
python中使用OpenCV进行人脸检测的例子
Apr 18 #Python
在python的WEB框架Flask中使用多个配置文件的解决方法
Apr 18 #Python
Python操作json数据的一个简单例子
Apr 17 #Python
You might like
德生BCL3000的电路分析和打磨
2021/03/02 无线电
一个显示某段时间内每个月的方法 返回由这些月份组成的数组
2012/05/16 PHP
laravel项目利用twemproxy部署redis集群的完整步骤
2018/05/11 PHP
PHP基于GD2函数库实现验证码功能示例
2019/01/27 PHP
JavaScript与DOM组合动态创建表格实例
2012/12/23 Javascript
javascript中的previousSibling和nextSibling的正确用法
2015/09/16 Javascript
Javascript将数值转换为金额格式(分隔千分位和自动增加小数点)
2016/06/22 Javascript
jQuery 获取页面li数组并删除不在数组中的key
2016/08/02 Javascript
用JS写的一个Ajax库(实例代码)
2016/08/06 Javascript
nodejs实现发出蜂鸣声音(系统报警声)的方法
2017/01/18 NodeJs
ionic2打包android时gradle无法下载的解决方法
2017/04/05 Javascript
socket.io与pm2(cluster)集群搭配的解决方案
2017/06/02 Javascript
微信小程序实现导航栏选项卡效果
2020/06/19 Javascript
使用vue如何构建一个自动建站项目
2018/02/05 Javascript
vue内置组件transition简单原理图文详解(小结)
2018/07/12 Javascript
原生js检测页面加载完毕的实例
2018/09/11 Javascript
vue axios 简单封装以及思考
2018/10/09 Javascript
在React项目中使用Eslint代码检查工具及常见问题
2018/10/10 Javascript
Webpack4+Babel7+ES6兼容IE8的实现
2019/04/10 Javascript
微信小程序API—获取定位的详解
2019/04/30 Javascript
原生JS实现拖拽功能
2020/12/16 Javascript
python分析网页上所有超链接的方法
2015/05/08 Python
使用python爬虫获取黄金价格的核心代码
2018/06/13 Python
python通过Windows下远程控制Linux系统
2018/06/20 Python
Python二叉树的遍历操作示例【前序遍历,中序遍历,后序遍历,层序遍历】
2018/12/24 Python
Python创建数字列表的示例
2019/11/28 Python
基于Python数据结构之递归与回溯搜索
2020/02/26 Python
Python龙贝格法求积分实例
2020/02/29 Python
python实现俄罗斯方块游戏(改进版)
2020/03/13 Python
解决django FileFIELD的编码问题
2020/03/30 Python
HTML5 History API 实现无刷新跳转
2016/01/11 HTML / CSS
企业三严三实学习心得体会
2014/10/13 职场文书
2015年公路养护工作总结
2015/05/13 职场文书
工作计划范文之财务管理
2019/08/09 职场文书
经典格言警句:没有热忱,世间便无进步
2019/11/13 职场文书
使用nginx动态转换图片大小生成缩略图
2021/03/31 Servers