Notice that ConsoleMessage.lineReceived
is called with each line of the message. Each line has a size (similar to its length, no doubt). You can tally the size of all the lines as they are received and take action based on the result.
Additionally, you could explore the SIZE
ESMTP extension which allows the server to declare the maximum message size which will be accepted. This does not replace checking in the code handling message lines since there's no guarantee a client will respect the declared maximum but in the case of a smart, cooperating client it will save some pointless data transfer.
SIZE
is a simple enough extension that you can probably add it to Twisted's ESMTP server by subclassing twisted.mail.smtp.ESMTP
and overriding the extensions
method to add it.