免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5564 | 回复: 3

python写的多线程邮件群发脚本,贴出来大家一起讨论学习下 [复制链接]

论坛徽章:
2
酉鸡
日期:2014-02-19 09:11:08摩羯座
日期:2014-05-23 10:16:16
发表于 2012-07-10 17:50 |显示全部楼层
本帖最后由 tiankafeiwu 于 2012-07-10 17:51 编辑

我是新手,对于很多地方运用还不是很到位,贴出来欢迎大家一起讨论和改进,还有不少地方需要改进。
  1. #-*- encoding: utf-8 -*-
  2. import threading
  3. import time,datetime
  4. import sys, os
  5. import smtplib
  6. from email.mime.text import MIMEText
  7. import Queue

  8. '''
  9. list.txt文件格式:
  10. aaa@163.com
  11. bbb@yahoo.com.cn
  12. ccc@qq.com
  13. ddd@126.com
  14. '''
  15. sub="这里是邮件的主题subject"
  16. content="这里是邮件的内容content"
  17. #####################
  18. #设置服务器,用户名、口令以及邮箱的后缀
  19. mail_host="smtp.qq.com"
  20. mail_user="your qq number"
  21. mail_pass="your qq password"
  22. mail_postfix="qq.com"


  23. queue=Queue.Queue()
  24. class send_mail(threading.Thread):
  25.     def __init__(self,threadname,queue):
  26.         threading.Thread.__init__(self)
  27.         self.threadname=threadname
  28.         self.queue = queue
  29.         self.start()
  30.     def run(self):
  31.         while True:
  32.                 if self.queue.empty():break
  33.                 to_list=self.queue.get()
  34.                 me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
  35.                 msg = MIMEText(content)
  36.                 msg['Subject'] = sub
  37.                 msg['From'] = me
  38.                 msg['To'] = to_list
  39.                 try:
  40.                     s = smtplib.SMTP()
  41.                     s.connect(mail_host)
  42.                     s.login(mail_user,mail_pass)
  43.                     s.sendmail(me, to_list, msg.as_string())
  44.                     s.close()
  45.                     print '发送成功:'+to_list
  46.                     self.queue.task_done()
  47.                 except Exception, e:
  48.                     print str(e)

  49. if __name__ == '__main__':
  50.     #读取邮件列表
  51.     file_object = open('list.txt')
  52.     try:
  53.         all_the_text = file_object.readlines( )
  54.     finally:
  55.         file_object.close( )
  56.     mailto_list = all_the_text
  57.     # 产生线程序列
  58.     for i in mailto_list:
  59.         print i
  60.         queue.put(i)
  61.     for i in range(10):
  62.         threadname='Thread'+str(i)   
  63.         send_mail(threadname,queue)
  64.     queue.join()
复制代码

论坛徽章:
0
发表于 2012-07-13 09:27 |显示全部楼层
#读取邮件列表
    file_object = open('list.txt')
    try:
        all_the_text = file_object.readlines( )
    finally:
        file_object.close( )

================
上面这里可以用:
with open('list.txt') as f:
    all_the_text=f.readlines()

论坛徽章:
2
酉鸡
日期:2014-02-19 09:11:08摩羯座
日期:2014-05-23 10:16:16
发表于 2012-07-13 10:16 |显示全部楼层
还没用过这种写法,学习了,谢谢回复 2# anonymous0502


   

论坛徽章:
0
发表于 2012-07-13 10:19 |显示全部楼层
这里有个简单的介绍:
http://woodpecker.org.cn/diveintopython3/files.html
稍微详细一点的可以看自带的手册manual文档
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP