PYTHON如何自动处理邮件(三)——python获取邮件体

list会返回一个邮件列表,是一个有三个元素的列表。其中第二个元素是所有邮件的序号。
比如说:你邮箱里有10个邮件,那么列表就是1-10。
TIP:如果你是老程序员、老C或者老python,列表第一位是0,而很不幸,email的第一序号是1。
你又收到一封邮件,那么序号就是11。

——————————————————————————
你可以将序号存到一个文件里,然后定时轮询,当这个数字发生变化的时候,就代表着有一封新邮件。
用OS、open、time函数,这个我就不说了。逻辑太过简单。

获得邮件的命令是retr(i),其中i是邮件序号,就会返回一个列表,其中第二个就是邮件体。

你可以直接用print()看一下邮件源码。

这是一个邮件的源码。bytes64编码。

Received: from Jtjnmail201614.home.langchao.com (10.100.2.14) by
 Jtjnmail201618.home.langchao.com (10.100.2.18) with Microsoft SMTP Server
 (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2044.4
 via Mailbox Transport; Wed, 3 Feb 2021 11:06:40 +0800
Received: from jtjnmail201609.home.langchao.com (10.100.2.9) by
 Jtjnmail201614.home.langchao.com (10.100.2.14) with Microsoft SMTP Server
 (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id
 15.1.2044.4; Wed, 3 Feb 2021 11:06:40 +0800
Received: from mailgate.inspur.com (172.30.26.49) by
 jtjnmail201609.home.langchao.com (10.100.2.9) with Microsoft SMTP Server id
 15.1.2044.4; Wed, 3 Feb 2021 11:06:40 +0800
Received: from globalnet19.corpsmtp.net (unknown [222.73.239.19])
	(using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by Forcepoint Email with ESMTPS id AC218AD871CBD317F703
	for <[email protected]>; Wed,  3 Feb 2021 11:02:48 +0800 (CST)
Received: from cmccmta1.chinamobile.com ([221.176.66.79])
        by globalnet19.corpsmtp.net ([EK]) with ESMTP id EK202102031106369519
        for <[email protected]>; Wed, 03 Feb 2021 11:06:36 +0800
Received: from spf.mail.chinamobile.com (unknown[172.16.121.13]) by rmmx-syy-dmz-app04-12004 (RichMail) with SMTP id 2ee4601a133b96d-a4cd0; Wed, 03 Feb 2021 11:06:35 +0800 (CST)
X-RM-TRANSID: 2ee4601a133b96d-a4cd0
X-RM-TagInfo: emlType=0                                       
X-RM-SPAM-FLAG: 00000000
Received: from DY-WL-JIANKONG3 (unknown[10.18.75.220])
	by rmsmtp-syy-appsvr07-12007 (RichMail) with SMTP id 2ee7601a133ac25-517c6;
	Wed, 03 Feb 2021 11:06:35 +0800 (CST)
X-RM-TRANSID: 2ee7601a133ac25-517c6
Sender: <[email protected]>
x-PcFlag: e4277e7e-5c62-4809-92b0-70e499004780_23_12367
X-Mailer: PC_RICHMAIL 2.8.2
Date: Wed, 3 Feb 2021 11:06:39 +0800
From: dyjkz <[email protected]>
To: sdmcceoms <[email protected]>
CC:
Subject: =?utf-8?B?5Lic6JCl5ZGK6K2m5rWB5rC05Y+35ZCK5q2777yM54Om6K+35Y2P5Yqp5aSE55CG77yM6LCi6LCi77yB?=
Message-ID: [email protected]>
Content-Type: multipart/alternative;
	boundary="----=_001_NextPart-1498277105_=----"
TSender: [email protected]
tRcpt: <[email protected]>;
tUid: G20210203110636551ac1ce4fcfb28c9c61f785938e74da
Tcorpfrom: [email protected]
tIPaddress: 221.176.66.79
Return-Path: [email protected]
X-MS-Exchange-Organization-Network-Message-Id: dbd21430-a9c2-4bce-33e9-08d8c7f0ba3a
X-MS-Exchange-Organization-AVStamp-Enterprise: 1.0
X-MS-Exchange-Organization-AuthSource: jtjnmail201609.home.langchao.com
X-MS-Exchange-Organization-AuthAs: Anonymous
X-MS-Exchange-Transport-EndToEndLatency: 00:00:00.3962161
X-MS-Exchange-Processed-By-BccFoldering: 15.01.2044.007
MIME-Version: 1.0

------=_001_NextPart-1498277105_=----
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64

DQ0NDQ0NDea1qua9ruaCqOWlve+8jOS4nOiQpeWRiuitpua1geawtOWPtzogICAxMTI4NjA1Nzkx
ICDvvIzlkIrmrbvvvIzng6bor7fljY/liqnlpITnkIbvvIzosKLosKLvvIENDQ0NDQ0N

------=_001_NextPart-1498277105_=----
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: quoted-printable

<html><head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf-8"><s=
tyle>div {
    
    font-size: 12pt; color: #000000; font-family:=E5=BE=AE=E8=BD=AF=
=E9=9B=85=E9=BB=91;}</style></head><body style=3D"word-wrap: break-word; -w=
ebkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div style=
=3D"word-wrap: break-word;"><div style=3D"color: rgb(0, 0, 0); font-family:=
 =E5=BE=AE=E8=BD=AF=E9=9B=85=E9=BB=91; font-size: 12pt; word-wrap: break-wo=
rd;"><br></div><div style=3D"word-wrap: break-word;"><span style=3D"font-si=
ze: 12pt; line-height: 24px;">=E6=B5=AA=E6=BD=AE=E6=82=A8=E5=A5=BD=EF=BC=8C=
=E4=B8=9C=E8=90=A5=E5=91=8A=E8=AD=A6=E6=B5=81=E6=B0=B4=E5=8F=B7:</span><spa=
n style=3D"font-size: 12pt; line-height: 24px;">&nbsp; &nbsp;</span>1128605=
791 &nbsp;<span style=3D"font-size: 12pt;">=EF=BC=8C</span><span style=3D"f=
ont-size: 12pt; line-height: 24px;">=E5=90=8A=E6=AD=BB=EF=BC=8C=E7=83=A6=E8=
=AF=B7=E5=8D=8F=E5=8A=A9=E5=A4=84=E7=90=86=EF=BC=8C=E8=B0=A2=E8=B0=A2=EF=BC=
=81</span></div><div style=3D"color: rgb(0, 0, 0); font-family: =E5=BE=AE=
=E8=BD=AF=E9=9B=85=E9=BB=91; font-size: 12pt; word-wrap: break-word;"><br><=
/div></div><div id=3D"SIGNNAME12367"></div><div><span id=3D"_THINKMAILNAME1=
2367" font-size:12pt;font-family:microsoftyahei;=3D""></span></div></body><=
/html>

------=_001_NextPart-1498277105_=------

成功完成一半了。

邮件包括邮件头、邮件体。邮件头包括了发信人信息、收件人信息、发信时间、抄送等,

Date: Wed, 3 Feb 2021 11:06:39 +0800
From: dyjkz <[email protected]>
To: sdmcceoms <[email protected]>
CC:
Subject: =?utf-8?B?5Lic6JCl5ZGK6K2m5rWB5rC05Y+35ZCK5q2777yM54Om6K+35Y2P5Yqp5aSE55CG77yM6LCi6LCi77yB?=
Message-ID: [email protected]>
Content-Type: multipart/alternative;

相当于
在这里插入图片描述然后是邮件体。
邮件体包括了文件、文本等信息,如果只想获得文本信息,可以直接找
text/html和text/plain

比如说这一封邮件的文本就是

Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64

DQ0NDQ0NDea1qua9ruaCqOWlve+8jOS4nOiQpeWRiuitpua1geawtOWPtzogICAxMTI4NjA1Nzkx
ICDvvIzlkIrmrbvvvIzng6bor7fljY/liqnlpITnkIbvvIzosKLosKLvvIENDQ0NDQ0N

因为是base64,但是charset是utf-8.
所以很简单:获取charset,然后转码成为对应文本,就可成功读取邮件文本信息。

猜你喜欢

转载自blog.csdn.net/weixin_45642669/article/details/113590632
今日推荐