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实现过滤单个Android程序日志脚本分享
Jan 16 Python
python检测远程udp端口是否打开的方法
Mar 14 Python
初步讲解Python中的元组概念
May 21 Python
python搭建微信公众平台
Feb 09 Python
详解python3实现的web端json通信协议
Dec 29 Python
Python错误提示:[Errno 24] Too many open files的分析与解决
Feb 16 Python
Python subprocess模块常见用法分析
Jun 12 Python
基于python3实现倒叙字符串
Feb 18 Python
python根据完整路径获得盘名/路径名/文件名/文件扩展名的方法
Apr 22 Python
python实现无边框进度条的实例代码
Dec 30 Python
python实现批量移动文件
Apr 05 Python
Python并发编程实例教程之线程的玩法
Jun 20 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中根据某年第几天计算出日期年月日的代码
2011/02/24 PHP
PHP实现加密的几种方式介绍
2015/02/22 PHP
为百度UE编辑器上传图片添加水印功能
2015/04/16 PHP
php使用Jpgraph绘制柱形图的方法
2015/06/10 PHP
Smarty最简单实现列表奇偶变色的方法
2015/07/01 PHP
php解析base64数据生成图片的方法
2016/12/06 PHP
High Performance JavaScript(高性能JavaScript)读书笔记分析
2011/05/05 Javascript
JS清空多文本框、文本域示例代码
2014/02/24 Javascript
引用其它js时如何同时处理多个window.onload事件
2014/09/02 Javascript
js实现图片放大和拖拽特效代码分享
2015/09/05 Javascript
js生成随机数的方法实例
2015/10/16 Javascript
bootstrap3 兼容IE8浏览器!
2016/05/02 Javascript
jquery ajaxfileupload异步上传插件使用详解
2017/02/08 Javascript
Angular 2父子组件之间共享服务通信的实现
2017/07/04 Javascript
JQuery判断正整数整理小结
2017/08/21 jQuery
实时监控input框,实现输入框与下拉框联动的实例
2018/01/23 Javascript
AngularJS与后端php的数据交互方法
2018/08/13 Javascript
使用D3.js构建实时图形的示例代码
2018/08/28 Javascript
轻松学习JavaScript函数中的 Rest 参数
2019/05/30 Javascript
微信小程序自定义弹窗实现详解(可通用)
2019/07/04 Javascript
vue图片加载失败时用默认图片替换的方法
2019/08/29 Javascript
vue路由教程之静态路由
2019/09/03 Javascript
详解vue实现坐标拾取器功能示例
2020/11/18 Vue.js
Python实现处理逆波兰表达式示例
2018/07/30 Python
django admin 自定义替换change页面模板的方法
2019/08/23 Python
python等差数列求和公式前 100 项的和实例
2020/02/25 Python
python和js交互调用的方法
2020/06/23 Python
加拿大花店:1800Flowers.ca
2016/11/16 全球购物
Opodo意大利:欧洲市场上领先的在线旅行社
2019/10/24 全球购物
毕业生个人求职的自我评价
2013/10/28 职场文书
技校个人求职信范文
2014/01/25 职场文书
大学旷课检讨书
2014/01/28 职场文书
科技工作者先进事迹
2014/08/16 职场文书
心得体会的写法
2014/09/05 职场文书
长城英文导游词
2015/01/30 职场文书
SQL Server携程核心系统无感迁移到MySQL实战
2022/06/01 SQL Server