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使用Flask框架获取用户IP地址的方法
Mar 21 Python
Python中的高级数据结构详解
Mar 27 Python
深入理解Python中各种方法的运作原理
Jun 15 Python
python使用电子邮件模块smtplib的方法
Aug 28 Python
Python中GIL的使用详解
Oct 03 Python
Django组件cookie与session的具体使用
Jun 05 Python
Python button选取本地图片并显示的实例
Jun 13 Python
Python + OpenCV 实现LBP特征提取的示例代码
Jul 11 Python
python爬虫学习笔记之pyquery模块基本用法详解
Apr 09 Python
python 3.8.3 安装配置图文教程
May 21 Python
Python 通过正则表达式快速获取电影的下载地址
Aug 17 Python
pycharm永久激活超详细教程
Oct 29 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产生随机字符串函数
2006/12/06 PHP
提高define性能的php扩展hidef的安装和使用
2011/06/14 PHP
dedecms中使用php语句指南
2014/11/13 PHP
两个php日期控制类实例
2014/12/09 PHP
php基于自定义函数记录log日志方法
2017/07/21 PHP
PHP基于curl post实现发送url及相关中文乱码问题解决方法
2017/11/25 PHP
Laravel 自带的Auth验证登录方法
2019/09/30 PHP
Javascript 判断客户端浏览器类型代码
2010/03/01 Javascript
js实现select跳转功能代码
2014/10/22 Javascript
angular.js分页代码的实例
2016/07/27 Javascript
浅谈js对象的创建和对6种继承模式的理解和遐想
2016/10/16 Javascript
JS常见算法详解
2017/02/28 Javascript
深入理解Angular4中的依赖注入
2017/06/07 Javascript
Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object’#‘的解决方法
2017/06/17 Javascript
Vue调试神器vue-devtools安装方法
2017/12/12 Javascript
仿京东快报向上滚动的实例
2017/12/13 Javascript
vue自动化表单实例分析
2018/05/06 Javascript
详解Vue之事件处理
2020/07/10 Javascript
多线程爬虫批量下载pcgame图片url 保存为xml的实现代码
2013/01/17 Python
Python判断操作系统类型代码分享
2014/11/22 Python
详解Python中time()方法的使用的教程
2015/05/22 Python
Python使用functools模块中的partial函数生成偏函数
2016/07/02 Python
详谈python在windows中的文件路径问题
2018/04/28 Python
简单了解python反射机制的一些知识
2019/07/13 Python
flask 实现上传图片并缩放作为头像的例子
2020/01/09 Python
CSS3盒子模型详解
2013/04/24 HTML / CSS
5分钟弄清楚html5的drag and drop(小结)
2019/04/10 HTML / CSS
阿迪达斯墨西哥官方网站:adidas墨西哥
2017/11/03 全球购物
西班牙品牌鞋子、服装和配饰在线商店:Esdemarca
2021/02/17 全球购物
C有"按引用传递"吗
2016/09/06 面试题
程序员机试试题汇总
2012/03/07 面试题
迟到检讨书400字
2014/01/13 职场文书
幼儿园中班教师个人工作总结
2015/02/06 职场文书
学习党章心得体会2016
2016/01/15 职场文书
python实现简单的名片管理系统
2021/04/26 Python
Python中json.dumps()函数的使用解析
2021/05/17 Python