1. 程式人生 > 其它 >PYTHON如何自動處理郵件(三)——python獲取郵件體

PYTHON如何自動處理郵件(三)——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 <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,然後轉碼成為對應文字,就可成功讀取郵件文字資訊。