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 相关文章推荐
数据挖掘之Apriori算法详解和Python实现代码分享
Nov 07 Python
用生成器来改写直接返回列表的函数方法
May 25 Python
浅谈python中的正则表达式(re模块)
Oct 17 Python
python 通过字符串调用对象属性或方法的实例讲解
Apr 21 Python
python中pip的安装与使用教程
Aug 10 Python
对Python中class和instance以及self的用法详解
Jun 26 Python
python获取点击的坐标画图形的方法
Jul 09 Python
pyinstaller还原python代码过程图解
Jan 08 Python
Python切割图片成九宫格的示例代码
Mar 10 Python
python解包概念及实例
Feb 17 Python
Python中快速掌握Data Frame的常用操作
Mar 31 Python
tensorflow学习笔记之tfrecord文件的生成与读取
Mar 31 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/06/09 PHP
解析PHP SPL标准库的用法(遍历目录,查找固定条件的文件)
2013/06/18 PHP
Jquery之Ajax运用 学习运用篇
2011/09/26 Javascript
js获取本机的外网/广域网ip地址完整源码
2013/08/12 Javascript
跟我学Nodejs(一)--- Node.js简介及安装开发环境
2014/05/20 NodeJs
jQuery源码解读之removeClass()方法分析
2015/02/20 Javascript
JS对字符串编码的几种方式使用指南
2015/05/14 Javascript
vue2 中如何实现动态表单增删改查实例
2017/06/09 Javascript
vue项目设置scrollTop不起作用(总结)
2018/12/21 Javascript
vue实现局部刷新的实现示例
2019/04/16 Javascript
electron-vue开发环境内存泄漏问题汇总
2019/10/10 Javascript
Vue中多元素过渡特效的解决方案
2020/02/05 Javascript
Python Web开发模板引擎优缺点总结
2014/05/06 Python
Python语言实现获取主机名根据端口杀死进程
2016/03/31 Python
详解Python装饰器由浅入深
2016/12/09 Python
django2 快速安装指南分享
2018/01/05 Python
基于python内置函数与匿名函数详解
2018/01/09 Python
python3+PyQt5实现拖放功能
2018/04/24 Python
Python用61行代码实现图片像素化的示例代码
2018/12/10 Python
对python中类的继承与方法重写介绍
2019/01/20 Python
python批量修改图片尺寸,并保存指定路径的实现方法
2019/07/04 Python
Jupyter notebook设置背景主题,字体大小及自动补全代码的操作
2020/04/13 Python
Python定时任务APScheduler原理及实例解析
2020/05/30 Python
Python识别处理照片中的条形码
2020/11/16 Python
Canvas 文字碰撞检测并抽稀的方法
2019/05/27 HTML / CSS
canvas画布实现手写签名效果的示例代码
2019/04/23 HTML / CSS
美国在线家装零售商:Build.com
2016/09/02 全球购物
高职助产应届生自荐信
2013/09/24 职场文书
运动会开幕式邀请函
2014/02/03 职场文书
《池塘边的叫声》教学反思
2014/04/12 职场文书
党的群众路线教育实践方案
2014/05/11 职场文书
烹饪大赛策划方案
2014/05/26 职场文书
个人总结与自我评价
2015/02/14 职场文书
2015年护士医德医风自我评价
2015/03/03 职场文书
入党自荐书范文
2015/03/05 职场文书
科学家研发出新型速效酶,可在 24 小时内降解塑料制品
2022/04/29 数码科技