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实现一个简单的能够发送带附件的邮件程序的教程
Apr 08 Python
利用Python的Twisted框架实现webshell密码扫描器的教程
Apr 16 Python
python实现内存监控系统
Mar 07 Python
Flask配置Cors跨域的实现
Jul 12 Python
pyinstaller打包程序exe踩过的坑
Nov 19 Python
python使用配置文件过程详解
Dec 28 Python
Python使用Tkinter实现滚动抽奖器效果
Jan 06 Python
Python使用Pandas库常见操作详解
Jan 16 Python
python通用读取vcf文件的类(复制粘贴即可用)
Feb 29 Python
pycharm-professional-2020.1下载与激活的教程
Sep 21 Python
python调用摄像头的示例代码
Sep 28 Python
详解python的变量缓存机制
Jan 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通过array_merge()函数合并两个数组的方法
2015/03/18 PHP
两种php实现图片上传的方法
2016/01/22 PHP
Win10 下安装配置IIS + MySQL + nginx + php7.1.7
2017/08/04 PHP
详解PHP使用日期时间处理器Carbon人性化显示时间
2017/08/10 PHP
解密效果
2006/06/23 Javascript
详谈 Jquery Ajax异步处理Json数据.
2011/09/09 Javascript
table insertRow、deleteRow定义和用法总结
2014/05/14 Javascript
最全的Javascript编码规范(推荐)
2016/06/22 Javascript
浅谈js基本数据类型和typeof
2016/08/09 Javascript
针对后台列表table拖拽比较实用的jquery拖动排序
2016/10/10 Javascript
JS匿名函数实例分析
2016/11/26 Javascript
jQuery取得元素标签名称小结(附代码)
2017/08/16 jQuery
解决Vue不能检测数组或对象变动的问题
2018/02/24 Javascript
微信小程序实现手指触摸画板
2018/07/09 Javascript
element-ui 关于获取select 的label值方法
2018/08/24 Javascript
浅谈在vue中使用mint-ui swipe遇到的问题
2018/09/27 Javascript
详解js静态检查工具eslint配置文件
2018/11/23 Javascript
javascript实现遮罩层动态效果实例
2019/05/14 Javascript
微信小程序页面间跳转传参方式总结
2019/06/13 Javascript
Promise扫盲贴
2019/06/24 Javascript
python Django批量导入不重复数据
2016/03/25 Python
Python使用monkey.patch_all()解决协程阻塞问题
2020/04/15 Python
解析Tensorflow之MNIST的使用
2020/06/30 Python
CSS3中Transition动画属性用法详解
2016/07/04 HTML / CSS
HTML5 背景的显示区域实现
2020/07/09 HTML / CSS
应聘教师自荐信
2013/10/12 职场文书
八年级历史教学反思
2014/01/10 职场文书
毕业评语大全
2014/05/04 职场文书
养牛场项目建议书
2014/05/13 职场文书
环保建议书200字
2014/05/14 职场文书
学生检讨书怎么写
2015/05/07 职场文书
“5.12”护士节主持词
2015/07/04 职场文书
读《教育心理学》心得体会
2016/01/22 职场文书
《中华上下五千年》读后感3篇
2019/11/29 职场文书
pytorch 一行代码查看网络参数总量的实现
2021/05/12 Python
MySql 8.0及对应驱动包匹配的注意点说明
2021/06/23 MySQL