IBM supervessel power云平台 之 send mail 篇

声明 : 此文档只做学习交流使用,请勿用作其他商业用途 author : 朝阳_tony E-mail : linzhaolover@163.com Create Date: 2015-3-9 22:55:43 Monday Last Change: 2015-3-9 22:55:52 Monday 转载请注明出处:

摘要: 朝弟,咱们今天有个新的任务,在你的机器上运行一个测试实例,然后将结果通过mail发送到我的邮箱,方便后期查阅!先通过文本模式发送,后期改为html格式的,增加美观。

程序运行平台

IBM supervessel power云平台 https://ptopenlab.com/cloudlab/index.html

测试获取ifconfig信息opuserifconfig > ifconfig_info.txtopusercat ifconfig_info.txtethinet ..255.0inet6 e663/((4.8 MB)loinet .0.0inet6 /((954.0 B)

测试结果有了,那么接下来就是要将结果发送到我的mail中去

sys, os, string, timeimport smtplib,base64, StringIOfrom email.MIMEMultipart import MIMEMultipartfrom email.MIMEText import MIMETextfrom email.Utils import COMMASPACE, formatdatefrom email import Encodersdef send_mail(send_from, send_to,subject, text,server=”localhost”, user = None, password = None):assert type(send_to)==listmsg = MIMEMultipart()msg[‘From’] = send_frommsg[‘To’] = COMMASPACE.join(send_to)msg[‘Date’] = formatdate(localtime=True)msg[‘Subject’] = subjectmsg.attach( MIMEText(text) )try:#smtp = smtplib.SMTP(server)smtp = smtplib.SMTP_SSL(server)if (user != None):smtp.ehlo()smtp_userid64 = base64.encodestring(user)smtp.docmd(“auth”, “login ” + smtp_userid64[:-1])if password != None:smtp_pass64 = base64.encodestring(password)smtp.docmd(smtp_pass64[:-1])smtp.sendmail(send_from, send_to, msg.as_string())smtp.quit()Exception as e:print “send maill error”, e### set mail server config### 465 is open ssl port mail_server=’smtp.163.com:465’CODEC = ‘utf-8’mail_subject=’Test result report mail’### read test result information, add to mail , use ‘utf-8’ encodertext=’This is my first mail , for ifconfig infomations!’fp=open(‘ifconfig_info.txt’,’r’)text=text+’\n’+fp.read()mailtext=text.decode(CODEC)fp.close()mail_from=’myaccount@163.com’user=’myaccount’passwd=’mypassword’send_to=[‘zhaoyang_tony@163.com’]send_mail(mail_from, send_to, mail_subject, mailtext,mail_server,user ,passwd )print ‘Done’

注意: 465是mail服务器的加密端口,通常使用这个相对安全一些,当然你也可以更换为非加密端口25, 但这时要用smtp = smtplib.SMTP(server) 注释掉下面那加密那行#smtp = smtplib.SMTP_SSL(server)。

mail内容是通过read直接读取的文件内容,然后通过utf-8编码发送的邮件内容, 后期会考虑修改为html格式,会更加的美观一些,,

run demoopuserpython Smail.py有图有真相

总结: 老板收邮件

思想如钻子,必须集中在一点钻下去才有力量

IBM supervessel power云平台 之 send mail 篇

相关文章:

你感兴趣的文章:

标签云: