#-*-coding:utf-8-*-#!/usr/bin/pythonimport sysreload (SYS) # Reload to invoke Setdefaultencoding method Sys.setdefaultencoding ( ' Utf-8 ') # set ' utf-8 ' Import mysqldbimport houtai_dbnamefrom email.mime.text import mimetextfrom email.header import Heade Rimport smtplib# Third party services smtpmail_host= ' smtp.yeah.net ' mail_user= ' xxxx ' mail_pass= ' xxxx ' mail_postfix= ' yeah.net ' Receivers= ' [email protected] ' Def get_addserver (): number= "" Global conn,plname Defdb=houtai_dbname.check_ Plname (plname) try:cur = Conn.cursor (cursorclass=mysqldb.cursors.dictcursor) conn.select_db (defdb) #选择数据库 sql= " Select COUNT (*) as num,state from ServerInfo where pf=\ '%s\ ' and zoneid<8000 GROUP by state '% (plname) Cur.execute (SQL) #执行sql Result=cur.fetchall () #获取的数据集结 cur.close () #关闭游标 conn.close () #关闭数据库 except mysqldb.error,e: # Print "MySQLdb error%d:%s"% (E.args[0],e.args[1]) Print e print result for I in result:number=plname+ "," +number+str (i[ ' num ']) + "," #stri = "Current platform is open%s"% (result[' num ']) print number return Numberdef Send_mail (mail_to,subject,content): me = mail_user+ "<" +mail_user+ "@" +mail_postfix+ ">" #邮件发送方 message = Mimetext (content, ' plain ', ' utf-8 ') #邮件内容 message[' subject '] = subject #邮件标题 message[' from '] = Me #发送方 message["to"] = R Eceivers #接收方 try:smtpobj = smtplib. SMTP () smtpobj.connect (mail_host) #连接smtp smtpobj.login (mail_user,mail_pass) #登录 smtpobj.sendmail (Me,receivers, Message.as_string ()) #获取参数发送邮件 smtpobj.close () #关闭 print "Mail sent successfully" except Exception,e:print "ERROR, unable to send mail" print EIF __nam e__== "__main__": id= "" Dic=houtai_dbname.houtaidic for I in DIC: #print i,dic[i] plname= i conn=mysqldb.connect (host= "%s" % dic[i],user= ' xxxx ', passwd= ' xxx ', port=3306,charset= ' UTF8 ') max_server=get_addserver () Id=id+max_server #print ID Send_mail (' [email protected] ', ' platfrom open servervices ', id)
Python mail to send a record of open service