Python邮件群发,做广告用的,注意中文编码

昨天用Python写了个邮件群发工具,做广告用的,注意中文编码,代码如下:

# -*- 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>'
htmlText = htmlText+'<B>更多国庆特价活动,请猛击这里</B> >>> <a href="http://www.taobaosc.net" target="_blank">淘宝十一国庆特价汇总</a> <<<<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()






在linux下执行用 nohup python send_mail.py mail_log.txt 2>&1 &
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值