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中的split()函数的使用方法
Apr 07 Python
浅谈python对象数据的读写权限
Sep 12 Python
NLTK 3.2.4 环境搭建教程
Sep 19 Python
详解python爬虫系列之初识爬虫
Apr 06 Python
python实现名片管理系统项目
Apr 26 Python
selenium跳过webdriver检测并模拟登录淘宝
Jun 12 Python
Python何时应该使用Lambda函数
Jul 02 Python
Python环境使用OpenCV检测人脸实现教程
Oct 19 Python
python实现文件分片上传的接口自动化
Nov 19 Python
python生成word合同的实例方法
Jan 12 Python
神经网络训练采用gpu设置的方式
Mar 03 Python
Python Pandas数据分析之iloc和loc的用法详解
Nov 11 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在Web开发领域的优势
2006/10/09 PHP
php foreach、while性能比较
2009/10/15 PHP
php环境配置之CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI比较?
2011/10/17 PHP
使用PHP+Redis实现延迟任务,实现自动取消订单功能
2019/11/21 PHP
jQuery AnythingSlider滑动效果插件
2010/02/07 Javascript
基于Jquery的仿Windows Aero弹出窗(漂亮的关闭按钮)
2010/09/28 Javascript
AJAX分页的代码(后台asp.net)
2011/02/14 Javascript
javascript字符串替换及字符串分割示例代码
2013/12/12 Javascript
JavaScript实现找质数代码分享
2015/03/24 Javascript
Javascript 实现微信分享(QQ、朋友圈、分享给朋友)
2016/10/21 Javascript
vue之数据交互实例代码
2017/06/20 Javascript
浅谈ECMAScript6新特性之let、const
2017/08/02 Javascript
微信小程序实现给嵌套template模板传递数据的方式总结
2017/12/18 Javascript
Nuxt升级2.0.0时出现的问题(小结)
2018/10/08 Javascript
vue src动态加载请求获取图片的方法
2018/10/17 Javascript
IE9 elementUI文件上传的问题解决
2018/10/17 Javascript
vue 项目地址去掉 #的方法
2018/10/20 Javascript
NodeJs入门教程之定时器和队列
2019/03/08 NodeJs
JavaScript实现拖拽盒子效果
2020/02/06 Javascript
JS前端面试必备——基本排序算法原理与实现方法详解【插入/选择/归并/冒泡/快速排序】
2020/02/24 Javascript
Python标准库内置函数complex介绍
2014/11/25 Python
Python实现高效求解素数代码实例
2015/06/30 Python
详解Django中Request对象的相关用法
2015/07/17 Python
Python画图高斯分布的示例
2019/07/10 Python
Python二元赋值实用技巧解析
2019/10/25 Python
Python列表去重复项的N种方法(实例代码)
2020/05/12 Python
利用CSS3实现单选框动画特效示例代码
2016/09/26 HTML / CSS
HTML5 CSS3实现一个精美VCD包装盒个性幻灯片案例
2014/06/16 HTML / CSS
Petmate品牌官方网站:宠物用品
2018/11/25 全球购物
幼儿园中秋节活动方案2013
2014/01/29 职场文书
高级编程求职信模板
2014/02/16 职场文书
乡镇纠风工作实施方案
2014/03/22 职场文书
法人授权委托书公证范本
2014/09/14 职场文书
销售人员工作自我评价
2014/09/21 职场文书
2016年教师节特级教师获奖感言
2015/12/09 职场文书
详解Python魔法方法之描述符类
2021/05/26 Python