• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python群发邮件实例代码

Python群发邮件实例代码

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python 群发邮件,python 短信群发,python 微信群发,python脚本编写实例,python项目实例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

直接上代码了

#构造附件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)
</div>

细心的读者会发现代码中有这样一句: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.

好了今天就到这里。

</div>

您可能想查找下面的文章:

  • 利用Python自动监控网站并发送邮件告警的方法
  • 利用Python自动监控网站并发送邮件告警的方法
  • python发送邮件功能实现代码
  • Python获取邮件地址的方法
  • Python实现给qq邮箱发送邮件的方法
  • Python实现给qq邮箱发送邮件的方法
  • python通过邮件服务器端口发送邮件的方法
  • python通过邮件服务器端口发送邮件的方法
  • python监控网站运行异常并发送邮件的方法
  • python监控网站运行异常并发送邮件的方法

相关文章

  • python用装饰器自动注册Tornado路由详解
  • 跟老齐学Python之大话题小函数(2)
  • python的random模块及加权随机算法的python实现方法
  • python thread 并发且顺序运行示例
  • Python 创建子进程模块subprocess详解
  • python网页请求urllib2模块简单封装代码
  • Python 字符串操作实现代码(截取/替换/查找/分割)
  • 用Python实现服务器中只重载被修改的进程的方法
  • Python中模拟enum枚举类型的5种方法分享
  • Python魔术方法详解

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 简单介绍Python中的JSON模块
    • python字典排序实例详解
    • Python中使用asyncio 封装文件读写
    • 在Python的Flask框架中使用日期和时间的教程
    • 在Windows8上的搭建Python和Django环境
    • python 打印出所有的对象/模块的属性(实例代码)
    • python实现通过代理服务器访问远程url的方法
    • CentOS安装pillow报错的解决方法
    • python创建线程示例
    • Python实现将一个大文件按段落分隔为多个小文件的简单操作方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有