Robot Framework經過Python SMTP進行email收發測試。

工做中須要對發送的郵件進行過濾,方法基本屬於ACL控制,即查看「源/目的」郵件地址,郵件標題,郵件正文,郵件附件等進行過濾。linux

因此須要先模擬一下用Python可否達到郵件Client,Server的功能,還有可否在server上顯示發送的  「源/目的」郵件地址,郵件標題,郵件正文,郵件附件  這些信息。vim

若是能取到這些信息,而後就能夠對這些字符串進行查找,而後判斷過濾功能是否work的目的了。服務器

 

如今我們先看下郵件服務器:app

 

mailserver.py:async

import smtpd
import asyncore

class CustomSMTPServer(smtpd.SMTPServer):
    
    def process_message(self, peer, mailfrom, rcpttos, data):
        print 'Receiving message from:', peer
        print 'Message addressed from:', mailfrom
        print 'Message addressed to  :', rcpttos
        print 'Message length        :', len(data)
        print 'data                  :', data
        return

server = CustomSMTPServer(('172.18.0.100', 9999), None)

asyncore.loop()

這個就是郵件服務器,保存完後,直接雙擊運行就好,它會一直運行的。函數

 

UPDATE:oop

  將此郵件服務器作成相似於linux服務:測試

  1.將此程序放到/home目錄下:ui

[root@Server init]# vim /home/mailserver.py 

import smtpd
import asyncore

class CustomSMTPServer(smtpd.SMTPServer):
    
    def process_message(self, peer, mailfrom, rcpttos, data):
        self.peer = peer
        self.mailfrom = mailfrom
        self.rcpttos = rcpttos
        self.data = data
        mail_log = open('/home/current_mail.log','wb+')
        mail_log.write(str('Receiving message from:')+ str(self.peer)+'\n')
        mail_log.write(str('Message addressed from:')+ str(self.mailfrom)+'\n')
        mail_log.write(str('Message addressed to  :')+ str(self.rcpttos)+'\n')
        mail_log.write(str('Message length        :')+ str(len(self.data))+'\n')
        mail_log.write(str('data                  :')+ str(self.data)+'\n')
        mail_log.close()
        return

server = CustomSMTPServer(('172.16.0.3', 9999), None)

asyncore.loop()

這個程序會在當前目錄下建立郵件log,用來判斷客戶端發送的請求是否正確。加密

  

  2.在/etc/init目錄下(不是/etc/init.d)建立mailserver.conf這個文件,而後填寫以下配置:

[root@Server init]# vim mailserver.conf 
description "mailserver is in /home/mailserver.py"
author "xxx@xxx.com"

start on runlevel [2345]
stop on runlevel [!2345]

#env AN_ENVIRONMENTAL_VARIABLE=i-want-to-set

respawn

exec /home/mailserver.py

  3. 執行「initctl reload-configuration"命令

  4. 而後能夠經過如下命令啓動或關閉郵件服務器:

 

start mailserver
stop mailserver
restart mailserver

 

 

 

 

 

 

郵件客戶端:

Step1: 先在pyton目錄下建立一個自定義的文件夾「SelfEmailLibrary」(您的Python安裝到哪裏就相應的放到哪裏):

F:\Python27\Lib\site-packages\SelfEmailLibrary

 

Step2: 將下面的代碼複製到「mailsend.py」和「__init__.py」中。

 

mailsend.py:

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os

class SendEmailUtility(object):


    ROBOT_LIBRARY_SCOPE = 'Global'
    
    def __init__(self):
        print 'send email utility'
        
    def send_mail_with_attachment(self, from_user, from_password, to, subject, text, attach, mailserver, serverport):
        self.mailserver = str(mailserver)
        self.serverport = int(serverport)
        msg = MIMEMultipart()
    
        msg['From'] = from_user
        msg['To'] = to
        msg['Subject'] = subject

        msg.attach(MIMEText(text))

        part = MIMEBase('application', 'octet-stream')
        part.set_payload(open(attach, 'rb').read())
        #Encoders.encode_base64(part)
        part.add_header('Content-Disposition',
               'attachment; filename="%s"' % os.path.basename(attach))
        msg.attach(part)

        mailServer = smtplib.SMTP(self.mailserver, self.serverport)
        mailServer.ehlo()
        #mailServer.starttls()
        mailServer.ehlo()
        #mailServer.login(from_user, from_password)
        mailServer.sendmail(from_user, to, msg.as_string())
        # Should be mailServer.quit(), but that crashes...
        mailServer.close()

    def send_mail_no_attachment(self, from_user, from_password, to, subject, text,
                                mailserver, serverport):
        self.mailserver = str(mailserver)
        self.serverport = int(serverport)
        
        msg = MIMEMultipart()

        msg['From'] = from_user
        msg['To'] = to
        msg['Subject'] = subject
        msg.attach(MIMEText(text))

        mailServer = smtplib.SMTP(self.mailserver, self.serverport)
        mailServer.ehlo()
        #mailServer.starttls()
        mailServer.ehlo()
        #mailServer.login(from_user, from_password)
        mailServer.sendmail(from_user, to, msg.as_string())
        # Should be mailServer.quit(), but that crashes...
        mailServer.close()

 

__init__.py:

from mailsend import SendEmailUtility
__version__ = '1.0'

class SelfEmailLibrary(SendEmailUtility):

       
    ROBOT_LIBRARY_SCOPE = 'GLOBAL'

Step3:建立一個腳本測試一下這兩個function是否工做:

mailclient.py:

from SelfEmailLibrary import *


a = SendEmailUtility()

a.send_mail_no_attachment(from_user="aaa@aaa.com", from_password='', to="bbb@bbb.com", subject="selftry",
                          text="asdlfkajsdlfd", mailserver="172.18.0.100", serverport="9999")



a.send_mail_with_attachment(from_user="ddd@ddd.com", from_password='', to="ccc@ccc.com",
                            subject="selftry",text="bbbbbbbbbb", attach='simple.txt',
                            mailserver="172.18.0.100", serverport="9999")

 

Step4: 測試這倆函數對不對:

1. 先雙擊開啓mailserver.py。

2. 執行mailclient.py。

3. 查看mailserver的顯示是否正確:

 

能夠看到,郵件的各個屬性還有附件內容(simple.txt的內容就是「testkeyword」)均可以查看到。這樣咱們就能夠經過查看mailserver收到的DATA來判斷過濾功能了。

 

 

Note:

send_mail_with_attachmentsend_mail_no_attachment兩個函數中的from_password參數其實沒有用,能夠刪掉。而後若是你想用base64加密,能夠取消註銷掉Encoders.encode_base64(part)這一行。其實功能已經實現了,要是想讓它在RobotFramework中work就是垂手可得的事兒了。
相關文章
相關標籤/搜索