zoukankan      html  css  js  c++  java
  • 邮件推广工具

    此工具用python2.7编写,用于发送QQ邮件,可以多个邮件一起发送 例如:邮件1;邮件2;

    也可以随机发送,目标是9位数的QQ邮箱

    缺点:

    发送到163邮箱好像会有乱码,不过发送的QQ邮箱可发附件,可发图片,内容需要用html编辑,可参考程序默认样本

    #-*- coding:utf-8 -*-

    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')
    import wx
    import time
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    from email.mime.image import MIMEImage
    from email.header import Header
    import email.MIMEBase
    import re
    import random
    import threading

    import smtplib,mimetypes
    import threading
    import os


    class Panel(wx.Panel):
        def __init__(self,parent):
            wx.Panel.__init__(self,parent, -1)
            self.panel=wx.Panel(self,-1)
            bj=wx.BoxSizer(wx.VERTICAL)
            self.dai_info=wx.StaticBox(self,-1,"代发送方信息")
            boxsizer=wx.StaticBoxSizer(self.dai_info)
            grid = wx.FlexGridSizer( cols=4 )
            self.group1=[]


            dai_user=wx.StaticText(self,-1,"邮箱用户名:")
            self.user_info=wx.TextCtrl(self,-1,size=(170,25),value='962584902')

            dai_passwd=wx.StaticText(self,-1,"邮箱密码:")
            self.passwd_info=wx.TextCtrl(self,-1,size=(170,25),style=wx.TE_PASSWORD,value='这里填QQ密码,不填也一样,只是设个默认值')

            dai_postfix=wx.StaticText(self,-1,"邮箱后缀:")
            self.postfix_info=wx.TextCtrl(self,-1,size=(170,25),value='qq.com')

            dai_host=wx.StaticText(self,-1,"邮箱主机:")
            self.host_info=wx.TextCtrl(self,-1,size=(170,25),value='smtp.qq.com')

            name=wx.StaticText(self,-1,"发件人:")
            self.name=wx.TextCtrl(self,-1,size=(170,25),value='道月芬')

            self.login=wx.Button(self,-1,"登录")
            self.login.Bind(wx.EVT_BUTTON,self.login_evt)
            self.status=wx.StaticText(self,-1,"")

            self.group1.append((dai_user,self.user_info))
            self.group1.append((dai_passwd,self.passwd_info))
            self.group1.append((dai_host,self.host_info))
            self.group1.append((dai_postfix,self.postfix_info))
            self.group1.append((name,self.name))
            self.group1.append((self.status,self.login))

            for radio1, text1 in self.group1:
                grid.Add( radio1, 0, wx.LEFT|wx.RIGHT|wx.TOP, 5 )
                grid.Add( text1, 0, wx.LEFT|wx.RIGHT|wx.TOP, 5 )

            boxsizer.Add(grid,0,wx.LEFT,10)

            #=============================================
            #收件方信息
            self.rec_info=wx.StaticBox(self,-1,"接收方信息")
            bk=wx.StaticBoxSizer(self.rec_info,wx.VERTICAL)
            grid1 = wx.FlexGridSizer( cols=2 )
            self.group=[]
            rec_people=wx.StaticText(self,-1,"收件人邮箱:")
            self.rec_email=wx.TextCtrl(self,-1,size=(780,25),value='2844179840@qq.com')
            rec_title=wx.StaticText(self,-1,"主题:")
            self.rec_title=wx.TextCtrl(self,-1,size=(780,25),value='亲亲我')
            fj=wx.StaticText(self,-1,"附件:")
            self.fj=wx.TextCtrl(self,-1,size=(280,25))
            jpej=wx.StaticText(self,-1,"图片:",pos=(420,250))
            self.jpej=wx.TextCtrl(self,-1,pos=(450,250),size=(300,25))
            self.jpej.Bind(wx.EVT_MOUSE_CAPTURE_CHANGED,self.jpej_get)
            self.fj.Bind(wx.EVT_MOUSE_CAPTURE_CHANGED,self.fj_get)
            rec_context=wx.StaticText(self,-1,"内容:")
            self.rec_context=wx.TextCtrl(self,-1,size=(780,100),value='<hr><h1>点击进入</h1><a href="http://daoyf.taobao.com"><h2>成人性用品</h2></a><h2>赶紧去吧!精彩不停</h2><br><img src="cid:image1"><hr><br><h3><font color="red">邮件由系统自动发出! 请勿回复,谢谢!</font></h3>',style=wx.TE_MULTILINE)
            self.group.append((rec_people,self.rec_email))
            self.group.append((rec_title,self.rec_title))
            self.group.append((fj,self.fj))
            self.group.append((rec_context,self.rec_context))


            for radio, text in self.group:
                grid1.Add( radio, 0, wx.LEFT|wx.RIGHT|wx.TOP, 5 )
                grid1.Add( text, 0, wx.LEFT|wx.RIGHT|wx.TOP, 5 )

            bk.Add(grid1,0,wx.LEFT,10)

            self.send_button=wx.Button(self,-1,"发送")
            self.send_button.Bind(wx.EVT_BUTTON,self.send_evt)
            self.rand=wx.Button(self,-1,"随机发送",pos=(680,395))
            self.rand.Bind(wx.EVT_BUTTON,self.rand_evt)
            self.log=wx.StaticBox(self,-1,"发送日志")
            bb=wx.StaticBoxSizer(self.log,wx.VERTICAL)
            self.log_info=wx.TextCtrl(self,-1,style=wx.MULTIPLE)
            bb.Add(self.log_info,1,wx.LEFT|wx.EXPAND,5)

            bj.Add(boxsizer,0,wx.LEFT|wx.TOP,20)
            bj.Add(bk,0,wx.LEFT|wx.TOP,20)
            bj.Add(self.send_button,0,wx.ALIGN_RIGHT|wx.ALL,10)
            bj.Add(bb,1,wx.LEFT|wx.EXPAND,20)

            self.SetSizer(bj)
            self.mail_host=''
            self.mail_user=''
            self.mail_pass=''
            self.mail_postfix=''
            self.s=smtplib.SMTP()
            self.me=""


        def login_evt(self,evt):
            self.mail_host=str(self.host_info.GetValue())
            self.mail_user=str(self.user_info.GetValue())
            self.mail_pass=str(self.passwd_info.GetValue())
            self.mail_postfix=str(self.postfix_info.GetValue())
            self.s.connect(self.mail_host)
            try:
                self.s.login(self.mail_user,self.mail_pass)
                self.log_info.AppendText(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+""+self.mail_user+"@"+self.mail_postfix+"登录成功"+' ')
                return True
            except Exception, e:
                self.log_info.AppendText(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+" "+str(e)+' ')
                return False
        def rand_evt(self,evt):
            tar=threading.Thread(target=self.rand_test)
            tar.start()
        def rand_test(self):
            me=self.mail_user+"<"+self.mail_user+"@"+self.mail_postfix+">"
            fileName=r"%s"%str(self.fj.GetValue())
            ifileName=unicode(fileName,"utf-8")

            msgROOT =MIMEMultipart('related')

            msgROOT['Subject'] = str(self.rec_title.GetValue())

            msgROOT['From'] =str(self.name.GetValue())+"<"+self.mail_user+"@"+self.mail_postfix+">"

            #msgROOT['To'] = str(self.rec_email.GetValue())

            msgROOT['Reply-to'] =str(self.user_info.GetValue())
            msgROOT.preamble = 'This is a multi-part message in MIME format.'

            msgROOT['Date'] = email.Utils.formatdate()


            msgAlternative=MIMEMultipart('alternative')

            msgROOT.attach(msgAlternative)

            txt=MIMEText('%s'%str(self.rec_context.GetValue()), 'html')
            msgAlternative.attach(txt)


            #构造图片
            if self.jpej.GetValue() != "":
                fp=open(r"%s"%unicode(str(self.jpej.GetValue()),'utf-8'),'rb')
                msgImage=MIMEImage(fp.read())
                fp.close()
                msgImage.add_header('Content-ID', '<image1>')
                msgROOT.attach(msgImage)
            else:
                pass

            # 构造MIMEBase对象做为文件附件内容并附加到根容器
            ctype,encoding = mimetypes.guess_type(fileName)
            if ctype is None or encoding is not None:
                if ifileName!="":
                    ctype='application/octet-stream'
                    maintype,subtype = ctype.split('/',1)
                    file_msg=email.MIMEImage.MIMEImage(open(ifileName,'rb').read(),subtype)
                    basename = os.path.basename(ifileName)
                    file_msg.add_header('Content-Disposition','attachment', filename ="%s"%Header(basename))
                    msgROOT.attach(file_msg)
                else:
                    pass

            while True:
                m=random.sample([0,1,2,3,4,5,6,7,8,9],9)
                time.sleep(5)
                if m[0] != 0:
                    ma=str(m[0])+str(m[1])+str(m[2])+str(m[3])+str(m[4])+str(m[5])+str(m[6])+str(m[7])+str(m[8])+"@qq.com"
                    try:
                        self.s.sendmail(me,ma, msgROOT.as_string())
                        self.log_info.AppendText(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+" "+"发送给"+ma+"的邮件成功"+" ")
                    except Exception, e:
                        self.log_info.AppendText(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+" "+str(e)+" ")

        def send_evt(self,evt):
            me=self.mail_user+"<"+self.mail_user+"@"+self.mail_postfix+">"
            fileName=r"%s"%str(self.fj.GetValue())
            ifileName=unicode(fileName,"utf-8")

            msgROOT =MIMEMultipart('related')

            msgROOT['Subject'] = str(self.rec_title.GetValue())

            msgROOT['From'] =str(self.name.GetValue())+"<"+self.mail_user+"@"+self.mail_postfix+">"

            #msgROOT['To'] = str(self.rec_email.GetValue())

            msgROOT['Reply-to'] =str(self.user_info.GetValue())
            msgROOT.preamble = 'This is a multi-part message in MIME format.'

            msgROOT['Date'] = email.Utils.formatdate()


            msgAlternative=MIMEMultipart('alternative')

            msgROOT.attach(msgAlternative)

            txt=MIMEText('%s'%str(self.rec_context.GetValue()), 'html')
            msgAlternative.attach(txt)


            #构造图片
            if self.jpej.GetValue() != "":
                fp=open(r"%s"%unicode(str(self.jpej.GetValue()),'utf-8'),'rb')
                msgImage=MIMEImage(fp.read())
                fp.close()
                msgImage.add_header('Content-ID', '<image1>')
                msgROOT.attach(msgImage)
            else:
                pass

            # 构造MIMEBase对象做为文件附件内容并附加到根容器
            ctype,encoding = mimetypes.guess_type(fileName)
            if ctype is None or encoding is not None:
                if ifileName!="":
                    ctype='application/octet-stream'
                    maintype,subtype = ctype.split('/',1)
                    file_msg=email.MIMEImage.MIMEImage(open(ifileName,'rb').read(),subtype)
                    basename = os.path.basename(ifileName)
                    file_msg.add_header('Content-Disposition','attachment', filename ="%s"%Header(basename))
                    msgROOT.attach(file_msg)
                else:
                    pass

            people=re.split(r';',str(self.rec_email.GetValue()))
            try:
                for i in people:
                    self.s.sendmail(me,i, msgROOT.as_string())
                    self.log_info.AppendText(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+" "+"发送给"+i+"的邮件成功"+" ")
                return True
            except Exception, e:
                if str(e)=="'str' object has no attribute 'sendmail'":
                    self.log_info.AppendText(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+" "+"发送失败,请先登录!"+" ")
                elif str(self.rec_email.GetValue())=="":
                    self.log_info.AppendText(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+" "+"发送失败,收件人邮箱为空!"+" ")
                else:
                    self.log_info.AppendText(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+" "+"发送给"+i+"的邮件失败"+" ")
                return False

        def fj_get(self,event):
            dlg=wx.FileDialog(None,message="请选择附件",style=wx.OPEN)
            if dlg.ShowModal() == wx.ID_OK:
                self.fj.SetValue(dlg.GetPath())
                dlg.DestroyChildren()
                self.panel.Refresh()
            else:
                self.panel.Refresh()
        def jpej_get(self,event):
            dlg=wx.FileDialog(None,message="请选择图片",style=wx.OPEN)
            if dlg.ShowModal() == wx.ID_OK:
                self.jpej.SetValue(dlg.GetPath())
                dlg.DestroyChildren()
                self.panel.Refresh()
            else:
                self.panel.Refresh()


    class frame(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self,None,-1,"邮件推广",size=(920,640),style=wx.CAPTION|wx.MINIMIZE_BOX|wx.CLOSE_BOX)
            Panel(self)


    class app(wx.App):
        def OnInit(self):
            myfrmae=frame()
            myfrmae.Show()
            return True
    if __name__=="__main__":
        myapp=app()
        myapp.MainLoop()

  • 相关阅读:
    转载:山寨币凶猛
    Windows8.1 关机异常的解决
    Windows8、Windows8.1使用便签工具
    下载Sourceforge等国内无法下载站点文件的另一种方法
    专著出版成本计算
    PL2303 Windows8.1驱动
    转载:寒门再难出贵子
    华为荣耀品牌独立,子品牌战略能否实现新突破
    路由大战前夜,盘点智能路由的前世今生
    2020年实用工具推荐
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3262938.html
Copyright © 2011-2022 走看看