苏泽湛 发表于 2018-8-9 10:59:01

python邮件发送开服记录

#-*-coding:utf-8 -*-  #!/usr/bin/python
  import sys

  reload(sys)#>  sys.setdefaultencoding('utf-8')# 设置 'utf-8'
  import MySQLdb
  import houtai_dbname
  from email.mime.text import MIMEText
  from email.header import Header
  import smtplib
  #第三方服务smtp
  mail_host='smtp.yeah.net'
  mail_user='xxxx'
  mail_pass='xxxx'
  mail_postfix= 'yeah.net'
  receivers='xxx@sina.com'
  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,statefrom serverinfo where pf=\'%s\' and zoneid<8000 GROUP BYstate order 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,e.args)
  print e
  print result
  for i in result:
  number=plname+","+number+str(i['num'])+","
  #stri="当前该平台已开区 %s" % (result['num'])
  print number
  return number
  def 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'] = receivers       #接收方
  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 "邮件发送成功"
  except Exception,e:
  print "ERROR,无法发送邮件"
  print e
  if __name__=="__main__":
  id=" "
  dic=houtai_dbname.houtaidic
  for i in dic:
  #print i,dic
  plname= i
  conn=MySQLdb.connect(host="%s" % dic,user='xxxx',passwd='xxx',port=3306,charset='utf8')
  max_server=get_addserver()
  id=id+max_server

  #print>  send_mail('xxx@sina.com','platfrom open servervices',id)
页: [1]
查看完整版本: python邮件发送开服记录