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 相关文章推荐
pymongo为mongodb数据库添加索引的方法
May 11 Python
使用Python对Excel进行读写操作
Mar 30 Python
Python使用matplotlib的pie函数绘制饼状图功能示例
Jan 08 Python
Python实现的栈(Stack)
Jan 26 Python
Python实现将多个空格换为一个空格.md的方法
Dec 20 Python
Python图像滤波处理操作示例【基于ImageFilter类】
Jan 03 Python
Python OpenCV利用笔记本摄像头实现人脸检测
Aug 20 Python
Django中多种重定向方法使用详解
Jul 17 Python
Django与pyecharts结合的实例代码
May 13 Python
使用pytorch实现论文中的unet网络
Jun 24 Python
python中pop()函数的语法与实例
Dec 01 Python
selenium+python自动化78-autoit参数化与批量上传功能的实现
Mar 04 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常用Stream函数集介绍
2013/06/24 PHP
WordPress开发中短代码的实现及相关函数使用技巧
2016/01/05 PHP
深入认识JavaScript中的函数
2007/01/22 Javascript
Jquery跨域获得Json时invalid label错误的解决办法
2011/01/11 Javascript
jQuery 全选/反选以及单击行改变背景色实例
2013/07/02 Javascript
如何判断元素是否为HTMLElement元素
2013/12/06 Javascript
用原生js做个简单的滑动效果的回到顶部
2014/10/15 Javascript
JavaScript 作用域链解析
2014/11/13 Javascript
DEDECMS如何为文章添加HOT NEW标志图片
2015/08/14 Javascript
Jquery easyui 实现动态树
2015/11/17 Javascript
Java与JavaScript中判断两字符串是否相等的区别
2017/03/13 Javascript
纯JS实现图片验证码功能并兼容IE6-8(推荐)
2017/04/19 Javascript
微信小程序 实现动态显示和隐藏某个控件
2017/04/27 Javascript
详解Vue.js搭建路由报错 router.map is not a function
2017/06/27 Javascript
jQuery实现点击下拉框中的值累加到文本框中的方法示例
2017/10/28 jQuery
element-ui 的el-button组件中添加自定义颜色和图标的实现方法
2018/10/26 Javascript
Vue 中如何正确引入第三方模块的方法步骤
2019/05/05 Javascript
谈谈IntersectionObserver懒加载的具体使用
2019/10/15 Javascript
基于JavaScript实现简单的轮播图
2021/03/03 Javascript
用Python实现web端用户登录和注册功能的教程
2015/04/30 Python
Flask框架的学习指南之制作简单blog系统
2016/11/20 Python
Python定义函数功能与用法实例详解
2019/04/08 Python
Django框架视图层URL映射与反向解析实例分析
2019/07/29 Python
Python连接SQLite数据库并进行增册改查操作方法详解
2020/02/18 Python
django model 条件过滤 queryset.filter(**condtions)用法详解
2020/05/20 Python
什么是python的自省
2020/06/21 Python
解决Django响应JsonResponse返回json格式数据报错问题
2020/08/09 Python
雅诗兰黛澳大利亚官网:Estée Lauder澳大利亚
2019/05/31 全球购物
如果让你测试一台高速激光打印机,你都会进行哪些测试
2012/12/04 面试题
火锅店创业计划书范文
2014/02/02 职场文书
职业女性的职业规划
2014/03/04 职场文书
实习生评语
2014/04/26 职场文书
教师竞聘上岗演讲稿
2014/09/03 职场文书
大学生赌博检讨书
2014/09/22 职场文书
2014保险公司个人工作总结
2014/12/09 职场文书
再谈python_tkinter弹出对话框创建
2022/03/20 Python