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中操作文件之seek()方法的使用教程
May 24 Python
python实现Floyd算法
Jan 03 Python
python3.6+opencv3.4实现鼠标交互查看图片像素
Feb 26 Python
Python反转序列的方法实例分析
Mar 21 Python
Linux 修改Python命令的方法示例
Dec 03 Python
使用Django连接Mysql数据库步骤
Jan 15 Python
python使用socket实现的传输demo示例【基于TCP协议】
Sep 24 Python
matlab中二维插值函数interp2的使用详解
Apr 22 Python
Python实现LR1文法的完整实例代码
Oct 25 Python
Pycharm安装python库的方法
Nov 24 Python
一文搞懂如何实现Go 超时控制
Mar 30 Python
Python使用Beautiful Soup(BS4)库解析HTML和XML
Jun 05 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.ini修改php上传文件大小限制的方法详解
2013/06/17 PHP
10款实用的PHP开源工具
2015/10/23 PHP
深入理解PHP JSON数组与对象
2016/07/19 PHP
简单谈谈PHP面向对象之标识对象
2017/06/27 PHP
php和html的区别点详细总结
2019/09/24 PHP
基于jquery的页面划词搜索JS
2010/09/14 Javascript
JS中如何设置readOnly的值
2013/12/25 Javascript
js验证电话号码与手机支持+86的正则表达式
2014/01/23 Javascript
21个值得收藏的Javascript技巧
2014/02/04 Javascript
验证码在IE中不刷新而谷歌等浏览器正常的解决方案
2014/03/18 Javascript
JavaScript实现弹出子窗口并传值给父窗口
2014/12/18 Javascript
Angularjs单选改为多选的开发过程及问题解析
2017/02/17 Javascript
JS实现unicode和UTF-8之间的互相转换互转
2017/07/05 Javascript
es7学习教程之fetch解决异步嵌套问题的方法示例
2017/07/21 Javascript
vue 实现复制内容到粘贴板clipboard的方法
2018/03/17 Javascript
微信小程序 textarea 层级过高问题简单解决方案
2019/10/14 Javascript
Python中__init__和__new__的区别详解
2014/07/09 Python
Python中pygame的mouse鼠标事件用法实例
2015/11/11 Python
Python 实现网页自动截图的示例讲解
2018/05/17 Python
Python发送邮件功能示例【使用QQ邮箱】
2018/12/04 Python
Python 加密与解密小结
2018/12/06 Python
Python数据可视化教程之Matplotlib实现各种图表实例
2019/01/13 Python
如何利用Anaconda配置简单的Python环境
2019/06/24 Python
Python中xml和dict格式转换的示例代码
2019/11/07 Python
python文件操作seek()偏移量,读取指正到指定位置操作
2020/07/05 Python
英国网上花店:Bunches
2016/11/29 全球购物
德国宠物用品、宠物食品及水族馆网上商店:ZooRoyal
2017/07/09 全球购物
中国制造网:Made-in-China.com
2019/10/25 全球购物
介绍一下代理模式(Proxy)
2014/10/17 面试题
竞争上岗演讲稿
2014/01/05 职场文书
房地产公司见习自我鉴定
2014/04/28 职场文书
孝老爱亲事迹材料
2014/12/24 职场文书
2015中秋祝酒词
2015/08/12 职场文书
七年级思品教学反思
2016/02/20 职场文书
Nginx访问日志及错误日志参数说明
2021/03/31 Servers
python井字棋游戏实现人机对战
2022/04/28 Python