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''<image1>' )     
  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 = '淘宝网 <shop001@taobao.com>'     
  71.         toAdd = ['xxx@163.com' ]     
  72.         subject = '最新:国庆中秋让利大行动,特价疯狂抢购中...最低一折起!'     
  73.         plainText = '这里是普通文本'   
  74.         htmlText = '<a href="http://www.taobaosc.net"><img src="http://pics.taobao.com/bao/album/sys/icon/email_logo.gif" width="568" height="55" border="0"></a><br><br>'   
  75.         htmlText = htmlText+'<B>淘宝国庆疯狂抢购活动之一:</B> <a href="http: //haibao.huoban.taobao.com/tms/topic.php?pid=mm_11964787_0_0& eventid=101075" target="_blank">数码特价专场</a><br><br>'   
  76.         htmlText = htmlText+'<B>淘宝国庆疯狂抢购活动之二:</B> <a href="http: //haibao.huoban.taobao.com/tms/topic.php?pid=mm_11964787_0_0& eventid=101055" target="_blank">MM护肤品专场</a><br><br> '   
  77.         htmlText = htmlText+'<B>淘宝国庆疯狂抢购活动之三:</B> <a href="http: //haibao.huoban.taobao.com/tms/topic.php?pid=mm_11964787_0_0& eventid=101067" target="_blank">09秋冬饰品施华洛世奇专场</a><br>< br>'   
  78.         htmlText = htmlText+'<B>淘宝国庆疯狂抢购活动之四:</B> <a href="http: //haibao.huoban.taobao.com/tms/topic.php?pid=mm_11964787_0_0& eventid=101074" target="_blank">一折起国庆大放价</a><br><br& gt;'   
  79.         htmlText = htmlText+'<B>更多国庆特价活动,请猛击这 里</B> >>> <a href="http: //www.taobaosc.net" target="_blank">淘宝十一国庆特价汇总</a> <<& lt;<br><br>'    
  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 = '淘宝网 <shop' +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.              
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值