43213 发表于 2015-11-12 08:34:28

用Python发送html类型的广告邮件

#!/usr/bin/python
# ——*—— coding=utf-8 __*__
#author: zhangdonghong
#email: zhangdonghongemail@163.com
#date: 2014-11-17

import smtplib
from email.Header import Header
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart

def sent_mail(rcpt):
    sender = "service@only.cn"
    msg = MIMEMultipart('alternative')
    msg['Subject'] = Header("(AD)定情之吻,让TA为你倾情","utf-8")
    msg['From'] = "%s <service@only.cn>" % Header("Only","utf-8")
    msg['To'] = rcpt

    #only.html is the AD page
    html = open('only.html').read()
    html_part = MIMEText(html,'html')
    html_part.set_charset('utf-8')
    msg.attach(html_part)

    try:
      s = smtplib.SMTP()
      s.connect('127.0.0.1:25')
      s.login('zhangdonghong','zdh@#123:)')
      s.sendmail(sender,rcpt,msg.as_string())
      s.quit()
      print '%s 邮件发送成功' % rcpt
    except Exception,e:
      print e
      print '%s 邮件发送失败' % rcpt

if __name__ == '__main__':
    #save all email address with list.txt
    for line in open("list.txt"):
      #print line,
      sent_mail(line)

页: [1]
查看完整版本: 用Python发送html类型的广告邮件