码迷,mamicode.com
首页 > 其他好文 > 详细

IBM supervessel power云平台 之 send mail 篇

时间:2015-03-10 00:14:05      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:sendmail   云平台   

声明 : 此文档只做学习交流使用,请勿用作其他商业用途
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
转载请注明出处:http://blog.csdn.net/linzhaolover

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

程序运行平台

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

测试获取ifconfig信息

opuser@gto:~/Smail$ ifconfig >  ifconfig_info.txt
opuser@gto:~/Smail$ cat ifconfig_info.txt
eth0      Link encap:Ethernet  HWaddr fa:16:3e:2f:e6:63
          inet addr:10.10.1.29  Bcast:10.10.1.255  Mask:255.255.255.0
          inet6 addr: fe80::f816:3eff:fe2f:e663/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1454  Metric:1
          RX packets:33241 errors:0 dropped:0 overruns:0 frame:0
          TX packets:19081 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:7482632 (7.4 MB)  TX bytes:4848283 (4.8 MB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:10 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:954 (954.0 B)  TX bytes:954 (954.0 B)

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

#!/usr/bin/env python
# -*- coding:UTF-8 -*-
import sys, os, string, time
import smtplib,base64, StringIO
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
def send_mail(send_from, send_to,subject, text,
          server="localhost", user = None, password = None):
    assert type(send_to)==list
    msg = MIMEMultipart()
    msg[‘From‘] = send_from
    msg[‘To‘] = COMMASPACE.join(send_to)
    msg[‘Date‘] = formatdate(localtime=True)
    msg[‘Subject‘] = subject
    msg.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()
        print "Send Ok"
    except 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‘ encoder
text=‘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 demo

opuser@gto:~/Smail$ python Smail.py
Send Ok
Done

有图有真相

技术分享

总结: 老板收邮件

IBM supervessel power云平台 之 send mail 篇

标签:sendmail   云平台   

原文地址:http://blog.csdn.net/linzhaolover/article/details/44162765

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!