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 相关文章推荐
Django框架中的对象列表视图使用示例
Jul 21 Python
谈谈Python进行验证码识别的一些想法
Jan 25 Python
python利用matplotlib库绘制饼图的方法示例
Dec 18 Python
Golang与python线程详解及简单实例
Apr 27 Python
git使用.gitignore设置不生效或不起作用问题的解决方法
Jun 01 Python
python创建列表和向列表添加元素的实现方法
Dec 25 Python
Python批量生成幻影坦克图片实例代码
Jun 04 Python
解决python文件双击运行秒退的问题
Jun 24 Python
使用 Python 在京东上抢口罩的思路详解
Feb 27 Python
如何使用PyCharm将代码上传到GitHub上(图文详解)
Apr 27 Python
python palywright库基本使用
Jan 21 Python
Python OpenCV快速入门教程
Apr 17 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
使用网络地址转换实现多服务器负载均衡
2006/10/09 PHP
php FPDF类库应用实现代码
2009/03/20 PHP
PHP IN_ARRAY 函数使用注意事项
2010/07/24 PHP
PHP解压ZIP文件到指定文件夹的方法
2016/11/17 PHP
js随机颜色代码的多种实现方式
2013/04/23 Javascript
zepto与jquery的区别及zepto的不同使用8条小结
2016/07/28 Javascript
图解prototype、proto和constructor的三角关系
2016/07/31 Javascript
JS点击某个图标或按钮弹出文件选择框的实现代码
2016/09/27 Javascript
常用jQuery选择器汇总
2017/02/02 Javascript
微信小程序 动态绑定事件并实现事件修改样式
2017/04/13 Javascript
Vue.js项目部署到服务器的详细步骤
2017/07/17 Javascript
jQuery实现腾讯信用界面(自制刻度尺)样式
2017/08/15 jQuery
关于react中组件通信的几种方式详解
2017/12/10 Javascript
Express进阶之log4js实用入门指南
2018/02/10 Javascript
JS实现把一个页面层数据传递到另一个页面的两种方式
2018/08/13 Javascript
vue transition 在子组件中失效的解决
2019/11/12 Javascript
环形加载进度条封装(Vue插件版和原生js版)
2019/12/04 Javascript
编写自定义的Django模板加载器的简单示例
2015/07/21 Python
Python中基础的socket编程实战攻略
2016/06/01 Python
利用Python实现Windows定时关机功能
2017/03/21 Python
Python多线程经典问题之乘客做公交车算法实例
2017/03/22 Python
python PyTorch参数初始化和Finetune
2018/02/11 Python
Python装饰器语法糖
2019/01/02 Python
Python利用sqlacodegen自动生成ORM实体类示例
2019/06/04 Python
详解Django 时间与时区设置问题
2019/07/23 Python
Python Django 命名空间模式的实现
2019/08/09 Python
Django如何实现上传图片功能
2019/08/16 Python
Python3.7将普通图片(png)转换为SVG图片格式(网站logo图标)动起来
2020/04/21 Python
Python模块常用四种安装方式
2020/10/20 Python
浅析border-radius如何兼容IE
2016/04/19 HTML / CSS
.NET里面什么时候需要调用垃圾回收
2015/06/01 面试题
采购部部门职责
2013/12/15 职场文书
项目投资建议书
2014/05/16 职场文书
2016年社会主义核心价值观心得体会
2016/01/21 职场文书
小学生节约用水倡议书
2019/08/12 职场文书
MyBatis配置文件解析与MyBatis实例演示
2022/04/07 Java/Android