需求:
公司使用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!"