需求:
公司使用SVN,建立用戶,密碼,分配權限,為了保證安全性,密碼隨機生成並通過郵件發送給每個人
資源:
1、會一份表格,中有如下字段:
mail, passwd, name, team, rank
郵箱,密碼,姓名,部門,職位
2、郵件的模板:
%s,您好:
系統為您分配了SVN用戶名和密碼
用戶名:%s(即您的郵箱地址)
密 碼:%s(系統自動分配,不能修改,系統將定期修改並發郵件給大家)
....
主要要帶進去,姓名,郵箱地址,密碼
上代碼:
- #! /usr/bin/env python
- # -*- coding: utf-8 -*-
- #@author [email protected]
- #@version 2012-02-22 09:20
-
- import time
- import csv
- import smtplib
- from email.MIMEText import MIMEText
- from email.MIMEMultipart import MIMEMultipart
- from email.Header import Header
-
- mail_host = "smtp.xxx.cn"
- mail_from = "[email protected]"
- mail_user = mail_from
- mail_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!"