Robot Framework通过Python SMTP进行email收发测试。

python

工作中需要对发送的邮件进行过滤,方法基本属于ACL控制,即查看“源/目的”邮件地址,邮件标题,邮件正文,邮件附件等进行过滤。

所以需要先模拟一下用Python能否达到邮件Client,Server的功能,还有能否在server上显示发送的  “源/目的”邮件地址,邮件标题,邮件正文,邮件附件  这些信息。

如果能取到这些信息,然后就可以对这些字符串进行查找,然后判断过滤功能是否work的目的了。

现在咱们先看下邮件服务器:

mailserver.py:

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:

  将此邮件服务器做成类似于linux服务:

  1.将此程序放到/home目录下:

[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_attachment和send_mail_no_attachment两个函数中的from_password参数其实没有用,可以删掉。然后如果你想用base64加密,可以取消注销掉Encoders.encode_base64(part)这一行。

其实功能已经实现了,要是想让它在RobotFramework中work就是轻而易举的事儿了。

以上是 Robot Framework通过Python SMTP进行email收发测试。 的全部内容, 来源链接: utcz.com/z/388060.html

回到顶部