- # -*- coding: cp936 -*-
- import email
- import mimetypes
- import random
- import time
- import string
- from email.MIMEMultipart import MIMEMultipart
- from email.MIMEText import MIMEText
- from email.MIMEImage import MIMEImage
- import smtplib
- def sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText):
- strFrom = fromAdd
- strTo = toAdd
- server = authInfo.get('server' )
- user = authInfo.get('user' )
- passwd = authInfo.get('password' )
- if not (server and user and passwd) :
- print 'incomplete login info , exit now '
- return
- # 设 定root信息
- msgRoot = MIMEMultipart('related' )
- msgRoot['Subject' ] = subject
- msgRoot['From' ] = 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(plainText, 'plain' , 'gb2312' )
- # msgAlternative.attach(msgText)
- # 设定HTML信息
- msgText = MIMEText(htmlText, 'html' , 'gb2312' )
- msgAlternative.attach(msgText)
- # 设定内置图片信息
- # fp = open('test.jpg' , 'rb' )
- # msgImage = MIMEImage(fp.read())
- # fp.close()
- # msgImage.add_header('Content-ID' , '<image1>' )
- # msgRoot.attach(msgImage)
- # 发送邮件
- smtp = smtplib.SMTP()
- # 设定调试级别,依情况而定
- smtp.set_debuglevel(1 )
- smtp.connect(server)
- smtp.login (user, passwd)
- smtp.sendmail(strFrom, strTo, msgRoot.as_string())
- # smtp.sendmail(strFrom, strTo, msgRoot.as_string())
- smtp.quit()
- #print strFrom+'==' +strTo
- return
- if __name__ == '__main__' :
- authInfo = {}
- authInfo['server' ] = '127.0.0.1'
- authInfo['user' ] = 'admin'
- authInfo['password' ] = 'aaa'
- fromAdd = '淘宝网 <shop001@taobao.com>'
- toAdd = ['xxx@163.com' ]
- subject = '最新:国庆中秋让利大行动,特价疯狂抢购中...最低一折起!'
- plainText = '这里是普通文本'
- 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>'
- 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>'
- 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> '
- 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>'
- 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;'
- htmlText = htmlText+'<B>更多国庆特价活动,请猛击这 里</B> >>> <a href="http: //www.taobaosc.net" target="_blank">淘宝十一国庆特价汇总</a> <<& lt;<br><br>'
- # 收信人邮件地址列表
- email_file='email_list.txt'
- email_all=open(email_file,"r" )
- emails=email_all.readlines()
- # 记录发送进度
- loginfo=open("email_log.txt" , "a" )
- i=0
- for line in emails:
- try :
- if i% 100 == 0 :
- loginfo.close()
- loginfo=open("email_log.txt" , "a" )
- time.sleep(5 )
- i=i+1
- fromAdd = '淘宝网 <shop' +str(random.randint( 0 , 9999 ))+ '@taobao.com>'
- toAdd = str(line[0 :- 1 ])
- #print fromAdd,toAdd,'==' ,i
- sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText)
- loginfo.write(toAdd+'/t' +str(i)+ '/n' )
- except:
- continue
- loginfo.close()
-
python群发广告邮件
最新推荐文章于 2024-06-17 03:06:11 发布