使用Python和imaplib处理编码消息

时间:2014-07-29 02:31:03

标签: python imaplib

在过去的几天里,我一直在编写一个脚本来解析自动生成的帮助台票证并将其内容存储到数据库中。在测试时,我遇到了几封似乎被编码并导致脚本失败的电子邮件。以下是RFC822之一的示例:

  

" [(b' 9255(RFC822 {12558}',b'已发送至:XXXXXXXXX \ r \ n已接收:通过10.220.77.132使用SMTP ID g4csp176213vck; \ r \ n星期一,2014年7月28日09:37:05 -0700(PDT)\ r \ nX-Received:10.67.30.130,SMTP id ke2mr39896936pad.44.1406565425185; \ r \ n星期一,2014年7月28日09:37:05 -0700 (PDT)\ r \ n返回路径:\ r \ n接收:来自XXXXXXXXX(XXXXXXXXX [74.125.149.112])\ r \ n由XXXXXXXXX与SMTP id yh3si18379315pab.170.2014.07.28.09.37.04 \ r \ n for; \ r \ n星期一,2014年7月28日09:37:04 -0700(PDT)\ r \ n已接收-SPF:无(XXXXXXXXX:XXXXXXXXX未指定允许的发件人主机)client-ip = 74.125.149.141; \ r \ n身份验证 - 结果:XXXXXXXXX; \ r \ n \ nspf =中性(XXXXXXXXX:XXXXXXXXX未指定允许的发件人主机)v \ r \ n已接收:来自XXXXXXXXX([74.125.149.141])XXXXXXXXX([74.125.148.10])带SMTP; \ r \ n \ tMon,2014年7月28日16:37:04 GMT \ r \ n接收:来自XXXXXXXXX([209.85.213.178])(使用TLSv1)XXXXXXXXX([74.125.148.12])和SXXXXXXXXX;星期一,28七月2014 09:37:04 PDT \ r \ n接收:由XXXXXXXXX与SMTP id uq10sf3897971igb.11 \ r \ n for;星期一,2014年7月28日09:37:03 -0700(PDT)\ r \ n-Google-DKIM-Signature:v = 1; A = RSA-SHA256; c =放松/放松; \ r \ n d = 1e100.net; s = 20130820; \ r \ nh = x-gm-message-state:mime-version:from:to:date:subject:message-id \ r \ n:x-original-sender:x-original-authentication-results :precedence \ r \ n:mailing-list:list-id:list-post:list-help:list-archive \ r \ n:list-unsubscribe:content-type:content-transfer-encoding; \ r \ n bh = H + FlcmWQAFURCHnDFK / bNHUOvofUAPB8bcDYlBceyxE =; \ r \ NB = LoR8D1MK8eoDG9DLkP9gkfR82 + EGUIEeOTLpqymqxyx9HJl0C9BW6iwPD7OgrJFbV4 \ r \ n xWYumML6RCinpcZc4d6VCDSw + akXLdhiol + lbWJBZWvgN4BQPgHJwCF6EaHYf3h8j4tq \ r \ N / KAZIkXowz4 / WKW8STri4BVjlA2a4LPwV / wazP + I9Kvr1yz433ymd + iCY1V0NexTI + CB \ r \ n 9m3IyL8sqB0 + Efyu + XQrR2y7ZdXDPwdzGS / WNHJBtKga5xPDtPga + 21pozVMCbuCc / CJ \ r \ n Cx9me6cVo19PrNKIOtSimDZ1u6ELdpVr4iprYQSaT8arYYiCPHJE34OFPlqspTxjm1eI \ r \ n ngyg == \ r \ NX-GM-消息州:ALoCoQkb908wRLWedDE + CtRzjD6VwC6Nja6duttyoVAdf + TFFn + uCxFB0Kwd5jk411YWdMD2G6HuFeRj2y3q7EzTe / vTvPLfymDIkHwZQa1r1zQ8I1B254t6v01ourR8InF / 41aPGnnD \ r \ NX-接收:由10.42。 48.74,SMTP id为r10mr260 49776icf.18.1406565423564; \ r \ n星期一,2014年7月28日09:37:03 -0700(PDT)\ r \ nX-Received:10.42.48.74,SMTP id r10mr26049775icf.18.1406565423537; \ r \ n星期一,2014年7月28日09:37:03 -0700(PDT)\ r \ nX-BeenThere:XXXXXXXXX \ r \ n接收:10.50.153.15与SMTP id vc15ls1961411igb.42.gmail;星期一,7月28日\ r \ n 2014 09:37:03 -0700(PDT)\ r \ nX-Received:10.66.254.37,SMTP id af5mr39703901pad.113.1406565423331; \ r \ n星期一,2014年7月28日09:37: 03 -0700(PDT)\ r \ n接收:来自XXXXXXXXX(XXXXXXXXX [74.125.149.158])\ r \ n由XXXXXXXXX和SMTP id da9si9190520pdb.425.2014.07.28.09.37.02 \ r \ n for; \ r \ n星期一,2014年7月28日09:37:03 -0700(PDT)\ r \ n接收-SPF:无(XXXXXXXXX:XXXXXXXXX未指定允许的发件人主机)client-ip = 207.211.31.47; \ r \ n接收:来自XXXXXXXXX([ 207.211.31.47])XXXXXXXXX([74.125.148.10])和SMTP; \ r \ n \ tMon,2014年7月28日16:37:02 GMT \ r \ n接收:来自XXXXXXXXX(XXXXXXXXX \ r \ n [129.135.112.43] ])(使用TLS)XXXXXXXXX;星期一,7月28日\ r \ n 2014 12:37:01 -0400 \ r \ n接收:来自XXXXXXXXX(129.135.128.210)的XXXXXXXXX \ r \ n(129.135.112.45),带有Microsoft SMTP Server id 14.3.181.6;星期一,2014年7月28日\ r \ n 11:36:58 -0500 \ r \ n接收:来自ITSDC50([127.0.0.1])XXXXXXXXX与Microsoft \ r \ n SMTPSVC(6.0.3790.4675); \ t星期一,28 2014年7月11日36:58 -0500 \ r \ nMIME-Version:1.0 \ r \ nFrom:\ r \ nTo:\ r \ n日期:星期一,2014年7月28日11:36:58 -0500 \ r \ n主题:调度IT / Cares案例:SC-118656-7031 \ r \ n消息编号:\ r \ nX-OriginalArrival时间:2014年7月28日16:36:58.0498(UTC)FILETIME = [26792E20:01CFAA82] \ r \ nX-MC-Unique :114072812370105901 \ r \ nX-pstn-levels:(S:85.19264 / 99.90000 CV:99.9000 FC:95.5390 LC:95.5390 R:95.9108 P:95.9108 M:97.0282 C:98.6951)\ r \ nX-pstn-dkim:0跳过:未启用\ r \ n-nX-pstn-settings:1(0.1500:0.1500)cv gt6 gt5 gt4 gt3 gt2 gt1 \ r \ n \ n -n-pstn-addresses:from [1094/49] \ r \ n-nX-pstn-nxpr: DISP =中性envrcpt = XXXXXXXXX \ r \ NX-PSTN-NXP:bodyHash = 9500f76054cf97c2a0eec20f8940768958faf6c3,headerHash = eb9362a172738328a8b8a8ae406c42a63f5545f9,的keyName = 4,rcptHash = e0dd4695780dcb1818e78b482447ac976870bcbe,SOURCEIP = 207.211.31.47,版本= 1 \ r \ NX-原始-发信人: XXXXXXXXX \ r \ NX-原创 - 身份验证 - 结果:XXXXXXXXX; spf = neutral \ r \ n(XXXXXXXXX:XXXXXXXXX不指定允许的发件人\ r \ n主机)smtp.mail = XXXXXXXXX \ r \ nPrecedence:list \ r \ nMailing-list:list XXXXXXXXX contact XXXXXXXXX \ r \ nList-ID :\ r \ nX-Google-Group-Id:511158325204 \ r \ nList-Post:,\ r \ nList-Help :, \ r \ nList-Archive:\ r \ nList-Unsubscribe:,\ r \ n \ r \ n \ nContent-Type:text / plain; charset = UTF-8 \ r \ nContent-Transfer-Encoding:base64 \ r \ nX-pstn-neptune:0/0 / 0.00 / 0 \ r \ n \ n -x-pstn-levels:(S:65.87536 / 99.90000 CV:99.9000 FC :95.5390 LC:95.5390 R:95.9108 P:95.9108 M:97.0282 C:98.6951)\ r \ nX-pstn-dkim:0跳过:未启用\ r \ nX-pstn-settings:5(2.0000:0.0200)s cv fc lc gt6 gt5 gt4 GT3 gt2 gt1 ft lt rpmc \ r \ nX-pstn-addresses:from [db-null] \ r \ n-nX-pstn-nxpr:disp = neutral,envrcpt = XXXXXXXXX \ r \ nX-pstn-nxp :bodyHash = 45f4f2e59005199791055b3d1f937e1d3fb7d7ca,headerHash = ca981838d5783da04d9d38e3fffc3f5907100fcf,的keyName = 4,rcptHash = 4f3dee680a09495dc5b095849a4225f49c4a45f4,SOURCEIP = 74.125.149.141,版本= 1 \ r \ n \ r \ nQ2FzZSBOdW1iZXI6ICAgICAgICAgU0MtMTE4NjU2LTcwMzENClNldmVyaXR5IExldmVsOiAg \ r \ nICAgIE5vcm1hbA0KQWNjb3VudCBOYW1lOiAgICAgICAgSENSIE1hbm9yY2FyZQ0KU2l0ZSBO \ r \ nYW1lOiAgICAgICAgICAgMzAxDQpDbGllbnQgTmFtZTogICAgICAgICBBbWFuZGEgUGVucm9k \ r \ nDQpDbGllbnQgUGhvbmU6ICAgICAgICANCkNsaWVudCBNYWlsUGF0aDogICAgIGFtYW5kYS5w \ r \ nZW5yb2RAaGNyLW1hbm9yY2FyZS5jb20NCkNhc2UgUHJvZHVjdDogICAgICAgIE hDUi1GaWV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LiA1MTk4DQoNCk9OTFkgSUYgWU9VIEFSRSBVTkFCTEUgVE8gUkVBQ0ggSU5HUiBjb250 \ r \ nYWN0LCBhZnRlciBsZWF​​2aW5nIG1lc3NhZ2VzIGZvciBlYWNoIHBlcnNvbiBsaXN0ZWQgYWJv \ r \ ndmUgYW5kIHdhaXRpbmcgNSBtaW51dGVzIGZvciBhIHJldHVybiBjYWxsLCBjb250YWN0Og0K \ r \ nVG9ueSBCdXRsZXIgYXQgODAwLTYwMy01NTAwIGV4dC4gNTE0MA0KQmFyYiBFZHdhcmRzIGF0 \ r \ nIDgwMC00MjctMTkwMiBleHQuIDUxODMNCkRhdmUgSGlyZSBhdCA4MDAtNDI3LTE5MDIgZXh0 \ r \ nLiA2NDE4DQoNCkFTRCBBZnRlciBIb3VycyBudW1iZXIgaXMgODI4LTYyNC0xMDk5IGFuZCBl \ r \ nbWFpbCBmb3IgdGhpcyBhY2NvdW50IGlzIHRlYW1pbnRlcmdyYXBoQGFzZC11c2EuY29tDQoN \ r \ nCjwwNy8yOC8xNCAxMTozNiBDYXNlIG9wZW5lZCBieTogIHJtYXJ0aW4gKDI1Nik3MzAtNTEx \ r \ nMT4NCg0KDQpQcm9ibGVtIFNvbHV0aW9uDQoqKioqKioqKioqKioqKioqKioqKioqKioqKioq \ r \ nKioqKioqKioqKioqKioqKioqKioqKioqKioqK ioqKg0KUmVmZXJyZWQgZm9yIFJlc29sdXRp \ r \ nb24gVG86IEFTRA0KPDA3LzI4LzE0IDExOjM2IENhc2UgZWRpdGVkIGJ5OiBybWFydGluICgy \ r \ nNTYpNzMwLTUxMTE + DQoqfip + Kn4qfip + Kn4qfip + Kn4qfip + Kn4qfip + Kn4qfip + Kn4qfip + \ r \ nKn4qfip + Kn4qfip + Kg0KDQoNCg0KDQpfX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f \ r \ nX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f \ r \ nX19fX19fX19fX19fX19fX18NCk5vdGljZSByZXF1aXJlZCBieSBsYXc6ICBUaGlzIGVtYWls \ r \ nIG1heSBjb25zdGl0dXRlIGFuIGFkdmVydGlzZW1lbnQgb3Igc29saWNpdGF0aW9uIHVuZGVy \ r \ nIFUuUy4gbGF3IGlmIGl0cyBwcmltYXJ5IHB1cnBvc2UgaXMgdG8gYWR2ZXJ0aXNlIG9yIHBy \ r \ nb21vdGUgYSBjb21tZXJjaWFsIHByb2R1Y3Qgb3Igc2VydmljZS4gIFlvdSBtYXkgY2hvb3Nl \ r \ nIG5vdCB0byByZWNlaXZlIGFkdmVydGlzaW5nIGFuZCBwcm9tb3Rpb25hbCBtZXNzYWdlcyBm \ r \ ncm9tIEFTRCAoZXhjZXB0IGZvciB3d3cuYXNkLXVzYS5jb20sIHdoaWNoIG1hbmFnZXMgZW1h \ r \ naWwgcHJlZmVyZW5jZXMgdGhyb3VnaCBhIHNlcGFyYXRlIHByb2Nlc3MpIGF0IHRoaXMgZW1h \ r \ naWwgYWRkcmVzcyBieSBmb3J3YXJkaW5nIHRoaXMgbWVzc2FnZSB0byBsZWF​​2ZW1lYWxvbmVA \ r \ nYXNkLXVzYS5jb20uICBJZiB5b3UgZG8gc28sIHRoZSBzZW5kZ XIgb2YgdGhpcyBlbWFpbCB3 \ r \ naWxsIGJlIG5vdGlmaWVkIHByb21wdGx5IGFuZCB5b3Ugd2lsbCBub3QgYmUgY29udGFjdGVk \ r \ nIGFnYWluLiAgT3VyIHByaW5jaXBhbCBwb3N0YWwgYWRkcmVzcyBpcyA3NzUgR29kZGFyZCBD \ r \ nb3VydCBBbHBoYXJldHRhLCBHQSAgMzAwMDUuDQoNCg == \ r \ n')中,b')']"

