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计算牛顿迭代多项式实例分析
May 07 Python
深入理解Python变量与常量
Jun 02 Python
基于python脚本实现软件的注册功能(机器码+注册码机制)
Oct 09 Python
Python实现简单的四则运算计算器
Nov 02 Python
Python模拟简单电梯调度算法示例
Aug 20 Python
对Python 3.2 迭代器的next函数实例讲解
Oct 18 Python
Python读取Pickle文件信息并计算与当前时间间隔的方法分析
Jan 30 Python
Python3中_(下划线)和__(双下划线)的用途和区别
Apr 26 Python
Python爬虫 批量爬取下载抖音视频代码实例
Aug 16 Python
Python使用贪婪算法解决问题
Oct 22 Python
Python 中使用 PyMySQL模块操作数据库的方法
Nov 10 Python
使用pymysql查询数据库,把结果保存为列表并获取指定元素下标实例
May 15 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/06/09 PHP
php批量删除cookie的简单实现方法
2015/01/26 PHP
PHP实现QQ登录的开原理和实现过程
2018/02/04 PHP
PHP PDOStatement::setAttribute讲解
2019/02/01 PHP
PHP实现简单日历类编写
2020/08/28 PHP
IE 缓存策略的BUG的解决方法
2007/07/21 Javascript
jquery入门必备的基本认识及实例(整理)
2013/06/24 Javascript
JS的参数传递示例介绍
2014/02/08 Javascript
jQuery中:not选择器用法实例
2014/12/30 Javascript
深入理解JavaScript系列(45):代码复用模式(避免篇)详解
2015/03/04 Javascript
基于BootStrap的Metronic框架实现页面链接收藏夹功能按钮移动收藏记录(使用Sortable进行拖动排序)
2016/08/29 Javascript
JS封装的选项卡TAB切换效果示例
2016/09/20 Javascript
jQuery基于ajax方式实现用户名存在性检查功能示例
2017/02/10 Javascript
js从输入框读取内容,比较两个数字的大小方法
2017/03/13 Javascript
vue指令之表单控件绑定v-model v-model与v-bind结合使用
2019/04/17 Javascript
layer提示框添加多个按钮选择的实例
2019/09/12 Javascript
JavaScript中如何调用Java方法
2020/09/16 Javascript
python的几种开发工具介绍
2007/03/07 Python
举例讲解Django中数据模型访问外键值的方法
2015/07/21 Python
浅谈pycharm出现卡顿的解决方法
2018/12/03 Python
Python多图片合并PDF的方法
2019/01/03 Python
python 非线性规划方式(scipy.optimize.minimize)
2020/02/11 Python
pycharm新建Vue项目的方法步骤(图文)
2020/03/04 Python
Python基于数列实现购物车程序过程详解
2020/06/09 Python
python 中的9个实用技巧,助你提高开发效率
2020/08/30 Python
波比布朗英国官网:Bobbi Brown英国
2017/11/13 全球购物
鞋子女王塔玛拉·梅隆同名奢侈品牌:Tamara Mellon
2017/11/22 全球购物
超级英雄、电影和电视、乐队和音乐T恤:Loud Clothing
2019/09/01 全球购物
应届大学生自荐信格式
2013/09/21 职场文书
周年庆典答谢词
2015/01/20 职场文书
专家推荐信怎么写
2015/03/25 职场文书
2015年团支部工作总结
2015/04/03 职场文书
十月围城观后感
2015/06/08 职场文书
郭明义观后感
2015/06/08 职场文书
公司回复函格式
2015/07/14 职场文书
2016年大学生社区服务活动总结
2016/04/06 职场文书