Python群发邮件实例代码

 更新时间:2014年01月03日 14:46:16   作者:  
今天试了试Python发邮件,突然想到能不能群发邮件呢?群发邮件是smtplib的一个bug,不过最终还是解决了

直接上代码了

复制代码 代码如下:

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.

好了今天就到这里。

相关文章

  • TensorFlow实现卷积神经网络

    TensorFlow实现卷积神经网络

    这篇文章主要为大家详细介绍了TensorFlow实现卷积神经网络,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Python获取服务器信息的最简单实现方法

    Python获取服务器信息的最简单实现方法

    这篇文章主要介绍了Python获取服务器信息的最简单实现方法,涉及Python中urllib2库的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • 基于Google的Python编码规范标准

    基于Google的Python编码规范标准

    这篇文章主要介绍了基于Google的Python编码规范标准,其中包含了分号,行长度,括号,缩进,空行,空格等基本符号的使用规则,有需要的朋友可以参考下
    2021-08-08
  • Python 循环函数详细介绍

    Python 循环函数详细介绍

    循环用于重复执行一些程序块。从上一讲的选择结构,我们已经看到了如何用缩进来表示程序块的隶属关系。循环也会用到类似的写法。感兴趣得小伙伴请参考下面文字得具体内容
    2021-09-09
  • python:列表详解

    python:列表详解

    这篇文章主要介绍了Python中列表(List)的详解操作方法,包含创建、访问、更新、删除、其它操作等,需要的朋友可以参考下
    2021-10-10
  • 分析python垃圾回收机制原理

    分析python垃圾回收机制原理

    这篇文章主要介绍了python垃圾回收机制原理,python采用的是引用计数机制为主,标记-清除和分代收集两种机制为辅的策略,有需要的的朋友可以借鉴参考想
    2021-09-09
  • Python的Django框架中使用SQLAlchemy操作数据库的教程

    Python的Django框架中使用SQLAlchemy操作数据库的教程

    SQLAlchemy是Python一个专门的数据库管理工具,如果对Django ORM觉得有些生疏的话完全可以结合SQLAlchemy,这里我们就来总结一下Python的Django框架中使用SQLAlchemy操作数据库的教程
    2016-06-06
  • Python中常用的四种取整方式分享

    Python中常用的四种取整方式分享

    在数据处理和数值计算中,取整操作是非常常见的需求,Python 提供了多种取整方式,本文为大家整理了四种常用的方法,希望对大家有所帮助
    2025-02-02
  • Python数据库反向生成Model最优方案示例

    Python数据库反向生成Model最优方案示例

    这篇文章主要介绍了Python数据库反向生成Model最优方案的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • Python实现Restful API的例子

    Python实现Restful API的例子

    今天小编就为大家分享一篇Python实现Restful API的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08

最新评论