mail群发

#coding=utf8
os.chdir(sys.path[0])

#配置信息
authInfo = {}
authInfo['server'] = 'smtp.163.com'
authInfo['user'] = 'Zvht2mD@163.com'
authInfo['password'] = '2JlP'
fromAdd = 'Zvht2mD@163.com'
#toAdd = ['test@163.com','test@qq.com']
subject = '邮件主题'
plainText = '这里是普通文本'
htmlText = '<DIV><B>HTML文本</B></DIV>'
name='张三' #别人看到的你的名字
tno=5 #线程数
q=Queue.Queue()
jpg='test.jpg' #图像附件文件名
doc='doc.docx' #非图像音频文件名
html=''
addlist='lis.txt'  #邮件列表



import email,os,sys,threading,smtplib,base64,Queue,time
from email.MIMEMultipart import MIMEMultipart
from email.header import Header
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from email.mime.application import MIMEApplication


class smail(threading.Thread):
        def __init__(self,authInfo,fromAdd,q,subject,plainText,htmlText,name):
                super(smail,self).__init__()
                self.authInfo=authInfo
                self.fromAdd=fromAdd
                self.q=q
                self.subject=subject
                self.plainText=plainText
                self.htmlText=htmlText
                self.name=name
        
        def run(self):
                while True:
                    try:
                        strFrom = self.fromAdd
                        if self.q.empty():
                                break
                        strTo = self.q.get(timeout=4)
                        server = self.authInfo.get('server')
                        user = self.authInfo.get('user')
                        passwd = self.authInfo.get('password')
        
                        if not (server and user and passwd) :
                                print 'incomplete login info, exit now'
                                return
        
                        # 设定root信息
                        msgRoot = MIMEMultipart('related')
                        msgRoot['Subject'] = Header(self.subject, 'utf8')
                        msgRoot['From'] =  '=?utf8?B?%s?= <%s>' % (base64.b64encode(self.name),strFrom)
                        msgRoot['To'] = strTo
                        msgRoot.preamble = 'This is a multi-part message in MIME format.'
        
                        # Encapsulate the plain and HTML versions of the message body in an
                        # 'alternative' part, so message agents can decide which they want to display.
                        msgAlternative = MIMEMultipart('alternative')
                        msgRoot.attach(msgAlternative)
        
                        #设定纯文本信息
                        msgText = MIMEText(self.plainText, 'plain', 'utf-8')
                        msgAlternative.attach(msgText)
        
                        #设定HTML信息
                        msgText = MIMEText(self.htmlText, 'html', 'utf-8')
                        msgAlternative.attach(msgText)
        
                       #设定内置图片信息,如果不需要可以注解掉
                        fp = open(jpg, 'rb')
                        msgImage = MIMEImage(fp.read())
                        fp.close()
                        msgImage.add_header('Content-ID', '<%s1>' % jpg)
                        msgImage.add_header('Content-Disposition', 'attachment; filename=%s' % jpg)
                        msgImage.set_param('name',jpg)
                        msgRoot.attach(msgImage)
                        
                        #设定其他附件
                        fp=open(doc,'rb')
                        msgf=MIMEApplication(fp.read())
                        fp.close()
                        msgf.add_header('Content-ID', '<%s1>' % doc)
                        msgf.add_header('Content-Disposition', 'attachment; filename="%s"' % doc)
                        msgf.set_param('name',doc)
                        msgRoot.attach(msgf)
                       #发送邮件
                        smtp = smtplib.SMTP()
                        #smtp.set_debuglevel(1)
                        smtp.connect(server)
                        smtp.login(user, passwd)
                        smtp.sendmail(strFrom, strTo, msgRoot.as_string())
                        smtp.quit()
                        print 'sending '+strTo
                    except Exception as ad:
                        print ad
                        pass



for i in open(addlist).readlines():
    q.put(i.strip())
for i in xrange(tno):
    smail(authInfo,fromAdd,q,subject,plainText,htmlText,name).start()
time.sleep(30)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值