通過python群發郵件,支援模板

來源:互聯網
上載者:User

需求:

公司使用SVN,建立使用者,密碼,分配許可權,為了保證安全性,密碼隨機產生並通過郵件發送給每個人

資源:

1、會一份表格,中有如下欄位:

mail, passwd, name, team, rank

郵箱,密碼,姓名,部門,職位

2、郵件的模板:

%s,您好:
系統為您分配了SVN使用者名稱和密碼
使用者名稱:%s(即您的郵箱地址)
密  碼:%s(系統自動分配,不能修改,系統將定期修改並發郵件給大家)

....

主要要帶進去,姓名,郵箱地址,密碼

上代碼:

#! /usr/bin/env python   # -*- coding: utf-8 -*-   #@author jinqinghua@gmail.com  #@version 2012-02-22 09:20import timeimport csvimport smtplibfrom email.MIMEText import MIMETextfrom email.MIMEMultipart import MIMEMultipartfrom email.Header import Headermail_host = "smtp.xxx.cn"mail_from = "from@xxx.cn"mail_user = mail_frommail_password = "不告訴你"mail_subject = "[SVN密碼通知]重要!此郵件請不要刪除!"mail_template_file = "郵件模板.txt"mail_password_file = "SVN密碼.csv"def send_mail(mail_to, subject, body):    msg = MIMEMultipart()    msg['subject'] = Header(subject,'utf-8')     msg['from'] = mail_from    msg['to'] = mail_to    msg['date'] = time.ctime()    txt = MIMEText(body, 'plain', 'utf-8')    msg.attach(txt)        try:        s = smtplib.SMTP()        s.connect(mail_host)        s.login(mail_user, mail_password)        s.sendmail(mail_from, mail_to, msg.as_string())        s.close()        return True    except Exception, e:        print str(e)        return False    if __name__ == '__main__':    subject = mail_subject    content = open(mail_template_file).read()    reader = csv.reader(open(mail_password_file))    for mail, passwd, name, team, rank in reader:        print mail, passwd, name.decode('utf-8'), team.decode('utf-8')        body = content %(name, mail, passwd)                print "DEBUG:sending mail to %s" %(mail)        if send_mail(mail, subject, body):            print "INFO:success to send mail to %s" %(mail)        else:            print "ERROR:fail to send mail to %s" %(mail)    print "done...python is great!"
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.