Python Mail send open service record

Source: Internet
Author: User

#-*-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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.