zoukankan      html  css  js  c++  java
  • python发邮件附件

    python 2.7环境

    [root@localhost send_wrong_sql]# more send_fujin01.py 
    #!/usr/bin/env python 
    # -*- coding: UTF-8 -*- 
    
    import smtplib
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from email.mime.application import MIMEApplication
    import datetime
    import time
    
    mail_to = "aa@threegene.com"
    mail_host = "mail.threegene.com:465"
    mail_user = "yw@aa.com"
    mail_pass = "123456"
    mail_postfix = "aa.com"
    receivers = ['aa@threegene.com', 'bb@threegene.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
    
    
    now_time = datetime.datetime.now()
    yes_time = now_time + datetime.timedelta(days=-1)
    
    def send_attach(file_name) :
            msg = MIMEMultipart()
            msg["Subject"] = "从库慢查询日志-"+str(now_time.strftime('%Y%m%d'))
            msg["From"] = mail_user  #邮件发送人
            msg["To"] = ','.join(receivers)
            part = MIMEText("123从库慢查询日志,请查看附件!",_charset="utf-8")
            msg.attach(part)
            part = MIMEApplication(open(file_name, 'rb').read())
            part.add_header('Content-Disposition', 'attachment', filename="slowquery_123_"+str(now_time.strftime('%Y%m%d'))+".log")
            msg.attach(part)
    
            try:
                s = smtplib.SMTP_SSL(mail_host)
                s.login(mail_user, mail_pass)
                s.sendmail(mail_user, receivers, msg.as_string())
                s.close()
            except Exception as e:
                print("error:", e)
    
    
    
    def get_filecount(file_name) :
            count=-1
            for count, line in enumerate(open(file_name,'rU')):
                    pass
            count+=1
            return count
    
    if __name__ == '__main__' :
            file_name="/tmp/slowquery/daily_slow_query_"+str(yes_time.strftime('%Y_%m_%d'))+".log"
            file_row=get_filecount(file_name)
            print(file_row)
            if file_row > 0:
                    send_attach(file_name)
  • 相关阅读:
    面试(转)
    Expression Blend实战开发技巧
    Twelve Principles of Agile Software
    Test Software Engineer
    Web开发工程师必读的15个设计博客
    麻省理工的C/C++的课程
    Orchard:处理1对多的关系
    DotNetNuke Switches to C# !!
    我的那个他
    2011 微软MVP全球大会
  • 原文地址:https://www.cnblogs.com/hxlasky/p/14848542.html
Copyright © 2011-2022 走看看