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操作字符串之rindex()方法的使用
May 19 Python
python操作excel的方法(xlsxwriter包的使用)
Jun 11 Python
Python日期时间对象转换为字符串的实例
Jun 22 Python
Python Tkinter模块实现时钟功能应用示例
Jul 23 Python
如何用Python来搭建一个简单的推荐系统
Aug 07 Python
python自动生成model文件过程详解
Nov 02 Python
pytorch:torch.mm()和torch.matmul()的使用
Dec 27 Python
根据tensor的名字获取变量的值方式
Jan 04 Python
Python获取、格式化当前时间日期的方法
Feb 10 Python
Python3和PyCharm安装与环境配置【图文教程】
Feb 14 Python
Python新手如何理解循环加载模块
May 29 Python
selenium自动化测试入门实战
Dec 21 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获取MySql新增记录ID值的3种方法
2014/06/24 PHP
php上传图片生成缩略图(GD库)
2016/01/06 PHP
JQuery1.6 使用方法三
2011/11/23 Javascript
实用的JS正则表达式(手机号码/IP正则/邮编正则/电话等)
2013/01/11 Javascript
javascript从右边截取指定字符串的三种实现方法
2013/11/29 Javascript
JavaScript解析json格式数据简单示例
2014/12/09 Javascript
JQuery设置时间段下拉选择实例
2014/12/30 Javascript
JQuery操作元素的css样式
2015/03/09 Javascript
nodejs中实现阻塞实例
2015/03/24 NodeJs
18个非常棒的jQuery代码片段
2015/11/02 Javascript
AngularJS 与百度地图的结合实例
2016/10/20 Javascript
javascript编程实现栈的方法详解【经典数据结构】
2017/04/11 Javascript
JavaScript的六种继承方式(推荐)
2017/06/26 Javascript
使用live-server快速搭建本地服务器+自动刷新的方法
2018/03/09 Javascript
vue自定义指令的创建和使用方法实例分析
2018/12/04 Javascript
[jQuery] 事件和动画详解
2019/03/05 jQuery
jQuery添加新内容的四个常用方法分析【append,prepend,after,before】
2019/03/19 jQuery
搭建Vue从Vue-cli到router路由护卫的实现
2019/11/14 Javascript
实例讲解React 组件生命周期
2020/07/08 Javascript
vue监听浏览器原生返回按钮,进行路由转跳操作
2020/09/09 Javascript
python正则表达式re模块详解
2014/06/25 Python
举例讲解Python中metaclass元类的创建与使用
2016/06/30 Python
python实现批量监控网站
2016/09/09 Python
解决新版Pycharm中Matplotlib图像不在弹出独立的显示窗口问题
2019/01/15 Python
Python 操作 PostgreSQL 数据库示例【连接、增删改查等】
2020/04/21 Python
突袭HTML5之Javascript API扩展1—Web Worker异步执行及相关概述
2013/01/31 HTML / CSS
使用html2canvas.js实现页面截图并显示或上传的示例代码
2018/12/18 HTML / CSS
加拿大奢华时装品牌:Mackage
2018/01/10 全球购物
毕业生求职简历的自我评价
2013/10/07 职场文书
新郎婚宴答谢词
2014/01/19 职场文书
打造完美自荐信
2014/01/24 职场文书
投标承诺书范本
2014/03/27 职场文书
《英英学古诗》教学反思
2014/04/11 职场文书
经济信息系毕业生自荐信
2014/06/02 职场文书
2014工程部年度工作总结
2014/12/17 职场文书
2019年暑期法院实习报告
2019/12/18 职场文书