mailing list archives

meli community discussions

⚠️ if something does not work as intended when interracting with the mailing lists,
reach out Github mirror Gitea repo @epilys:matrix.org

E-mail headers
From: comicpilsen <comicpilsen@gmail.com>
To: imap-protocol@u.washington.edu
Date: Fri, 08 Jun 2018 12:34:55 -0000
Message-ID: 7b8b6e00-3396-2c08-f58e-290d619214ea@gmail.com permalink / raw / eml / mbox
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 orNone,notbytes|

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)')ifrv !='OK':print("ERROR getting message",num)returnmsg
=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)|


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman13.u.washington.edu/pipermail/imap-protocol/attachments/20161228/83f781fd/attachment.html>
Reply
E-mail headers
From: blong@google.com
To: imap-protocol@localhost
Date: Fri, 08 Jun 2018 12:34:55 -0000
Message-ID: CABa8R6t-+S4R7V=4eK8_A+8HLFcUqHLF=ncsToz_J2fi_kK5NA@mail.gmail.com permalink / raw / eml / mbox
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>
Reply
E-mail headers
From: johnl-imap@iecc.com
To: imap-protocol@localhost
Date: Fri, 08 Jun 2018 12:34:55 -0000
Message-ID: 20161228161305.29711.qmail@ary.lan permalink / raw / eml / mbox
In article <7b8b6e00-3396-2c08-f58e-290d619214ea@gmail.com> you write:
>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?

IMAP is very much alive.  Approximately every smartphone in the world uses it to
handle their users' email accounts.

The reason you see so much more about POP3 is that POP is about 1/100 as complex
as IMAP, so if can do what you want, it's much easier to use.

>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.

I've found that the third party python imapclient library is a lot
easier to use than the standard imaplib.  It deals with many of the
datatype strangenesses you've been running into.

R's,
John
Reply