需求:
公司使用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: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 = "from@xxx.cn"
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!"