python发邮件功能怎么实现_python实现发送邮件功能

#!/usr/bin/env python#coding: utf-8

importsmtplib, re, sys, osimportxlwtfrom email.mime.multipart importMIMEMultipartfrom email.mime.text importMIMETextfrom email.mime.image importMIMEImagedefchuli_data(filepath):

list1=[]

with open(filepath) as fobj:for line infobj:if re.match(".*\"_source\".*",line):

list1.append([])if re.match(".*\"@timestamp\".*",line):

tmp_value= re.findall(".*\"@timestamp\" *: *\"(.*)\" *",line)

list1[-1].append(tmp_value[0])if re.match(".*\"servicetype\".*",line):

tmp_value= re.findall(".*\"servicetype\" *: *\"(.*)\" *",line)

list1[-1].append(tmp_value[0])if re.match(".*\"source\".*",line):

tmp_value= re.findall(".*\"source\" *: *\"(.*)\" *",line)

list1[-1].append(tmp_value[0])if re.match(".*\"logmsg\".*",line):

tmp_value= re.findall(".*\"logmsg\" *: *\"(.*)\" *",line)

list1[-1].append(tmp_value[0])for ele inlist1:printelereturnlist1deftrans(str1):

upperlist=string.uppercase

str1=int(str1)if str1 < 26:

trans_str=upperlist[str1]else:

par= int(str1/26)

sub= str1 % 26trans_str=upperlist[sub]

trans_str= trans(par) +trans_strreturntrans_strdefrewrite_xml(list1):

wb=xlwt.Workbook()

sheet= wb.add_sheet('error日志')

ws=wb.active

col_num= 1

for row inlist1:

row_num=0for col inrow:

ws[trans(row_num)+str(col_num)] =col

row_num+= 1col_num+= 1wb.save("/tmp/tmp.xlsx")returnTruedefsend_email(config_file,FROM,TO,xlsxfile_list,email_password):

location_name="unknown"SUBJECT="error日志"with open(config_file) as fobj:for line infobj:if re.match(".*",line):

SUBJECT= re.search("(.*)",line).groups()[0]

location_name= re.split("Scarlet",SUBJECT)[0]breaklocation_name= "[%s]" % location_name.decode('utf-8')

HOST= "smtp.ipanel.cn"SUBJECT= SUBJECT.decode('utf-8')

missfile_list=[]

errfile_list=[]

inscribe= """

personal infomation

your own description

"""

for file inxlsxfile_list:if notos.path.isfile(file):

missfile_list.append(os.path.basename(file))ifmissfile_list:

mail_head= """All:

    附件是今日的errlog,请及时查收

    此邮件为系统自动发送,请勿回复


""" % ",".join(missfile_list)else:

mail_head= """
"""msg= MIMEMultipart('related')

n= 1contents= "%s
" %mail_headfor pngfile inpngfile_list:try:

msg.attach(addimg(pngfile,"daily"+str(n)))

contents+= "" %nif n % 2 ==0:

contents+= "
"

exceptException,e:

errfile_list.append(os.path.basename(pngfile))finally:

n+= 1contents+=inscribe

msgtext= MIMEText(contents,"html","utf-8")

msg.attach(msgtext)try:

attach1= MIMEText(open(xlsxfile_list[0],"rb").read(), "base64", "UTF-8")

time_tag= "-".join(re.split("[_.]",xlsxfile_list[0])[-3:-1])

attach1["Content-Type"] = "application/octet-stream"attach1["Content-Disposition"] = "attachment; filename=\"%s error日志 %s.xlsx\"" % (location_name.encode('utf-8'),time_tag)exceptException,e:

errfile_list.append(os.path.basename(xlsxfile_list[0]))

msg.attach(attach1)

msg['Subject'] =SUBJECT

msg['From'] =FROM

msg['To'] = ";".join(TO)try:

server=smtplib.SMTP()

server.connect(HOST,"25")

server.login("Scarlet@zzz.cn",email_password)

server.sendmail(FROM, TO, msg.as_string())

server.quit()print "邮件发送成功!"

returnTrueexceptException, e:print "失败:"+str(e)returnFalseif __name__ == '__main__':

config_file= "/opt/Scarlet/elkerrlog/monitor_elk.conf"FROM= "Scarlet@zzz.cn"TO= ["Scarlet@zzz.cn"]

FILELOG= "logfile.`date +%Y%m%d`"xlsxfile_list= [r"/opt/Scarlet/elkerrlog/$FILELOG"]

email_password= "Scarlet_password"list1=chuli_data(xlsxfile_list[0])

rewrite_xml(list1)

tmp_result= send_email(config_file,FROM,TO,/tmp/tmp.xlsx,email_password)if nottmp_result:

sys.exit(1)

sys.exit(0)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值