因此,在对@ Jean-PaulCalderone的大量耐心和感激之后,我现在有了一个支持(尚待测试)TLS的扭曲邮件服务器!我可以telnet到它,发出我的EHLO,MAIL FROM等,并根据下面的代码片段将消息转储到文件中(以下2个类从http://twistedmatrix.com/documents/current/_downloads/emailserver.tac翻录和修改:
class ConsoleMessageDelivery:
implements(smtp.IMessageDelivery)
def receivedHeader(self, helo, origin, recipients):
by = helo[1]
from_ = origin
for_ = "%s" % (' '.join(map(str, recipients)))
return "Client: %s\nFrom: %s\nTo: %s\n\n" % (by, from_, for_)
def validateFrom(self, helo, origin):
return origin
def validateTo(self, user):
return lambda: ConsoleMessage()
class ConsoleMessage:
implements(smtp.IMessage)
def __init__(self):
self.lines = []
def lineReceived(self, line):
self.lines.append(line)
def eomReceived(self):
print "New message received:"
print "\n".join(self.lines)
msgFile = open("./test.msg","w")
msgFile.writelines(self.lines)
msgFile.close()
self.lines = None
return defer.succeed(None)
def connectionLost(self):
# There was an error, throw away the stored lines
self.lines = None
但是,当我指定多个RCPT TO地址时,问题就出现了 - 只有最后一个在我的receiveheader方法中的“收件人”中?我在“receivedHeader”中处理不当吗?我开始认为它是我的validateTo函数中的return语句。当我向2个收件人发送单个邮件时,服务器日志如下所示:
2014-10-21 08:05:10+0000 [ESMTP,2,127.0.0.1] Receiving message for delivery: from=me@home.com to=['you@work1.com', 'you@work2.com']
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1] New message received:
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1] Client: 127.0.0.1
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1] From: me@home.com
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1] To: you@work1.com
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1]
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1]
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1]
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1]
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1] Today is the 21st Oct
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1] (still)
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1] New message received:
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1] Client: 127.0.0.1
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1] From: me@home.com
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1] To: you@work2.com
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1]
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1]
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1]
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1]
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1] Today is the 21st Oct
2014-10-21 08:05:17+0000 [ESMTP,2,127.0.0.1] (still)
此外,能够访问“完整”SMTP标头 - 消息ID等等将是件好事。我当然看了smtp.IMessage / smtp.IMessageDelivery的API文档 - 但看不出任何相关内容?最终,创建一个文件(可能是2,拆分标题/正文)会很好,名称基于msg ID ......
有人可以解释一下我缺少的东西吗?非常感谢!
答案 0 :(得分:1)
您的代码和输出看起来非常好。
不分组的收件人的行为是由于lambda
语句触发self.lines.append()
每个收件人/邮件的发送。
但是,您可以考虑更改lineReceived
的行为,以便对收到的所有行进行分组。
祝你好运,希望这会有所帮助。