Re: [Imap-protocol] is IMAP still the protocol to use to read emails???
Brandon Long <[email protected]> Wed, 28 Dec 2016 08:02:07 -0800
| Newsgroups | gmane.mail.imap.general |
|---|---|
| Message-ID | <CABa8R6t-+S4R7V=4eK8_A+8HLFcUqHLF=ncsToz_J2fi_kK5NA@mail.gmail.com> |
--===============3079639206704375771==
Content-Type: multipart/alternative; boundary=94eb2c08b3804725680544ba17dc
--94eb2c08b3804725680544ba17dc
Content-Type: text/plain; charset=UTF-8
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" <[email protected]> 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
> [email protected]
> http://mailman13.u.washington.edu/mailman/listinfo/imap-protocol
>
--94eb2c08b3804725680544ba17dc
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"auto">this is a Python 3 thing, you need to call decode on the =
bytes to get a string.<div dir=3D"auto"><br></div><div dir=3D"auto">The IMA=
P spec would say that the bytes returned are ASCII, but we return the raw m=
essage as is, which may include 8bit characters.=C2=A0 In general, the 8bit=
characters are mostly in utf8, but could be in latin1 or other non-embedde=
d null encodings.</div><div dir=3D"auto"><br></div><div dir=3D"auto">So, th=
e simplest is to assume utf8, the most correct would be to use some code to=
guess the encoding.</div><div dir=3D"auto"><br></div><div dir=3D"auto">str=
b =3D bodybytes.decode('utf-8')</div></div><div class=3D"gmail_extr=
a"><br><div class=3D"gmail_quote">On Dec 28, 2016 4:37 AM, "comicpilse=
n" <<a href=3D"mailto:[email protected]">[email protected]<=
/a>> wrote:<br type=3D"attribution"><blockquote class=3D"gmail_quote" st=
yle=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
=20
=20
=20
<div bgcolor=3D"#FFFFFF" text=3D"#000000">
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.=C2=A0 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?<br>
<br>
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. <br>
<br>
Using python 3.5.1 and its throwing this trace <br>
<pre class=3D"m_-8055269295658092791lang-py m_-8055269295658092791prett=
yprint m_-8055269295658092791prettyprinted"><code><span class=3D"m_-8055269=
295658092791typ">TypeError</span><span class=3D"m_-8055269295658092791pun">=
:</span><span class=3D"m_-8055269295658092791pln"> initial_value must be st=
r </span><span class=3D"m_-8055269295658092791kwd">or</span><span class=3D"=
m_-8055269295658092791pln"> </span><span class=3D"m_-8055269295658092791kwd=
">None</span><span class=3D"m_-8055269295658092791pun">,</span><span class=
=3D"m_-8055269295658092791pln"> </span><span class=3D"m_-805526929565809279=
1kwd">not</span><span class=3D"m_-8055269295658092791pln"> bytes</span></co=
de></pre>
<p>on the line</p>
<pre class=3D"m_-8055269295658092791lang-py m_-8055269295658092791prett=
yprint m_-8055269295658092791prettyprinted"><code><span class=3D"m_-8055269=
295658092791pln">text_msg </span><span class=3D"m_-8055269295658092791pun">=
=3D</span><span class=3D"m_-8055269295658092791pln"> email</span><span clas=
s=3D"m_-8055269295658092791pun">.</span><span class=3D"m_-80552692956580927=
91pln">message_from_string</span><span class=3D"m_-8055269295658092791pun">=
(</span><span class=3D"m_-8055269295658092791pln">data</span><span class=3D=
"m_-8055269295658092791pun"><wbr>[</span><span class=3D"m_-8055269295658092=
791lit">0</span><span class=3D"m_-8055269295658092791pun">][</span><span cl=
ass=3D"m_-8055269295658092791lit">1</span><span class=3D"m_-805526929565809=
2791pun">])</span></code></pre>
<p>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.</p=
>
<pre class=3D"m_-8055269295658092791lang-py m_-8055269295658092791prett=
yprint m_-8055269295658092791prettyprinted"><code><span class=3D"m_-8055269=
295658092791pln">rv</span><span class=3D"m_-8055269295658092791pun">,</span=
><span class=3D"m_-8055269295658092791pln"> data </span><span class=3D"m_-8=
055269295658092791pun">=3D</span><span class=3D"m_-8055269295658092791pln">=
M</span><span class=3D"m_-8055269295658092791pun">.</span><span class=3D"m=
_-8055269295658092791pln">fetch</span><span class=3D"m_-8055269295658092791=
pun">(</span><span class=3D"m_-8055269295658092791pln">num</span><span clas=
s=3D"m_-8055269295658092791pun">,</span><span class=3D"m_-80552692956580927=
91pln"> </span><span class=3D"m_-8055269295658092791str">'(RFC822)'=
</span><span class=3D"m_-8055269295658092791pun">)</span><span class=3D"m_-=
8055269295658092791pln">
</span><span class=3D"m_-8055269295658092791kwd">if</span><span class=
=3D"m_-8055269295658092791pln"> rv </span><span class=3D"m_-805526929565809=
2791pun">!=3D</span><span class=3D"m_-8055269295658092791pln"> </span><span=
class=3D"m_-8055269295658092791str">'OK'</span><span class=3D"m_-8=
055269295658092791pun">:</span><span class=3D"m_-8055269295658092791pln">
</span><span class=3D"m_-8055269295658092791kwd">print</span><span =
class=3D"m_-8055269295658092791pun">(</span><span class=3D"m_-8055269295658=
092791str">"ERROR getting message"</span><span class=3D"m_-805526=
9295658092791pun">,</span><span class=3D"m_-8055269295658092791pln"> num</s=
pan><span class=3D"m_-8055269295658092791pun">)</span><span class=3D"m_-805=
5269295658092791pln">
</span><span class=3D"m_-8055269295658092791kwd">return</span><span=
class=3D"m_-8055269295658092791pln">
msg </span><span class=3D"m_-8055269295658092791pun">=3D</span><span cl=
ass=3D"m_-8055269295658092791pln"> email</span><span class=3D"m_-8055269295=
658092791pun">.</span><span class=3D"m_-8055269295658092791pln">message_fro=
m_bytes</span><span class=3D"m_-8055269295658092791pun">(</span><span class=
=3D"m_-8055269295658092791pln">data</span><span class=3D"m_-805526929565809=
2791pun">[</span><span class=3D"m_-8055269295658092791lit"><wbr>0</span><sp=
an class=3D"m_-8055269295658092791pun">][</span><span class=3D"m_-805526929=
5658092791lit">1</span><span class=3D"m_-8055269295658092791pun">])</span><=
span class=3D"m_-8055269295658092791pln">
text_msg </span><span class=3D"m_-8055269295658092791pun">=3D</span><sp=
an class=3D"m_-8055269295658092791pln"> email</span><span class=3D"m_-80552=
69295658092791pun">.</span><span class=3D"m_-8055269295658092791pln">messag=
e_from_string</span><span class=3D"m_-8055269295658092791pun">(</span><span=
class=3D"m_-8055269295658092791pln">data</span><span class=3D"m_-805526929=
5658092791pun"><wbr>[</span><span class=3D"m_-8055269295658092791lit">0</sp=
an><span class=3D"m_-8055269295658092791pun">][</span><span class=3D"m_-805=
5269295658092791lit">1</span><span class=3D"m_-8055269295658092791pun">])</=
span><span class=3D"m_-8055269295658092791pln">
hdr</span><span class=3D"m_-8055269295658092791pun">=3D</span><span cla=
ss=3D"m_-8055269295658092791pln">mail</span><span class=3D"m_-8055269295658=
092791pun">.</span><span class=3D"m_-8055269295658092791pln">header</span><=
span class=3D"m_-8055269295658092791pun">.</span><span class=3D"m_-80552692=
95658092791pln">make_header</span><span class=3D"m_-8055269295658092791pun"=
>(</span><span class=3D"m_-8055269295658092791pln">em<wbr>ail</span><span c=
lass=3D"m_-8055269295658092791pun">.</span><span class=3D"m_-80552692956580=
92791pln">header</span><span class=3D"m_-8055269295658092791pun">.</span><s=
pan class=3D"m_-8055269295658092791pln">decode_header</span><span class=3D"=
m_-8055269295658092791pun">(</span><span class=3D"m_-8055269295658092791pln=
">msg</span><span class=3D"m_-8055269295658092791pun">[</span><span class=
=3D"m_-8055269295658092791str">'<wbr>Subject'</span><span class=3D"=
m_-8055269295658092791pun">]))</span><span class=3D"m_-8055269295658092791p=
ln">
subject </span><span class=3D"m_-8055269295658092791pun">=3D</span><spa=
n class=3D"m_-8055269295658092791pln"> str</span><span class=3D"m_-80552692=
95658092791pun">(</span><span class=3D"m_-8055269295658092791pln">hdr</span=
><span class=3D"m_-8055269295658092791pun">)</span><span class=3D"m_-805526=
9295658092791pln">
</span><span class=3D"m_-8055269295658092791kwd">print</span><span clas=
s=3D"m_-8055269295658092791pun">(</span><span class=3D"m_-80552692956580927=
91str">'Message %s: %s'</span><span class=3D"m_-8055269295658092791=
pln"> </span><span class=3D"m_-8055269295658092791pun">%</span><span class=
=3D"m_-8055269295658092791pln"> </span><span class=3D"m_-805526929565809279=
1pun">(</span><span class=3D"m_-8055269295658092791pln">num</span><span cla=
ss=3D"m_-8055269295658092791pun">,</span><span class=3D"m_-8055269295658092=
791pln"> subject</span><span class=3D"m_-8055269295658092791pun">))</span><=
span class=3D"m_-8055269295658092791pln">
</span><span class=3D"m_-8055269295658092791kwd">print</span><span clas=
s=3D"m_-8055269295658092791pun">(</span><span class=3D"m_-80552692956580927=
91str">"text =3D "</span><span class=3D"m_-8055269295658092791pun=
">,</span><span class=3D"m_-8055269295658092791pln">text_msg</span><span cl=
ass=3D"m_-8055269295658092791pun">)</span></code></pre>
<br>
</div>
<br>______________________________<wbr>_________________<br>
Imap-protocol mailing list<br>
<a href=3D"mailto:[email protected]">[email protected]=
n.edu</a><br>
<a href=3D"http://mailman13.u.washington.edu/mailman/listinfo/imap-protocol=
" rel=3D"noreferrer" target=3D"_blank">http://mailman13.u.washington.<wbr>e=
du/mailman/listinfo/imap-<wbr>protocol</a><br></blockquote></div></div>
--94eb2c08b3804725680544ba17dc--
--===============3079639206704375771==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
Imap-protocol mailing list
[email protected]
http://mailman13.u.washington.edu/mailman/listinfo/imap-protocol
--===============3079639206704375771==--