Finally fixed my Python 2 to Python 3 e-mail problem

Chris Green cl at isbd.net
Sat Aug 29 13:02:33 EDT 2020


I've started a new thread because this relates to two or three threads
I started here over the past few days.

First, thank you everyone for all the help and suggestions.


I've finally fixed the problem, it was due to sys.stdin.read()
returning a string object in Python 3 as opposed to bytes in Python 2.

So, the code that reads an incoming message was:-

    msg = mailbox.mboxMessage(sys.stdin.read())

... and msg eventually gets passed into mailbox.mbox.add(msg)

This works fine in Python 2 but fails with a "can't encode" error in
Python 3 (deep down in the mailbox code).


The cause of the problem is that Python 3's sys.stdin.read() returns a
string, so changing the above line to:-

    msg = mailbox.mboxMessage(sys.stdin.buffer.read()) 

fixes the problem.


It took me quite a while to find the answer, mainly because I was
thinking the wrong way round, trying to make my message into a string,
whereas what I needed to do was keep it as a 'byte string' (I still
think there should be a better name for that!).

Again, thank you everyone for all the help, it did get me there in the
end!

-- 
Chris Green
·


More information about the Python-list mailing list