一、基础环境 1、版本 cat /etc/debian_version 7.8 2、内核 uname -r 3.2.0-4-amd64 3、ip(eth0) 10.1.10.185 4、具体日志 cat 3.log aaa 10-29_10:30 29629395 test bbb 10-29_10:36 180035 test ccc 10-29_10:37 180035 test ddd 10-29_10:37 580495 test eee 10-29_10:37 580495 test fff 10-29_10:46 180035 test ggg 10-29_10:46 180035 test 5、mutt使用是发到163的 python的是在公司使用的 6、使用的python版本是2.7 二、具体代码 cat mailsend.sh #!/bin/bash #-------------------------------------------------- #Author:jimmygong #Email:[email protected] #FileName:mailsend.sh #Function: #Version:1.0 #Created:2015-10-30 #-------------------------------------------------- sendip="10.1.10.185" maillog="/root/3.log" mailbox="[email protected]" function handlemail () { sed -i -e ‘1i\title1\ttitle2\ttitle3\tdtitle4‘ $maillog sed -i -e ‘s/^/<tr><td>/‘ -e ‘s/\s\+/<\/td><td>/‘ -e ‘s/\s\+/<\/td><td>/‘ -e ‘s/\s\+/<\/td><td>/‘ -e ‘s/$/<\/td><\/tr>/‘ $maillog sed -i -e ‘1i\<table>‘ -e ‘$a\</table>‘ $maillog sed -i -e ‘1i\<style type="text\/css">table,td,th{border:1px solid black;}<\/style>‘ $maillog /usr/local/mutt/bin/mutt -s "aaa $sendip" -e ‘set content_type="text/html"‘ $mailbox < $maillog #python mailsend.py $maillog "aaa $sendip" } handlemail exit 0 PS: 1、如果使用mutt的话 那么请先参考以下链接安装 http://7938217.blog.51cto.com/7928217/1641803 2、如果使用python的话 那么使用以下python代码
cat mailsend.py
#!/usr/bin/python # -*- coding: utf-8 -*- #-------------------------------------------------- #Author:jimmygong #Email:[email protected] #FileName:mailsend.py #Function: #Version:1.0 #Created:2015-10-30 #-------------------------------------------------- import sys import smtplib from email import encoders from email.mime.base import MIMEBase from email.mime.text import MIMEText from email.utils import COMMASPACE from email.mime.multipart import MIMEMultipart def send_mail(fro,to,subject,text,txttype=‘html‘,files=[]): msg = MIMEMultipart() msg[‘From‘] = fro msg[‘Subject‘] = subject msg[‘To‘] = COMMASPACE.join(to) if txttype=="html": msg.attach(MIMEText(text,‘html‘)) else: msg.attach(MIMEText(text)) for file in files: part = MIMEBase(‘application‘, ‘octet-stream‘) part.set_payload(open(file, ‘r‘).read()) encoders.encode_base64(part) part.add_header(‘Content-Disposition‘, ‘attachment; filename="%s"‘ % os.path.basename(file)) msg.attach(part) s = smtplib.SMTP() s.connect(‘mail.shidc.taomee.com‘) s.sendmail(fro,to,msg.as_string()) s.quit() if __name__==‘__main__‘: fro=‘[email protected]‘ to=[‘[email protected]‘,] subject=sys.argv[2] with open(sys.argv[1],"r") as f: text=f.read() txttype=‘html‘ files=[] send_mail(fro,to,subject,text,txttype,files)
3、一些说明 sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径,所以参数从1开始. 三、效果
时间: 2024-10-13 09:38:14