电子邮件的正文是否已编码?如果是的话,我应该如何处理解码呢?

4 个答案:

答案 0 :(得分:3)

您可以使用email包。你有一个列表,列表的第一项是一个元组,其中第二个元素是整个电子邮件消息。假设你在名为msg_bytes的变量中有那个bytes对象。然后,您可以使用以下方法解析消息:

import email.parser
msg = email.parser.BytesParser().parsebytes(msg_bytes)

然后,您可以访问消息的不同部分(请参阅email.message.Message处的文档):

# get a bytes object containing the base64-decoded message
textbytes = msg.get_payload(decode=True)

# get the content charset
content_charset = msg.get_content_charset()

# decode the text to obtain a string object
text = textbytes.decode(content_charset)

这将能够处理大多数(如果不是全部)有效的电子邮件。

答案 1 :(得分:1)

正文编码(Content-Transfer-Encoding:base64)与加密不同。将第一组字符粘贴到在线解码器

Q2FzZSBOdW1iZXI6ICAgICAgICAgU0MtMTE4NjU2LTcwMzENClNldmVyaXR5IExldmVsOiAg

被解码为

Case Number:         SC-118656-7031
Severity Level:  

Python有解码base64的库,但如果imaplib没有内置来简化这一点,我会很失望。

答案 2 :(得分:1)

尝试Imbox,在这里您无需修复编解码器

因为imaplib是一个非常过多的低级库,并且返回难以使用的结果

安装

pip install imbox

用法

from imbox import Imbox

with Imbox('imap.gmail.com',
        username='username',
        password='password',
        ssl=True,
        ssl_context=None,
        starttls=False) as imbox:

    all_inbox_messages = imbox.messages()
    for uid, message in all_inbox_messages:
        message.sent_from
        message.sent_to
        message.body

答案 3 :(得分:0)

要获取由Imaplib生成的base64解码电子邮件,可以使用base64模块轻松对其进行解码,并且应包括以下步骤。

# import python base64 and re modules
import base64
import re

# Save the encoded part in a variable, e.g re.search grabs the encoded part after string "version=1\r\n\r\n" and saves to the variable named 'decoded'.
decoded = re.search('version=1\r\n\r\n(.*)', email, re.DOTALL)

# prints the decoded message
print(base64.b64decode(decoded.group(1)))

如果要获取两个字符串之间的编码部分,则搜索表达式为

decoded = re.search('string1(.*)string2', email)

其中“ email”变量包含Imaplib生成的整个电子邮件内容。