python群发广告邮件

  1. # -*- coding: cp936 -*-     
  2.     
  3. import  email     
  4. import  mimetypes  
  5. import  random  
  6. import  time  
  7. import  string  
  8. from email.MIMEMultipart import  MIMEMultipart     
  9. from email.MIMEText import  MIMEText     
  10. from email.MIMEImage import  MIMEImage     
  11. import  smtplib     
  12.     
  13. def sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText):     
  14.     
  15.         strFrom = fromAdd     
  16.         strTo = toAdd     
  17.     
  18.         server = authInfo.get('server' )     
  19.         user = authInfo.get('user' )     
  20.         passwd = authInfo.get('password' )     
  21.     
  22.         if  not (server and user and passwd) :     
  23.                 print 'incomplete  login  infoexit  now '     
  24.                 return     
  25.     
  26.         # 设 定root信息     
  27.         msgRoot = MIMEMultipart('related' )     
  28.         msgRoot['Subject' ] = subject     
  29.         msgRoot['From' ] = strFrom     
  30.         msgRoot['To' ] = strTo     
  31.         msgRoot.preamble = 'This is a multi-part message in MIME format.'     
  32.     
  33.         # Encapsulate the plain and HTML versions of the message body in an     
  34.         # 'alternative'  part, so message agents can decide which they want to display.     
  35.         msgAlternative = MIMEMultipart('alternative' )     
  36.         msgRoot.attach(msgAlternative)     
  37.     
  38.         # 设定纯文本信息     
  39. #        msgText = MIMEText(plainText, 'plain' 'gb2312' )     
  40. #        msgAlternative.attach(msgText)     
  41.     
  42.         # 设定HTML信息     
  43.         msgText = MIMEText(htmlText, 'html' 'gb2312' )     
  44.         msgAlternative.attach(msgText)     
  45.     
  46.        # 设定内置图片信息     
  47. #        fp = open('test.jpg' 'rb' )     
  48. #        msgImage = MIMEImage(fp.read())     
  49. #        fp.close()     
  50. #        msgImage.add_header('Content-ID' '' )     
  51. #        msgRoot.attach(msgImage)     
  52.     
  53.        # 发送邮件     
  54.         smtp = smtplib.SMTP()     
  55.        # 设定调试级别,依情况而定     
  56.         smtp.set_debuglevel(1 )     
  57.         smtp.connect(server)     
  58.         smtp.login (user, passwd)  
  59.         smtp.sendmail(strFrom, strTo, msgRoot.as_string())     
  60. #        smtp.sendmail(strFrom, strTo, msgRoot.as_string())     
  61.         smtp.quit()  
  62.         #print strFrom+'==' +strTo  
  63.         return     
  64.     
  65. if  __name__ ==  '__main__'  :     
  66.         authInfo = {}     
  67.         authInfo['server' ] =  '127.0.0.1'     
  68.         authInfo['user' ] =  'admin'     
  69.         authInfo['password' ] =  'aaa'     
  70.         fromAdd = '淘宝网 '     
  71.         toAdd = ['[email protected]' ]     
  72.         subject = '最新:国庆中秋让利大行动,特价疯狂抢购中...最低一折起!'     
  73.         plainText = '这里是普通文本'   
  74.         htmlText = '

    '
      
  75.         htmlText = htmlText+'淘宝国庆疯狂抢购活动之一: 数码特价专场

    '
      
  76.         htmlText = htmlText+'淘宝国庆疯狂抢购活动之二: MM护肤品专场

    '
      
  77.         htmlText = htmlText+'淘宝国庆疯狂抢购活动之三: 09秋冬饰品施华洛世奇专场
    < br>'
      
  78.         htmlText = htmlText+'淘宝国庆疯狂抢购活动之四: 一折起国庆大放价
      
  79.         htmlText = htmlText+'更多国庆特价活动,请猛击这 里 >>> 淘宝十一国庆特价汇总 <<& lt;

    '
       
  80.   
  81. # 收信人邮件地址列表  
  82.         email_file='email_list.txt'   
  83.         email_all=open(email_file,"r" )  
  84.         emails=email_all.readlines()  
  85.   
  86. # 记录发送进度  
  87.         loginfo=open("email_log.txt" , "a" )  
  88.   
  89.         i=0   
  90.         for  line in emails:  
  91.           try :  
  92.             if  i% 100 == 0 :  
  93.                     loginfo.close()  
  94.                     loginfo=open("email_log.txt" , "a" )  
  95.             time.sleep(5 )  
  96.               
  97.             i=i+1   
  98.             fromAdd = '淘宝网  +str(random.randint( 0 , 9999 ))+ '@taobao.com>'   
  99.             toAdd = str(line[0 :- 1 ])  
  100.             #print fromAdd,toAdd,'==' ,i  
  101.   
  102.               
  103.             sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText)  
  104.             loginfo.write(toAdd+'/t' +str(i)+ '/n' )  
  105.     
  106.           except:  
  107.             continue   
  108.   
  109.         loginfo.close()      
  110.              

你可能感兴趣的:(python群发广告邮件)