Erle Robotics Python Networking Gitbook Free

Getting Information from EHLO

Sometimes it is nice to know about what kind of messages a remote SMTP server will accept. For instance, most SMTP servers have a limit on what size message they permit, and if you fail to check first, then you may transmit a very large message only to have it rejected when you have completed transmission.

Some servers do not support ESMTP. On those servers, EHLO will just return an error. In that case, you must send a HELO command instead. In the previous examples, we used sendmail() immediately after creating our SMTP object, so smtplib had to send its own “hello” message to the server. But if it sees you attempt to send the EHLO or HELO command on your own, then sendmail() will no longer attempt to send these commands itself. shows a program that gets the maximum size from the server, and returns an error before sending if a message would be too large.

import sys, smtplib, socket

if len(sys.argv) < 4:
    print "usage: %s server fromaddr toaddr [toaddr...]" % sys.argv[0]

server, fromaddr, toaddrs = sys.argv[1], sys.argv[2], sys.argv[3:]

message = """To: %s
From: %s
Subject: Test Message from


This is a test message sent to you from the program.
""" % (', '.join(toaddrs), fromaddr)

    s = smtplib.SMTP(server)
    code = s.ehlo()[0]
    uses_esmtp = (200 <= code <= 299)
    if not uses_esmtp:
        code = s.helo()[0]
        if not (200 <= code <= 299):
            print "Remote server refused HELO; code:", code

    if uses_esmtp and s.has_extn('size'):
        print "Maximum message size is", s.esmtp_features['size']
        if len(message) > int(s.esmtp_features['size']):
            print "Message too large; aborting."

    s.sendmail(fromaddr, toaddrs, message)

except (socket.gaierror, socket.error, socket.herror,
        smtplib.SMTPException), e:
    print " *** Your message may not have been sent!"
    print e
    print "Message successfully sent to %d recipient(s)" % len(toaddrs)

If you run this program, and the remote server provides its maximum message size, then the program will display the size on your screen and verify that its message does not exceed that size before sending. Here is what running this program might look like:

[email protected]:~/Python_files# python localhost [email protected] [email protected] Maximum message size is 10240000
Message successfully sent to 1 recipient(s)

Take a look at the part of the code that verifies the result from a call to ehlo() or helo(). Those two functions return a list; the first item in the list is a numeric result code from the remote SMTP server.