标签:png python程序 header images 引号 种类 实现 report 思路
最近为了加强服务器监管信息,于是为所有服务器配置邮件增设报警监测功能,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!")
标签:png python程序 header images 引号 种类 实现 report 思路
原文地址:http://blog.51cto.com/jdonghong/2072394