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 相关文章推荐
Python模块学习 datetime介绍
Aug 27 Python
Python的gevent框架的入门教程
Apr 29 Python
Python应用库大全总结
May 30 Python
virtualenv 指定 python 解释器的版本方法
Oct 25 Python
Python基于datetime或time模块分别获取当前时间戳的方法实例
Feb 19 Python
wxPython实现绘图小例子
Nov 19 Python
python使用 cx_Oracle 模块进行查询操作示例
Nov 28 Python
解决pyecharts运行后产生的html文件用浏览器打开空白
Mar 11 Python
如何使用PyCharm将代码上传到GitHub上(图文详解)
Apr 27 Python
PyTorch之nn.ReLU与F.ReLU的区别介绍
Jun 27 Python
Python爬虫自动化爬取b站实时弹幕实例方法
Jan 26 Python
python如何正确使用yield
May 21 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
也谈 PHP 和 MYSQL
2006/10/09 PHP
PHP游戏编程25个脚本代码
2011/02/08 PHP
codeigniter自带数据库类使用方法说明
2014/03/25 PHP
php版微信公众号接口实现发红包的方法
2016/10/14 PHP
Javascript &amp; DHTML 实例编程(教程)基础知识
2007/06/02 Javascript
javascript得到XML某节点的子节点个数的脚本
2008/10/11 Javascript
jquery keypress,keyup,onpropertychange键盘事件
2010/06/25 Javascript
一个JQuery操作Table的代码分享
2012/03/30 Javascript
jQuery中delegate与on的用法与区别示例介绍
2013/12/20 Javascript
setTimeout()递归调用不加引号出错的解决方法
2014/09/05 Javascript
jQuery使用$.ajax进行即时验证实例详解
2015/12/11 Javascript
node.js require() 源码解读
2015/12/13 Javascript
AngularJS手动表单验证
2016/02/01 Javascript
js实现的下拉框二级联动效果
2016/04/30 Javascript
javascript创建含数字字母的随机字符串方法总结
2016/08/01 Javascript
Angular.js实现注册系统的实例详解
2016/12/18 Javascript
BootStrop前端框架入门教程详解
2016/12/25 Javascript
Vue.js路由vue-router使用方法详解
2017/03/20 Javascript
H5上传本地图片并预览功能
2017/05/08 Javascript
如何基于原生javaScript生成带图片的二维码
2019/11/21 Javascript
[55:39]DOTA2-DPC中国联赛 正赛 VG vs LBZS BO3 第二场 1月19日
2021/03/11 DOTA
Python中的ceil()方法使用教程
2015/05/14 Python
解析Python编程中的包结构
2015/10/25 Python
python通过zabbix api获取主机
2018/09/17 Python
django项目简单调取百度翻译接口的方法
2019/08/06 Python
Python datetime包函数简单介绍
2019/08/28 Python
python+selenium 点击单选框-radio的实现方法
2019/09/03 Python
jupyter notebook 实现matplotlib图动态刷新
2020/04/22 Python
python模块如何查看
2020/06/16 Python
外贸业务员求职信范文
2013/12/12 职场文书
高中地理教学反思
2014/01/29 职场文书
《问银河》教学反思
2014/02/19 职场文书
喝酒检查书范文
2014/02/23 职场文书
软件售后服务承诺书
2014/05/21 职场文书
2015年简历自我评价范文
2015/03/11 职场文书
2015教师个人师德工作总结
2015/10/23 职场文书