展开全部
#coding=utf-8
62616964757a686964616fe58685e5aeb931333337613862'''
create this file
Make it easier to maintain
'''
MailList = {'ablhy' : ['Yeah2015'],
'test' : ['testformail'],
}
def send(text, to, subject, imgpath="no"):
import smtplib
from email.Header import Header
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from email.Utils import COMMASPACE, formatdate
from email import Encoders
assert type(to)==list
#fro: 发邮件人的Email, server :mail server ..
fro = "xx.xx.xx.xx"
server="xx.xx.xx.xx"
msg = MIMEMultipart()
To = to
msg['From'] = fro
msg['To'] = COMMASPACE.join(To)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = Header(subject, 'utf-8')
#文本内容
msg.attach( MIMEText(text,'html', 'utf-8'))
#附件:两图片
msg.attach(MIMEImage(open("./test.jpg",'rb').read()))
msg.attach(MIMEImage(open("./testpng.png",'rb').read()))
if (imgpath != "no"):
f = open(imgpath, 'rb')
msgImage = MIMEImage(f.read())
f.close()
msgImage.add_header('Content-ID', '')
msg.attach(msgImage);
smtp = smtplib.SMTP(server)
smtp.sendmail(fro, To, msg.as_string() )
smtp.close()
def sendEmail(text, to, subject, imgpath='no'):
if MailList.has_key(to):
send(text, MailList[to], subject, imgpath)
else:
send(text, MailList['test'], subject, imgpath)
#测试
if __name__ == '__main__':
sendEmail('Test', 'ablhy', '删除日志提醒');