gpt4 book ai didi

python - Twisted SMTP 服务器 - 获取完整的收件人列表和 header 数据?

转载 作者:太空宇宙 更新时间:2023-11-03 18:08:11 24 4
gpt4 key购买 nike

因此,经过 @Jean-PaulCalderone 的巨大耐心和感激之后,我现在拥有了一个支持(尚未测试)TLS 的扭曲邮件服务器!我可以远程登录到它,发出我的 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 header (消息 ID 等)就好了。我当然查看了 smtp.IMessage/smtp.IMessageDelivery 的 API 文档 - 但看不到任何相关内容?最终,最好创建一个文件(可能是 2 个,拆分 header /正文),其名称以某种方式基于 msg ID...

有人可以解释一下我缺少什么吗?非常感谢!

最佳答案

您的代码和输出看起来绝对没问题。收件人不分组的行为是由于lambda语句触发了每个收件人/邮件发出的self.lines.append()

但是,您可以考虑更改 lineReceived 的行为,以便可以对收到的所有线路进行分组。

祝你好运,希望这会有所帮助。

关于python - Twisted SMTP 服务器 - 获取完整的收件人列表和 header 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26468043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com