# -*- coding:utf-8 -*- import smtplib import os import smtplib from email.mime.text import MIMEText from email.utils import formataddr #jj_gonghao=['jjtest001','jjtest002','jjtest003'] dont_check = ['jjtest004','jjtest005'] days = '40' def sendmail(gonghao,days): print('days = ' + days +'\n') my_sender='jisc@airmate-jiu.com' my_pass = 'p@ssw0rd' print('get email'+ gonghao) z = os.popen('dsquery user -samid '+ gonghao +' | dsget user -email').read() # print(z) email = z.split('\n') print(email) my_user= email[1].strip() print(my_user) if '@airmate-jiu' not in my_user: print(gonghao + 'dont have email') else: print('gonghao is :'+ my_user) msg=MIMEText('温馨提醒:您的工号'+gonghao+'密码已使用超过'+ days +'天,即将过期,为避免影响您的工作,建议及时修改。修改密码方法:<br/>方法一:按CTRL+ALT+DEL 键后,在弹出界面选择 更改密码 <br/>方法二:通过进入链接 <a href="https://ip_addr/iisadmpwd/">https://ip/iisadmpwd/</a> 进行修改(如提示此网站安全证书有问题,点击继续浏览此网站即可)','html','utf-8') msg['From']=formataddr(["邮件发件名",my_sender]) msg['To']=formataddr([my_user,my_user]) msg['Subject']='温馨提醒:您的工号'+gonghao+'开机密码即将过期,请及时修改' server=smtplib.SMTP("SMTPSERVER_ADDR") server.login(my_sender, my_pass) server.sendmail(my_sender,[my_user,],msg.as_string()) server.quit() print('send mail success...\n') #print('===============================now is check password not change =========') info=os.popen("dsquery user ou=XXX,dc=XXX,dc=XXX -stalepwd " + str(days) + " -limit 0 -o upn").read() info=info.split('\n') while '' in info:info.remove('') print(info) for i in info: #print(' i is =================================' + i + '\n') tmp = eval(i) # print(' eval is ================================' + tmp + '\n') CN_NAME = tmp.split('@') gonghao = CN_NAME[0] try: # if gonghao in jj_gonghao: if gonghao not in dont_check: print('=========================\n' + 'now sendmail funcntion ...') sendmail(gonghao,days) except: print('error')
原文地址:http://blog.51cto.com/374721/2128053