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实现baidu hi自动登录的代码
Feb 10 Python
在Django中限制已登录用户的访问的方法
Jul 23 Python
Python中的getopt函数使用详解
Jul 28 Python
详解 Python 读写XML文件的实例
Aug 02 Python
Python中input与raw_input 之间的比较
Aug 20 Python
python机器学习之神经网络(一)
Dec 20 Python
python中numpy的矩阵、多维数组的用法
Feb 05 Python
Flask框架学习笔记之路由和反向路由详解【图文与实例】
Aug 12 Python
Python 实现一个手机号码获取妹子名字的功能
Sep 25 Python
python中的线程threading.Thread()使用详解
Dec 17 Python
Python如何测试stdout输出
Aug 10 Python
python爬虫快速响应服务器的做法
Nov 24 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+AJAX传送中文会导致乱码的问题的解决方法
2008/09/08 PHP
超小PHP小马小结(方便查找后门的朋友)
2012/05/05 PHP
php根据分类合并数组的方法实例详解
2013/11/06 PHP
PHP 面向对象程序设计(oop)学习笔记(三) - 单例模式和工厂模式
2014/06/12 PHP
PHP基本语法总结
2014/09/06 PHP
php实现获取文件mime类型的方法
2015/02/11 PHP
php封装的验证码类分享
2017/02/26 PHP
thinkphp5.1 文件引入路径问题及注意事项
2018/06/13 PHP
Ubuntu 16.04中Laravel5.4升级到5.6的步骤
2018/12/07 PHP
用js统计用户下载网页所需时间的脚本
2008/10/15 Javascript
jsTree树控件(基于jQuery, 超强悍)[推荐]
2009/09/01 Javascript
javascript 解析url的search方法
2010/02/09 Javascript
jQuery在vs2008及js文件中的无智能提示的解决方法
2010/12/30 Javascript
防止xss和sql注入:JS特殊字符过滤正则
2013/04/18 Javascript
JS特殊函数(Function()构造函数、函数直接量)区别介绍
2013/05/19 Javascript
Js 正则表达式知识汇总
2014/12/02 Javascript
JavaScript ES6中CLASS的使用详解
2016/11/22 Javascript
vue中,在本地缓存中读写数据的方法
2018/09/21 Javascript
小程序角标的添加及绑定购物车数量进行实时更新的实现代码
2020/12/07 Javascript
常见的在Python中实现单例模式的三种方法
2015/04/08 Python
python使用socket向客户端发送数据的方法
2015/04/29 Python
Python处理PDF及生成多层PDF实例代码
2017/04/24 Python
Python 操作文件的基本方法总结
2017/08/10 Python
TensorFlow实现随机训练和批量训练的方法
2018/04/28 Python
详解python 注释、变量、类型
2018/08/10 Python
通过实例简单了解Python中yield的作用
2019/12/11 Python
python中的itertools的使用详解
2020/01/13 Python
CSS3的column-fill属性对齐列内容高度的用法详解
2016/07/01 HTML / CSS
丝芙兰法国官网:SEPHORA法国
2016/09/01 全球购物
加拿大最大的书店:Indigo
2017/01/01 全球购物
如何现实servlet的单线程模式
2014/08/05 面试题
先进工作者获奖感言
2014/02/08 职场文书
2019年教师节祝福语精选,给老师送上真诚的祝福
2019/09/09 职场文书
opencv 分类白天与夜景视频的方法
2021/06/05 Python
【js设计模式】SOLID五大设计原则
2022/03/24 Javascript
ant design charts 获取后端接口数据展示
2022/05/25 Javascript