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实现的简单hangman游戏实例
Jun 28 Python
详解Python3中yield生成器的用法
Aug 20 Python
python 把数据 json格式输出的实例代码
Oct 31 Python
python僵尸进程产生的原因
Jul 21 Python
python模块之time模块(实例讲解)
Sep 13 Python
python通过微信发送邮件实现电脑关机
Jun 20 Python
如何使用Python进行OCR识别图片中的文字
Apr 01 Python
Django中Middleware中的函数详解
Jul 18 Python
python实现简单的井字棋游戏(gui界面)
Jan 22 Python
python之openpyxl模块的安装和基本用法(excel管理)
Feb 03 Python
python线程优先级队列知识点总结
Feb 28 Python
关于Python中进度条的六个实用技巧分享
Apr 05 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
不用iconv库的gb2312与utf-8的互换函数
2006/10/09 PHP
3种平台下安装php4经验点滴
2006/10/09 PHP
php导入大量数据到mysql性能优化技巧
2014/12/29 PHP
Laravel使用Caching缓存数据减轻数据库查询压力的方法
2016/03/15 PHP
Nginx下ThinkPHP5的配置方法详解
2017/08/01 PHP
Yii框架日志操作图文与实例详解
2019/09/09 PHP
tp5.0框架隐藏index.php入口文件及模块和控制器的方法分析
2020/02/11 PHP
js和jquery批量绑定事件传参数一(新猪猪原创)
2010/06/23 Javascript
基于jquery的图片的切换(以数字的形式)
2011/02/14 Javascript
js页面滚动时层智能浮动定位实现(jQuery/MooTools)
2011/08/23 Javascript
jquery 实现二级/三级/多级联动菜单的思路及代码
2013/04/08 Javascript
Node.js的MongoDB驱动Mongoose基本使用教程
2016/03/01 Javascript
Bootstrap弹出框(modal)垂直居中的问题及解决方案详解
2016/06/12 Javascript
利用Angularjs和bootstrap实现购物车功能
2016/08/31 Javascript
JavaScript设计模式之策略模式详解
2017/06/09 Javascript
解决Vue2.0自带浏览器里无法打开的原因(兼容处理)
2017/07/28 Javascript
Validform验证时可以为空否则按照指定格式验证
2017/10/20 Javascript
vue 点击按钮增加一行的方法
2018/09/07 Javascript
深入解析ES6中的promise
2018/11/08 Javascript
jQuery 动画与停止动画效果实例详解
2020/05/19 jQuery
使用python调用浏览器并打开一个网址的例子
2014/06/05 Python
python实现多线程抓取知乎用户
2016/12/12 Python
Python语言描述连续子数组的最大和
2018/01/04 Python
Python闭包思想与用法浅析
2018/12/27 Python
在pycharm中设置显示行数的方法
2019/01/16 Python
Pandas中Series和DataFrame的索引实现
2019/06/27 Python
Selenium python时间控件输入问题解决方案
2020/07/22 Python
HTML5之SVG 2D入门6—视窗坐标系与用户坐标系及变换概述
2013/01/30 HTML / CSS
加拿大国民体育购物网站:National Sports
2018/11/04 全球购物
佳能法国商店:Canon法国
2019/02/14 全球购物
Linux如何为某个操作添加别名
2013/03/01 面试题
中专生毕业自我鉴定
2013/11/01 职场文书
消防安全检查制度
2014/02/04 职场文书
运动会领导邀请函
2014/02/05 职场文书
2014教师年度思想工作总结
2014/11/10 职场文书
开天辟地观后感
2015/06/09 职场文书