【发布时间】:2014-06-08 22:16:45
【问题描述】:
这是我用来创建使用 twisted 的 SMTP 服务器的非常基本的代码:
from email.Header import Header
from twisted.internet import protocol, defer, reactor
from twisted.mail import smtp
from zope.interface import implements
class ConsoleMessageDelivery(object):
implements(smtp.IMessageDelivery)
def receivedHeader(self, helo, origin, recipients):
myHostname, clientIP = helo
headerValue = "by %s from %s with ESMTP ; %s" % (myHostname,
clientIP,
smtp.rfc822date())
return "Received: %s" % Header(headerValue)
def validateFrom(self, helo, origin):
# All addresses are accepted
return origin
def validateTo(self, user):
# Only messages directed to the "console@domain" user are accepted.
if user.dest.local == "console":
return lambda: ConsoleMessage()
raise smtp.SMTPBadRcpt(user)
class ConsoleMessage(object):
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)
self.lines = None
return defer.succeed(None)
def connectionLost(self):
# There was an error, throw away the stored lines
self.lines = None
class LocalSMTPFactory(smtp.SMTPFactory):
def buildProtocol(self, addr):
smtpProtocol = smtp.ESMTP()
smtpProtocol.delivery = ConsoleMessageDelivery()
return smtpProtocol
reactor.listenTCP(2025, LocalSMTPFactory())
reactor.run()
我可以接收电子邮件,但如果我想拒绝大小为 1MB 或更大的传入消息,我该怎么做?
【问题讨论】:
标签: python email python-2.7 smtp twisted