MBOX-Line: From blong at google.com  Wed Dec 28 08:02:07 2016
To: imap-protocol@u.washington.edu
From: Brandon Long <blong@google.com>
Date: Fri Jun  8 12:34:55 2018
Subject: [Imap-protocol] is IMAP still the protocol to use to read
	emails???
In-Reply-To: <7b8b6e00-3396-2c08-f58e-290d619214ea@gmail.com>
References: <7b8b6e00-3396-2c08-f58e-290d619214ea@gmail.com>
Message-ID: <CABa8R6t-+S4R7V=4eK8_A+8HLFcUqHLF=ncsToz_J2fi_kK5NA@mail.gmail.com>

this is a Python 3 thing, you need to call decode on the bytes to get a
string.

The IMAP spec would say that the bytes returned are ASCII, but we return
the raw message as is, which may include 8bit characters.  In general, the
8bit characters are mostly in utf8, but could be in latin1 or other
non-embedded null encodings.

So, the simplest is to assume utf8, the most correct would be to use some
code to guess the encoding.

strb = bodybytes.decode('utf-8')

On Dec 28, 2016 4:37 AM, "comicpilsen" <comicpilsen@gmail.com> wrote:

> I am a little confused. I code in Python ( 3.5.1) and I am "trying" to
> read the body of messages from a gmail account.  The issue is that I see a
> lot of POP3 activity on stack exchange and google but very little for IMAP.
> So is IMAP dead? If so what should I be looking at?
>
> All I want to do is read in my email body and do something with it. I
> really am having issues like the one below.
>
> Using python 3.5.1 and its throwing this trace
>
> TypeError: initial_value must be str or None, not bytes
>
> on the line
>
> text_msg = email.message_from_string(data[0][1])
>
> I know the text of the message is stashed in data[0][1] but can't seem to
> find the correct code to convert from bytes to string for the RFC822 in
> python 3.5.1 what is the line please? here is the code I found to read my
> IMAP box which works well for getting the messages, formatting the date and
> showing the subject.
>
> rv, data = M.fetch(num, '(RFC822)')
>     if rv != 'OK':
>         print("ERROR getting message", num)
>         return
>     msg = email.message_from_bytes(data[0][1])
>
>     text_msg = email.message_from_string(data[0][1])
>
>     hdr=mail.header.make_header(email.header.decode_header(msg['Subject']))
>     subject = str(hdr)
>     print('Message %s: %s' % (num, subject))
>
>     print("text = ",text_msg)
>
>
>
> _______________________________________________
> Imap-protocol mailing list
> Imap-protocol@u.washington.edu
> http://mailman13.u.washington.edu/mailman/listinfo/imap-protocol
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman13.u.washington.edu/pipermail/imap-protocol/attachments/20161228/faf35273/attachment.html>
