首页 > 编程语言 > 详细

python发邮件附件

时间:2021-06-04 11:58:16      阅读:26      评论:0      收藏:0      [点我收藏+]

 

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)

 

python发邮件附件

原文:https://www.cnblogs.com/hxlasky/p/14848542.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!