更新时间:2022-06-11 15:30:34
mail.py 脚本
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# mail.py
import os.path
import time
import sys
import os
import json
import string
import random
import smtplib
import time
from datetime import date
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.Header import Header
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
mail_user="xxx@xxxx.com";
mail_pass="xxxxx";
mail_smtp_server="smtp.xxxxx.com";
mail_smtp_port= 25
def sendMail(contents,subject,attach,mail_to):
lTime=str(time.strftime('%Y%m%d_%H',time.localtime(time.time())))
msg = MIMEMultipart('related');
if subject=="":
subject='test';
msg['Subject'] = subject+" Time:"+ lTime;
msg['From'] = mail_user;
msg['To'] = ";".join(mail_to) ;
html="";
for cont in contents:
html = html+cont[0];
msgHtml = MIMEMultipart('alternative');
msgHtml.set_charset('UTF-8');
msgAtt = MIMEText(open(attach,'rb').read(),'base','gbk');
msgAtt["Content-Type"] = 'application/octet-stream'
msgAtt["Content-Disposition"] = 'attachment; filename="'+subject+'"'
msg.attach(msgAtt);
try:
smtp = smtplib.SMTP();
smtp.connect(mail_smtp_server,mail_smtp_port)
smtp.login(mail_user,mail_pass);
smtp.sendmail(mail_user,mail_to,msg.as_string());
smtp.close();
except Exception,e:
print str(e)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import mail ##使用了上述脚本
import sys
import time
import os
import datetime
from datetime import date
from os.path import join, getsize
def sendSlowlog(subject_info,attach,mail_to):
size = os.path.getsize(attach)
if(size > 1):
mail.sendMail("",subject_info,attach,mail_to)
if __name__ == '__main__':
mail_to=["qilong.yangql@xxxx.com"]
lDate=str(time.strftime('%Y%m%d',time.localtime(time.time())))
lTime=str(time.strftime('%Y%m%d_%H',time.localtime(time.time())))
subject_info="Slowlog of DBname" + lTime
attach="/u01/my3306/log/slow_"+ lDate +"/slow.log."+lTime
sendSlowlog(subject_info,attach,mail_to)