python判断邮箱类型_python程序发送服务器状态信息报警邮件(带多类型附件)

最近为了加强服务器监管信息,于是为所有服务器配置邮件增设报警监测功能,LINUX服务器可以使用SHELL即可轻松解决,但是为了通用性(还有部分WINDOWS系统服务器),后来改为使用Python程序实现其功能。现将邮件部分代码分享其实现过程的简单思路:

程序支持发送多种类型附件邮件:代码如下

#!/usr/bin/python

#coding: utf-8

#author by jerry 2017.1

import smtplib

from email.mime.text import MIMEText

from email.header import Header

import string

from email.parser import Parser

from email.mime.multipart import MIMEMultipart

from email.mime.application import MIMEApplication

host = "baidu.com"

sender = "username@cdbaidu.com"

receivers = "username@cdbaidu.com"

user="username"

authcode="password"

mybody="this is body content test."

subject="jerry's test"

message=MIMEMultipart()

# 邮件正文添加

mybody = MIMEText("good,send mail ok")

message.attach(mybody)

#---这是附件部分---

att1 =MIMEApplication(open('IDC server Reports.xls','rb').read(),_charset='utf-8')

att1.add_header('Content-Disposition', 'attachment', filename="IDC server Reports.xls")

message.attach(att1)

att1=MIMEApplication (open('domain.txt','rb').read())

att1.add_header('Content-Disposition', 'attachment', filename="domain.txt")

message.attach(att1)

att1=MIMEApplication (open('test.rar','rb').read())

att1.add_header('Content-Disposition', 'attachment', filename="test.rar")

message.attach(att1)

#整合

headers = Parser().parsestr('From: username@163.com\n'

'To: username@cdbaidu.com\n'

'Subject:%s' % subject +'\n'

'%s'% message +'\n'

)

try:

server = smtplib.SMTP(host)

server.login(user,authcode) #远程smtp主机方法。引号中是帐号和密码

server.sendmail(sender,receivers,headers.as_string())

server.quit() #断开smtp服务器

print ("Mail sent successfully")

except smtplib.SMTPException as e:

print(e)

print ("Mail sendfail!")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值