邮箱附件脚本
邮箱附件脚本
#!/usr/bin/env python
#coding:utf-8
############################################
#邮箱附件脚本
#cnsre 2018-6-11 V1
#
#修改者:xxx
#修改时间:2018-xx-xx
#修改内容:修改内容描述
############################################
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
mailto_list=['xuewenlong@qq.com'] #收件人(列表)
mail_host="smtp.189.cn" #使用的邮箱的smtp服务器地址
mail_user="xuewenlong@189.cn" #用户名
mail_pass="xuewenlong" #密码
mail_postfix="189.com" #邮箱的后缀
def make_mpa_msg():
email = MIMEMultipart('alterbative') #构建附件
text = MIMEText(open('/home/data/xunjian/log/lns1hz.txt', 'rb').read(), 'base64', 'utf-8') #发送当前路径下的文件
##这个路径要绝对路径,不然的话会在cronteb里边定时任务无法执行!!
text["Content-Disposition"] = 'attachment; filename="lns1hz.txt"' #附件名称
email.attach(text)
return email
def send_mail(to_list,sub,content):
me="巡检邮件"+"<"+mail_user+"@"+mail_postfix+">"
msg = make_mpa_msg()
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list) #将收件人列表以‘;’分隔
try:
server = smtplib.SMTP()
server.connect(mail_host) #连接服务器
server.login(mail_user,mail_pass) #登录操作
server.sendmail(me, to_list, msg.as_string())
server.close()
return True
except Exception, e:
print str(e)
return False
for i in range(1): #发送1封
if send_mail(mailto_list,"杭州lns1巡检","msg.as_string()"): #邮件主题和邮件内容
print "发送成功"
else:
print "发送失败"
作者:SRE运维博客
博客地址: https://www.cnsre.cn/