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基于twisted实现简单的web服务器
Sep 29 Python
Python的Django框架中的URL配置与松耦合
Jul 15 Python
python下读取公私钥做加解密实例详解
Mar 29 Python
Python3的高阶函数map,reduce,filter的示例详解
Jul 23 Python
python实现倒计时小工具
Jul 29 Python
linux环境下安装python虚拟环境及注意事项
Jan 07 Python
Python Selenium参数配置方法解析
Jan 19 Python
Python Numpy 控制台完全输出ndarray的实现
Feb 19 Python
在python里创建一个任务(Task)实例
Apr 25 Python
python 如何调用远程接口
Sep 11 Python
python实现图像高斯金字塔的示例代码
Dec 11 Python
python中random模块详解
Mar 01 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
Apache, PHP在Windows 9x/NT下的安装与配置 (一)
2006/10/09 PHP
PHP 上传文件大小限制
2009/07/05 PHP
redis 队列操作的例子(php)
2012/04/12 PHP
yii中widget的用法
2014/12/03 PHP
PHP实现获取FLV文件的时间
2015/02/10 PHP
php生成过去100年下拉列表的方法
2015/07/20 PHP
用Javscript实现表单复选框的全选功能
2007/05/25 Javascript
Javascript 去除数组的重复元素
2010/05/04 Javascript
S2SH整合JQuery+Ajax实现登录验证功能实现代码
2013/01/30 Javascript
两个数组去重的JS代码
2013/12/04 Javascript
JS 正则表达式验证密码、邮箱格式的实例代码
2018/10/28 Javascript
vue基础之使用get、post、jsonp实现交互功能示例
2019/03/12 Javascript
JavaScript实现汉字转换为拼音及缩写的方法示例
2019/03/28 Javascript
javascript中的相等操作符(==与===区别)
2019/12/21 Javascript
完美解决通过IP地址访问VUE项目的问题
2020/07/18 Javascript
Handtrack.js库实现实时监测手部运动(推荐)
2021/02/08 Javascript
Python处理json字符串转化为字典的简单实现
2016/07/07 Python
浅析Python中元祖、列表和字典的区别
2016/08/17 Python
python的mysqldb安装步骤详解
2017/08/14 Python
浅谈python下tiff图像的读取和保存方法
2018/12/04 Python
使用python实现ftp的文件读写方法
2019/07/02 Python
python web框架中实现原生分页
2019/09/08 Python
Jupyter Notebook 文件默认目录的查看以及更改步骤
2020/04/14 Python
Python高并发解决方案实现过程详解
2020/07/31 Python
TripAdvisor日本:全球领先的旅游网站
2019/02/14 全球购物
美国知名眼镜网站:Target Optical
2020/04/04 全球购物
PHP开发工程师面试问题集锦
2012/11/01 面试题
应届大学生自荐信格式
2013/09/21 职场文书
运动会开幕式主持词
2014/03/28 职场文书
企业宣传口号
2014/06/12 职场文书
党员自评材料范文
2014/12/17 职场文书
研讨会通知
2015/04/27 职场文书
《狮子和鹿》教学反思
2016/02/16 职场文书
详解MySQL的Seconds_Behind_Master
2021/05/18 MySQL
SpringBoot中获取profile的方法详解
2022/04/08 Java/Android
ORACLE中dbms_output.put_line输出问题的解决过程
2022/06/28 Oracle