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实现的阳历转阴历(农历)算法
Apr 25 Python
python中MySQLdb模块用法实例
Nov 10 Python
Python 列表排序方法reverse、sort、sorted详解
Jan 22 Python
Linux 下 Python 实现按任意键退出的实现方法
Sep 25 Python
Python利用operator模块实现对象的多级排序详解
May 09 Python
python将回车作为输入内容的实例
Jun 23 Python
Python使用sort和class实现的多级排序功能示例
Aug 15 Python
django在保存图像的同时压缩图像示例代码详解
Feb 11 Python
python pyecharts 实现一个文件绘制多张图
May 13 Python
python3.7调试的实例方法
Jul 21 Python
用60行代码实现Python自动抢微信红包
Feb 04 Python
端午节将至,用Python爬取粽子数据并可视化,看看网友喜欢哪种粽子吧!
Jun 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字符串处理的10个简单方法
2010/06/30 PHP
解析php获取字符串的编码格式的方法(函数)
2013/06/21 PHP
详解PHP的抽象类和抽象方法以及接口总结
2019/03/15 PHP
PHP高并发和大流量解决方案整理
2019/12/24 PHP
jQuery EasyUI API 中文文档 - Draggable 可拖拽
2011/09/29 Javascript
使用PHP+JQuery+Ajax分页的实现
2013/04/23 Javascript
纯文字版返回顶端的js代码
2013/08/01 Javascript
jQuery中常用的遍历函数用法实例总结
2015/09/01 Javascript
jQuery过滤特殊字符及JS字符串转为数字
2016/05/26 Javascript
AngularJS控制器之间的通信方式详解
2016/11/03 Javascript
vue.js入门(3)——详解组件通信
2016/12/02 Javascript
Bootstrap 填充Json数据的实例代码
2017/01/11 Javascript
Android中Okhttp3实现上传多张图片同时传递参数
2017/02/18 Javascript
JS中mouseup事件丢失的原因与解决办法
2017/06/14 Javascript
老生常谈Bootstrap媒体对象
2017/07/06 Javascript
backbone简介_动力节点Java学院整理
2017/07/14 Javascript
Vue批量图片显示时遇到的路径被解析问题
2019/03/28 Javascript
详解vue-cli+element-ui树形表格(多级表格折腾小计)
2019/04/17 Javascript
JsonProperty 的使用方法详解
2019/10/11 Javascript
详解微信小程序动画Animation执行过程
2020/09/23 Javascript
django-filter和普通查询的例子
2019/08/12 Python
python 读取更新中的log 或其它文本方式
2019/12/24 Python
Django 设置多环境配置文件载入问题
2020/02/25 Python
Selenium向iframe富文本框输入内容过程图解
2020/04/10 Python
PyCharm 2020.2 安装详细教程
2020/09/25 Python
LODI女鞋在线商店:阿利坎特的鞋类品牌
2019/02/15 全球购物
Exception类的常用方法
2012/06/16 面试题
2014年五四青年节演讲比赛方案
2014/04/22 职场文书
2014大学生党员评议个人总结
2014/09/22 职场文书
工厂见习报告范文
2014/10/31 职场文书
喜迎建国70周年:有关爱国的名言名句
2019/09/24 职场文书
JS数组的常用方法整理
2021/03/31 Javascript
ajax请求前端跨域问题原因及解决方案
2021/10/16 Javascript
MySQL中varchar和char类型的区别
2021/11/17 MySQL
口袋妖怪冰系十大最强精灵,几何雪花排第七,第六类似北极熊
2022/03/18 日漫
Win11黑色桌面背景怎么办?Win11黑色壁纸解决方法汇总
2022/04/05 数码科技