Receiving email with SMTPServer in python:

2019-05-10 14:56发布

问题:

I have a small application under Linux to receive an email with the use of smtpd.SMTPServer. Here is the small test code:

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)
        return

server = CustomSMTPServer(('0.0.0.0', 25), None)
asyncore.loop()

I have the following issues: (1) When using this piece of code, the computer sending the email gets the following message: 502 Error: command "EHLO" not implemented so the server cannot reply correctly to receive further data / communicate with the email-sending computer (which I assume is the client).

Shouldn't such a basic thing like EHLO be implemented in a Ubuntu installation in the first place? Why is it not implemented?

(2) I figured that EHLO can be installed by installing postfix in Ubuntu. I did that and the same test call went on, but stopped later with a different error:

Client: RCPT TO: XXX@YYY.com
Server: 554 5.7.1 <XXX@YYY>: Relay access denied

(3) At later times, after doing some more other tests, I got the error from the test code itself:

error: [Errno 98] Address already in use

It turns out that the used IP address was already in use as could be seen with

netstat -lnpt

of which the case was the running postfix. After stopping the postfix service the address was no longer in use, but of course it was back to issue #1:

502 Error: command "EHLO" not implemented

I would like to be able to use a SMTPServer to receive an email message 1. without the need to install postfix 2. with the use of asyncore

If there are any ideas of how to make this possible in an easy and simple way using python libraries that would be great!

Cheers Alex

回答1:

1) Postfix is an SMTP server, it has nothing to do with python's smtpd EHLO implementation. If you want your custom SMTP server, you don't need postfix, so feel free to remove it.

2) EHLO is a ESMTP command, not SMTP, standard smtpd python module implements SMTP, therefore it doesn't have an EHLO implementation.



回答2:

Try this. Of course, it does not implement the EHLO command, but makes it treat it the same as the HELO command. Of course, it might only get you past the first stumbling block, however if the rest of the smtp commands are compatible it might get you by:

You will probably find the smtpd.py file in /usr/lib/python2.7

def smtp_HELO(self, arg):
    if not arg:
        self.push('501 Syntax: HELO hostname')
        return
    if self.__greeting:
        self.push('503 Duplicate HELO/EHLO')
    else:
        self.__greeting = arg
        self.push('250 %s' % self.__fqdn)

#copy the above function and rename it smtp_EHLO

def smtp_EHLO(self, arg):
    if not arg:
        self.push('501 Syntax: HELO hostname')
        return
    if self.__greeting:
        self.push('503 Duplicate HELO/EHLO')
    else:
        self.__greeting = arg
        self.push('250 %s' % self.__fqdn)

Also, I note the python3.5 version of the same library looks like it supports EHLO, so maybe you could try and use python3. But apparently python3 is not backwards compatible it seems - so good luck.



标签: python smtp