Python实现向QQ群成员自动发邮件的方法

WBOY
Release: 2016-06-06 11:20:15
Original
1854 people have browsed it

本文实例讲述了Python实现向QQ群成员自动发邮件的方法。分享给大家供大家参考。具体实现方法如下:

原理:

我们需要先获取QQ群中的所有成员并保存到一个txt文本中去,然后再由python读取文件然后进行批量邮件发送了,具体解决方案如下:

1. 获取QQ群成员QQ号码,QQ群成员信息可以在如下网页中获取,选择全部成员保存在txt中即可
http://qun.qzone.qq.com/group#!/123456/member
(这里的123456为QQ群号码)

2. 解析获取的QQ群成员列表list.txt,给每个成员单独自动发送邮件,这里使用的发送方是163邮箱

完整脚本如下:

代码如下:

#coding:utf-8
import random
import smtplib
from email.mime.text import MIMEText
import time

def send_mail(mailto):
 print 'Setting MIMEText'
 CT=open('content.txt','r') #读取发送邮件内容
 content=CT.read().decode('utf-8')
 msg=MIMEText(content.encode('utf8'),_subtype='html')
 CT.close()#关闭文件
 msg['From']=mail_user
 msg['SUbject']=u'Python邮件发送测试'
 msg['To']=mailto
       
 try:
  print 'Connectting',mail_host
  s=smtplib.SMTP_SSL(mail_host,465)
  print 'Login to mail_host'
  s.login(mail_user,mail_pwd)
  print 'Send mail'
  s.sendmail(mail_user,mailto,msg.as_string())
  print 'Close the connection between the mail server'
  s.close()
 except Exception as e:
  print "Exceptioin ",e
def sendqunmail():
    try:
 f=open(filelist,'r')
 lines=f.readlines()
 for i in range(len(lines)-1):
  if lines[i].find('(')-1 and lines[i].find(')')-1:
   qqnum=lines[i].split('(')[1].split(')')[0]
   if qqnum.isdigit():
    mailto=qqnum+'@qq.com'
    print 'Sendmail to:'+mailto
    send_mail(mailto)
    time.sleep(10)
    except Exception,ex:
 print filelist,ex
if __name__=="__main__":
 mail_host='smtp.163.com'
 mail_user='test@163.com'
 mail_pwd='test123'
 filelist='list.txt'
 sendqunmail()

 希望本文所述对大家的Python程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template