Mail extraction problem (something's wrong with split methods)

Jeff Epler jepler at unpythonic.net
Mon Sep 13 08:32:47 EDT 2004


You cannot send data with arbitrary-length lines over SMTP without using
an encoding such as quoted-printable or base64.

RFC2821
section 2.3.7 "Limits MAY be imposed on line lengths by servers"
section 4.5.3:
           The maximum total length of a text line including the <CRLF>
           is 1000 characters (not counting the leading dot duplicated
           for transparency).  This number may be increased by the use
           of SMTP Service Extensions.
(The relevant SMTP Service Extension in this case being RFC1652, 8BITMIME)

While this is not the limit you seem to be running into (you said
problems happened at 350 bytes) you should be aware of this (and lots of
other details about SMTP and other mail protocols) before you write
something to run "on top of" SMTP.

If you want to send a Python data structure across the network, you
might use pickle and then the email module to create a properly
MIME-encoded message:
    import pickle, email.Message
    bytes = pickle.dumps(my_structure)
    message = email.Message.Message()
    message.set_type("application/x-luka-milkovic")
    message.set_payload(bytes.encode("base64"))
    # set other headers as needed
    message = str(message)

If your structure is actually a sequence of fixed-width integers, then
you might be happy using struct.pack() instead of pickle:
    import struct
    l = len(my_structure)
    bytes = struct.pack("!" + "H"*l, *my_structure)
"H" is for numbers in range(65536), "!" uses network byte-order

In either case, you perform the reverse steps on the reassembled message
on the other end:
    import email.Parser
    decoded_message = email.Parser.Parser().parsestr(message)
    decoded_message.get_type() # must be application/x-luka-milkovic
                               # or this is not a message from your
                               # program
    bytes = decoded_message.get_payload().decode("base64")
    # now pickle.loads or struct.unpack the bytes

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20040913/431102f0/attachment.sig>


More information about the Python-list mailing list