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中遍历文件的3个方法
Sep 02 Python
深入理解python多进程编程
Jun 12 Python
最近Python有点火? 给你7个学习它的理由!
Jun 26 Python
python通过socket实现多个连接并实现ssh功能详解
Nov 08 Python
Python实现读取txt文件并画三维图简单代码示例
Dec 09 Python
python实现机器人行走效果
Jan 29 Python
Python日期时间对象转换为字符串的实例
Jun 22 Python
pandas 使用均值填充缺失值列的小技巧分享
Jul 04 Python
python机器学习库xgboost的使用
Jan 20 Python
Django中的模型类设计及展示示例详解
May 29 Python
详解python logging日志传输
Jul 01 Python
一个非常简单好用的Python图形界面库(PysimpleGUI)
Dec 28 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 静态化实现代码
2009/03/20 PHP
php求数组全排列,元素所有组合的方法总结
2017/03/14 PHP
Yii框架实现记录日志到自定义文件的方法
2017/05/23 PHP
PHP多进程编程实例详解
2017/07/19 PHP
jQuery中使用了document和window哪些属性和方法小结
2011/09/13 Javascript
jQuery去掉字符串起始和结尾的空格(多种方法实现)
2013/04/01 Javascript
非常好用的JsonToString 方法 简单实例
2013/07/18 Javascript
Javascript表格翻页效果的具体实现
2013/10/05 Javascript
JS实现跟随鼠标立体翻转图片的方法
2015/05/04 Javascript
jQuery+HTML5+CSS3制作支持响应式布局时间轴插件
2016/08/10 Javascript
浅谈移动端之js touch事件 手势滑动事件
2016/11/07 Javascript
js中string和number类型互转换技巧(分享)
2016/11/28 Javascript
Javascript this 函数深入详解
2016/12/13 Javascript
angular ng-repeat数组中的数组实例
2017/02/18 Javascript
ionic 自定义弹框效果
2017/06/27 Javascript
学习使用ExpressJS 4.0中的新Router的用法
2018/11/06 Javascript
vue模仿网易云音乐的单页面应用
2019/04/24 Javascript
何时/使用 Vue3 render 函数的教程详解
2020/07/25 Javascript
原生js实现自定义滚动条组件
2021/01/20 Javascript
[04:42]2015国际邀请赛CDEC战队晋级之路
2015/08/13 DOTA
[43:58]DOTA2上海特级锦标赛C组败者赛 Newbee VS Archon第二局
2016/02/27 DOTA
20招让你的Python飞起来!
2016/09/27 Python
在CentOS6上安装Python2.7的解决方法
2018/01/09 Python
Python基于ThreadingTCPServer创建多线程代理的方法示例
2018/01/11 Python
python numpy 一维数组转变为多维数组的实例
2018/07/02 Python
对numpy中向量式三目运算符详解
2018/10/31 Python
Python代码实现http/https代理服务器的脚本
2019/08/12 Python
Python 图像对比度增强的几种方法(小结)
2019/09/25 Python
CSS3等相关属性制作分页导航实现代码
2012/12/24 HTML / CSS
亲子拓展活动方案
2014/02/20 职场文书
小学生竞选班干部演讲稿
2014/04/24 职场文书
安全责任书范文
2014/08/25 职场文书
党委书记个人对照检查材料
2014/09/15 职场文书
2019年农民幸福观调查的实践感悟
2019/12/19 职场文书
MySQL中utf8mb4排序规则示例
2021/08/02 MySQL
html粘性页脚的具体使用
2022/01/18 HTML / CSS