Python电子邮件 - 获取邮件的纯文本和HTML部分

时间:2017-08-06 22:51:59

标签: python email imap

我正在编写一个python电子邮件客户端,此时我有以下代码:

msg = email.message_from_bytes(server.fetch(id, ['BODY[1]'])[int(id)][b'BODY[1]'])

msg变量具有以下值:

Message: 
--001a114019b4123b7b0555b61a7c
Content-Type: text/plain; charset="UTF-8"

-- 
*Regards*
*Jack Smith (+374 98 99999)*

--001a114019b4123b7b0555b61a7c
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br clear=3D"all"><div><br></div>-- <br><div class=3D"gmai=
l_signature" data-smartmail=3D"gmail_signature"><div dir=3D"ltr"><span styl=
e=3D"font-size:small"><b><font face=3D"monospace, monospace">Regards</font>=
</b></span><div style=3D"font-size:small"><b><font face=3D"monospace, monos=
pace">Jack Smith (+374 98 99999)</font></b></div></div></div>
</div>

--001a114019b4123b7b0555b61a7c--

问题是,如何提取

字母的纯文本部分
-- 
    *Regards*
    *Jack Smith (+374 98 99999)*

...我如何提取HTML部分:

<div dir=3D"ltr"><br clear=3D"all"><div><br></div>-- <br><div class=3D"gmai=
    l_signature" data-smartmail=3D"gmail_signature"><div dir=3D"ltr"><span styl=
    e=3D"font-size:small"><b><font face=3D"monospace, monospace">Regards</font>=
    </b></span><div style=3D"font-size:small"><b><font face=3D"monospace, monos=
    pace">Jack Smith (+374 98 99999)</font></b></div></div></div>
    </div>

我尝试了以下内容:

for part in msg.walk():
     if part.get_content_type() == 'text/plain':
            return part.get_payload()

但由于某种原因,part.get_payload()具有与msg本身相同的值。 我在做什么呢?

0 个答案:

没有答案