Python群发邮件实例代码
发布时间:2021-05-05 18:42:35
直接上代码了
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.
好了今天就到这里。
猜你喜欢
- 树型结构在我们应用程序中还是很常见的,比如文件目录,BBS,权限设置,部门设置等。这些数据信息都采用层次型结构,而在我们现在的关系型数据库中
- 问题现在有多个字典或者映射,你想将它们从逻辑上合并为一个单一的映射后执行某些操作,比如查找值或者检查某些键是否存在。解决方案加入你有如下两个
- 在Python中可以存储很大的值,如下面的Python示例程序:x = 1000000000000000000000000000000000
- 无论是 DAO 还是 ADO 都有两种从 Recordset 对象中查询记录的方法: Find 方法和 Seek 方法。在这两种方法中可以让
- 我们以MNIST手写数字识别为例import numpy as npfrom keras.datasets import mnistfrom
- 事实上,当我们向文件导入某个模块时,导入的是
- 有一个优秀的库可以使用————demjson示范链接http
- <script type="text/javascript"> var params&n
- iframe标签在网页中可以创建一个内嵌框架,通过指定src属性来调用另一个网页文档的内容。和frameset一样,用它来对网页结构进行拆分
- 场景:查找who删了我,直接copy代码保存到一个python文件who.py,在python环境下运行此文件代码如下,copy保存到who
- 如何去读取一个没有表头的二维csv文件(如下图所示)?并以元组的形式表现数据:((1.0, 0.0, 3.0, 180.0), (2.0,
- 先让我们看一个例子,了解什么是模式化窗口。以下是QQ秀商城在非登录时提示登录的一种状态。当我在非登录状态,通过保存形象的方式买一件衣服时,弹
- 使用一个遵循buffer protocol的对象就可以和numpy交互了.这个buffer_protocol要有哪些东西呢? 要有如下接口:
- 当我发现要写python的面向对象的时候,我是踌躇满面,坐立不安呀。我一直在想:这个坑应该怎么爬?因为python中关于面向对象的内容很多,
- 内容摘要:asp使用最多的就是ACCESS数据库和ms sql server数据库,本文列出了asp连接这两个数据库的方
- 先看看CSS框架的利与弊前段时间一直在讨论CSS框架。很多朋友看了那三篇文章后提了不少自己意见。特别是一位北京的朋友A君,他有一个小的团体,
- 词频统计是指在文本中计算每个词出现的次数。在 Python 中,可以使用一些第三方库(如 jieba)来分词,然后使用字典等数据结构记录每个
- Web,全称为 World Wide Web,是 Internet 上最重要和最为人们所熟知的应用之一。Web 是指 Internet 上所
- LyScript 插件默认提供了一个get_disasm_code()方法可以直接获取到指定行数的反汇编代码,但如果需要自定义获取或者是需要
- 解决步骤:1、先打开一个cmd2、cd到你的exe文件目录3、输入 .\***.exe来源:https: