python mail指令碼應用

來源:互聯網
上載者:User

http://blog.csdn.net/jrckkyy/archive/2010/03/12/5373427.aspx

 

前一陣花了點時間學習python,近段時間完成了一個監控伺服器基本資料的項目,都是為了滿足大家監控的慾望,特殊日誌並警示的分布式系統,單台伺服器採集粒度為1次/1分鐘,一天大約1440條,目前監控了20多台伺服器,一天大約31680條日誌,現在單點監控中心伺服器在效能上還綽綽有餘,有更多的伺服器來測試就好了,估計可以支援到100台以上伺服器監控的層級。

現在遇到一個需求是發現警示時即時發送訊息給相關人員,由於公司簡訊網關只買了上海電信使用者沒有上海電信的號碼,汗一個,只好通過發郵件來實施。

支援發送GB18030編碼的常值內容,任意編碼附件,可以做出適當修改支援群發。

#coding=utf-8  
#!/usr/lib/python2.5/bin/python  
import os  
import sys  
from smtplib import SMTP  
from email.MIMEMultipart import MIMEMultipart  
from email.mime.application import MIMEApplication  
from email.MIMEText import MIMEText  
from email.MIMEBase import MIMEBase  
from email import Utils,Encoders  
import mimetypes  
import time  
 
STMP_SERVER = "mail.×××.com" 
STMP_PORT = "25" 
USERNAME = "×××@×××.com" 
USERPASSWORD = "×××" 
FROM = "MonitorCenterWarning@×××.com" 
TO = "×××@gmail.com" 
 
def sendFildByMail(config):  
    print 'Preparing...' 
    message = MIMEMultipart( )  
    message['from'] = config['from']  
    message['to'] = config['to']  
    message['Reply-To'] = config['from']  
    message['Subject'] = config['subject']  
    message['Date'] = time.ctime(time.time())  
    message['X-Priority'] =  '3' 
    message['X-MSMail-Priority'] =  'Normal' 
    message['X-Mailer'] =  'Microsoft Outlook Express 6.00.2900.2180' 
    message['X-MimeOLE'] =  'Produced By Microsoft MimeOLE V6.00.2900.2180' 
      
    if 'file' in config:  
        #添加附件  
        f=open(config['file'], 'rb')  
        file = MIMEApplication(f.read())  
        f.close()  
        file.add_header('Content-Disposition', 'attachment', filename= os.path.basename(config['file']))  
        message.attach(file)  
      
    if 'content' in config:  
        #添加常值內容  
        f=open(config['content'], 'rb')  
        f.seek(0)  
        content = f.read()  
        body = MIMEText(content, 'base64', 'gb2312')  
        message.attach(body)  
 
    print 'OKay' 
    print 'Logging...' 
    smtp = SMTP(config['server'], config['port'])  
    #如果SMTP伺服器發郵件時不需要驗證登入則對下面這行加上注釋  
    smtp.login(config['username'], config['password'])  
    print 'OK' 
      
    print 'Sending...',  
    smtp.sendmail (config['from'], [config['from'], config['to']], message.as_string())  
    print 'OK' 
    smtp.close()  
    time.sleep(1)  
 
if __name__ == "__main__":  
    if len(sys.argv) < 2:  
        print 'Usage: python %s contentfilename' % os.path.basename(sys.argv[0])  
        print 'OR Usage: python %s contentfilename attachfilename' % os.path.basename(sys.argv[0])  
        wait=raw_input("quit.")  
        sys.exit(-1)  
    elif len(sys.argv) == 2:  
        sendFildByMail({  
            'from': FROM,  
            'to': TO,  
            'subject': '[MonitorCenter]Send Msg %s' % sys.argv[1],  
            'content': sys.argv[1],  
            'server': STMP_SERVER,  
            'port': STMP_PORT,  
            'username': USERNAME,  
            'password': USERPASSWORD})  
    elif len(sys.argv) == 3:  
        sendFildByMail({  
            'from': FROM,  
            'to': TO,  
            'subject': '[MonitorCenter]Send Msg and File %s %s' % (sys.argv[1], sys.argv[2]),  
            'content': sys.argv[1],  
            'file': sys.argv[2],  
            'server': STMP_SERVER,  
            'port': STMP_PORT,  
            'username': USERNAME,  
            'password': USERPASSWORD})  
    wait=raw_input("end.") 
#coding=utf-8
#!/usr/lib/python2.5/bin/python
import os
import sys
from smtplib import SMTP
from email.MIMEMultipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import Utils,Encoders
import mimetypes
import time

STMP_SERVER = "mail.×××.com"
STMP_PORT = "25"
USERNAME = "×××@×××.com"
USERPASSWORD = "×××"
FROM = "MonitorCenterWarning@×××.com"
TO = "×××@gmail.com"

def sendFildByMail(config):
    print 'Preparing...'
    message = MIMEMultipart( )
    message['from'] = config['from']
    message['to'] = config['to']
    message['Reply-To'] = config['from']
    message['Subject'] = config['subject']
    message['Date'] = time.ctime(time.time())
    message['X-Priority'] =  '3'
    message['X-MSMail-Priority'] =  'Normal'
    message['X-Mailer'] =  'Microsoft Outlook Express 6.00.2900.2180'
    message['X-MimeOLE'] =  'Produced By Microsoft MimeOLE V6.00.2900.2180'
   
    if 'file' in config:
        #添加附件
        f=open(config['file'], 'rb')
        file = MIMEApplication(f.read())
        f.close()
        file.add_header('Content-Disposition', 'attachment', filename= os.path.basename(config['file']))
        message.attach(file)
   
    if 'content' in config:
        #添加常值內容
        f=open(config['content'], 'rb')
        f.seek(0)
        content = f.read()
        body = MIMEText(content, 'base64', 'gb2312')
        message.attach(body)

    print 'OKay'
    print 'Logging...'
    smtp = SMTP(config['server'], config['port'])
    #如果SMTP伺服器發郵件時不需要驗證登入則對下面這行加上注釋
    smtp.login(config['username'], config['password'])
    print 'OK'
   
    print 'Sending...',
    smtp.sendmail (config['from'], [config['from'], config['to']], message.as_string())
    print 'OK'
    smtp.close()
    time.sleep(1)

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print 'Usage: python %s contentfilename' % os.path.basename(sys.argv[0])
        print 'OR Usage: python %s contentfilename attachfilename' % os.path.basename(sys.argv[0])
        wait=raw_input("quit.")
        sys.exit(-1)
    elif len(sys.argv) == 2:
        sendFildByMail({
            'from': FROM,
            'to': TO,
            'subject': '[MonitorCenter]Send Msg %s' % sys.argv[1],
            'content': sys.argv[1],
            'server': STMP_SERVER,
            'port': STMP_PORT,
            'username': USERNAME,
            'password': USERPASSWORD})
    elif len(sys.argv) == 3:
        sendFildByMail({
            'from': FROM,
            'to': TO,
            'subject': '[MonitorCenter]Send Msg and File %s %s' % (sys.argv[1], sys.argv[2]),
            'content': sys.argv[1],
            'file': sys.argv[2],
            'server': STMP_SERVER,
            'port': STMP_PORT,
            'username': USERNAME,
            'password': USERPASSWORD})
    wait=raw_input("end.")
 

windows xp下:

 

 linux ubuntu,suse下:

 

收到的結果:

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.