用Python写一段发邮件的小程序

#!/usr/bin/env python

import random
import os
import string

allchoice = string.letters + string.digits
#print allchoice

"""
邮件正文,后面展现string.Template()的用法
"""
content = """your acccount is created
username is $user
password is $pwd"""
""" 
8位随机密码生成
"""
def pwdgen(num = 8):
    pwd = ''
    for i in range(num):
        pwd += random.choice(allchoice)
    #print pwd
    return pwd

if __name__=='__main__':
    password = pwdgen()
    username = raw_input('input your name:')
    os.system('useradd %s' % username) #os.system('')可以调用系统命令
    os.system('echo %s | passwd --stdin %s' % (password,username))
    t = string.Template(content)#创建模板
    os.system("echo '%s' | mail -s 'create user' root" % t.substitute(user=username,pwd=password))#模板内容替换

你可能感兴趣的:(Python)