Python群发邮件实例代码


Posted in Python onJanuary 03, 2014

直接上代码了

import smtplib
msg = MIMEMultipart()
#构造附件1
att1 = MIMEText(open('/home/a2bgeek/develop/python/hello.py', 'rb').read(), 'base64', 'gb2312')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="hello.txt"'#这里的filename可以任意写,写什么名字,邮件中显示什么名字
msg.attach(att1)
#构造附件2
#att2 = MIMEText(open('/home/a2bgeek/develop/python/mail.py', 'rb').read(), 'base64', 'gb2312')
#att2["Content-Type"] = 'application/octet-stream'
#att2["Content-Disposition"] = 'attachment; filename="123.txt"'
#msg.attach(att2)
#加邮件头
strTo = ['XXX1@139.com', 'XXX2@163.com', 'XXX3@126.com']
msg['to']=','.join(strTo)
msg['from'] = 'YYY@163.com'
msg['subject'] = '邮件主题'
#发送邮件
try:
    server = smtplib.SMTP()
    server.connect('smtp.163.com')
    server.login('YYY@163.com','yourpasswd')
    server.sendmail(msg['from'], strTo ,msg.as_string())
    server.quit()
    print '发送成功'
except Exception, e:
    print str(e)

细心的读者会发现代码中有这样一句:msg['to']=','.join(strTo),但是msg[['to']并没有在后面被使用,这么写明显是不合理的,但是这就是stmplib的bug。你只有这样写才能群发邮件。查明原因如下:

The problem is that SMTP.sendmail and email.MIMEText need two different things.

email.MIMEText sets up the “To:” header for the body of the e-mail. It is ONLY used for displaying a result to the human being at the other end, and like all e-mail headers, must be a single string. (Note that it does not actually have to have anything to do with the people who actually receive the message.)

SMTP.sendmail, on the other hand, sets up the “envelope” of the message for the SMTP protocol. It needs a Python list of strings, each of which has a single address.

So, what you need to do is COMBINE the two replies you received. Set msg‘To' to a single string, but pass the raw list to sendmail.

好了今天就到这里。

Python 相关文章推荐
python使用fileinput模块实现逐行读取文件的方法
Apr 29 Python
Python实现一个转存纯真IP数据库的脚本分享
May 21 Python
11月编程语言排行榜 Python逆袭C#上升到第4
Nov 15 Python
Python搜索引擎实现原理和方法
Nov 27 Python
Python跨文件全局变量的实现方法示例
Dec 10 Python
python抓取京东小米8手机配置信息
Nov 13 Python
python实现AES和RSA加解密的方法
Mar 28 Python
python每5分钟从kafka中提取数据的例子
Dec 23 Python
pytorch中tensor张量数据类型的转化方式
Dec 31 Python
Python基于pillow库实现生成图片水印
Sep 14 Python
python实现猜拳游戏项目
Nov 30 Python
python井字棋游戏实现人机对战
Apr 28 Python
python切换hosts文件代码示例
Dec 31 #Python
使用Python进行稳定可靠的文件操作详解
Dec 31 #Python
python连接mongodb操作数据示例(mongodb数据库配置类)
Dec 31 #Python
python连接mysql数据库示例(做增删改操作)
Dec 31 #Python
Python抓取Discuz!用户名脚本代码
Dec 30 #Python
python之模拟鼠标键盘动作具体实现
Dec 30 #Python
python多线程http下载实现示例
Dec 30 #Python
You might like
php连接数据库代码应用分析
2011/05/29 PHP
php mysql 判断update之后是否更新了的方法
2012/01/10 PHP
PHP图片验证码制作实现分享(全)
2012/05/10 PHP
Symfony控制层深入详解
2016/03/17 PHP
谷歌浏览器 insertCell与appendChild的区别
2009/02/12 Javascript
JavaScript实现页面滚动图片加载(仿lazyload效果)
2011/07/22 Javascript
js中有关IE版本检测
2012/01/04 Javascript
用JS将搜索的关键字高亮显示实现代码
2013/11/08 Javascript
javascript删除option选项的多种方法总结
2013/11/22 Javascript
非常实用的12个jquery代码片段
2015/11/02 Javascript
BootStrap的Datepicker控件使用心得分享
2016/05/25 Javascript
js遍历map javaScript遍历map的简单实现
2016/08/26 Javascript
jQuery实现给input绑定回车事件的方法
2017/02/09 Javascript
jQuery dateRangePicker插件使用方法详解
2017/07/28 jQuery
Angular 开发学习之Angular CLI的安装使用
2017/12/31 Javascript
vue里面v-bind和Props 利用props绑定动态数据的方法
2018/08/27 Javascript
webpack 如何解析代码模块路径的实现
2019/09/04 Javascript
vue自定义组件实现双向绑定
2021/01/13 Vue.js
[25:59]Newbee vs TNC 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
python实现判断数组是否包含指定元素的方法
2015/07/15 Python
qpython3 读取安卓lastpass Cookies
2016/06/19 Python
python多线程socket编程之多客户端接入
2017/09/12 Python
Python实现希尔排序算法的原理与用法实例分析
2017/11/23 Python
在python中利用GDAL对tif文件进行读写的方法
2018/11/29 Python
Python中extend和append的区别讲解
2019/01/24 Python
如何在 Django 模板中输出 "{{"
2020/01/24 Python
python Gabor滤波器讲解
2020/10/26 Python
python matplotlib工具栏源码探析二之添加、删除内置工具项的案例
2021/02/25 Python
ZWILLING双立人英国网上商店:德国刀具锅具厨具品牌
2018/05/15 全球购物
开办化妆品公司创业计划书
2013/12/26 职场文书
教学大赛获奖感言
2014/01/15 职场文书
买房委托公证书
2014/04/08 职场文书
毕业设计说明书
2014/05/07 职场文书
2014年药品销售工作总结
2014/12/16 职场文书
电力安全学习心得体会
2016/01/18 职场文书
7个关于Python的经典基础案例
2021/11/07 Python