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 Web框架Pylons中使用MongoDB的例子
Dec 03 Python
Python自定义scrapy中间模块避免重复采集的方法
Apr 07 Python
python 打印出所有的对象/模块的属性(实例代码)
Sep 11 Python
Python使用中文正则表达式匹配指定中文字符串的方法示例
Jan 20 Python
Python列表list内建函数用法实例分析【insert、remove、index、pop等】
Jul 24 Python
使用requests库制作Python爬虫
Mar 25 Python
Python重新加载模块的实现方法
Oct 16 Python
Python面向对象之类的定义与继承用法示例
Jan 14 Python
Python 根据日志级别打印不同颜色的日志的方法示例
Aug 08 Python
Python list运算操作代码实例解析
Jan 20 Python
python Polars库的使用简介
Apr 21 Python
Python 文本滚动播放器的实现代码
Apr 25 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加入ftp扩展的解决方法
2013/02/07 PHP
PHP_SELF,SCRIPT_NAME,REQUEST_URI区别
2014/12/24 PHP
微信公众平台实现获取用户OpenID的方法
2015/04/15 PHP
yii2中的rules 自定义验证规则详解
2016/04/19 PHP
php显示页码分页类的封装
2017/06/08 PHP
非常漂亮的JS代码经典广告
2007/10/21 Javascript
jquery实现网站超链接和图片提示效果
2013/03/21 Javascript
jquery实现table鼠标经过变色代码
2013/09/25 Javascript
js实现的后台左侧管理菜单代码
2015/09/11 Javascript
浅析JavaScript访问对象属性和方法及区别
2015/11/16 Javascript
讲解JavaScript的Backbone.js框架的MVC结构设计理念
2016/02/14 Javascript
深入理解JavaScript程序中内存泄漏
2016/03/17 Javascript
JavaScript用200行代码制作打飞机小游戏实例
2017/06/21 Javascript
jQuery实现常见的隐藏与展示列表效果示例
2018/06/04 jQuery
使用layui监听器监听select下拉框,事件绑定不成功的解决方法
2019/09/28 Javascript
浅谈vue项目,访问路径#号的问题
2020/08/14 Javascript
Python写的Socks5协议代理服务器
2014/08/06 Python
python下setuptools的安装详解及No module named setuptools的解决方法
2017/07/06 Python
python进程管理工具supervisor的安装与使用教程
2017/09/05 Python
Python去除、替换字符串空格的处理方法
2018/04/01 Python
python django框架中使用FastDFS分布式文件系统的安装方法
2019/06/10 Python
Python字符串处理的8招秘籍(小结)
2019/08/13 Python
Python list和str互转的实现示例
2020/11/16 Python
Python django框架 web端视频加密的实例详解
2020/11/20 Python
phpquery中文手册
2021/03/18 PHP
Chemist Warehouse官方海外旗舰店:澳洲第一连锁大药房
2017/08/25 全球购物
人力资源主管职责范本
2014/03/05 职场文书
党员公开承诺书范文
2014/03/25 职场文书
环卫工人节活动总结
2014/08/29 职场文书
师德师风自查材料
2014/10/14 职场文书
党的群众路线教育实践活动个人批评与自我批评
2014/10/16 职场文书
2014年小学工作总结
2014/11/26 职场文书
2015年项目工作总结
2015/04/29 职场文书
圣诞晚会主持词开场白
2015/05/28 职场文书
永不妥协观后感
2015/06/10 职场文书
Meta增速拉垮,元宇宙难当重任
2022/04/29 数码科技