Python阅读电子邮件信息

时间:2018-11-27 18:43:44

标签: python email

我正在尝试阅读上一封收到的电子邮件中的信息:

    mail = imaplib.IMAP4_SSL('imap.gmail.com')
    mail.login('test1@gmail.com', 'ABC#')
    mail.list()
    mail.select("inbox")

    result, data = mail.search(None, "ALL")
    ids = data[0]  # data is a list.
    id_list = ids.split()  # ids is a space separated string
    latest_email_id = id_list[-1]  # get the latest

    #print ("latest_email_id= ", latest_email_id)
    result, data = mail.fetch(latest_email_id, "(RFC822)")
    raw_email = data[0][1]

“ raw_email”的打印语句是这样的(请注意,出于安全原因,我更改了信息:

b'Delivered-To: test1@gmail.com\r\nReceived: by 2002:ac8:4593:0:0:0:0:0 with SMTP id l1234;\r\n        Tue, 27 Nov 2018 09:16:41 -0800 (PST)\r\nX-Received: by 2002:a5d:4ec4:: with SMTP id 111.1.1111;\r\n        Tue, 27 Nov 2018 09:16:40 -0800 (PST)\r\nARC-Seal: i=1; a=rsa-sha256; t=1543339000; cv=none;\r\n        d=google.com; s=arc-20160816;\r\n        b=AYhrN2mVh6Ho33wOVqGReMIqrFd7+UuozI633AT/FE59WUVIlg539V2+SYxGl1YXp7\r\n         q7n/17RQdSeBtv4FghG6T8C4FyXvrqnsoiXjtVH48MrJ0g+fqVdRQCINaLG6LP4Z/XAU\r\n         bjDhcg1bV9gqdOtOws5FMjnRtruWo390/jqqHaCOlJNy1++Yw2rHY894Bh619p6m2GL5\r\n         Yj4nLaDrB45M2O/CqLGpD0EJtYEkdUO1WClqf7qU35bDV0l8ALm4JVUM0P1aNeiFK5RI\r\n         sISOWOz3PDDt461Zm24TYrFBX4MaA5fZQoW2f2AFoYk8efhMpIr2idtvPjmGWJ1Vn6zo\r\n         V2uw==\r\nARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;\r\n        h=to:subject:message-id:date:from:mime-version:dkim-signature;\r\n        bh=DfCkM5yA0BCKRwEW2DYkDiGYlT4tuDfEz10cbS7lscs=;\r\n        b=iFgtdMnLGQ4oMk3s4MnCIFpZ+7phiGbhbyGs6FheIXNYtFt6pB59CRNH2llvJ+f3bS\r\n         6DS/wqelDIStDybG4DLPvAi6d+gmcFAjlvRhVgmFphx83vdH8PGL/0e50JbHUFUszTqe\r\n         aDQKGvB38dK+GpFk2lE0Ibw6NBOZlmN//xSmSGXl4i0tc5SybecqcVKFa4CFpP+o69Bh\r\n         nScpBZHn4kCo3O0H8aDtFi5ThVbOBczIXNwgjCadF09N1jlrzt/je8IIogyuvrJ/jUBv\r\n         NLJkWk2isCq8f2Q0m6NghqaNVdtlh0cG34i7oU39sjnYxEZ1E6l/zwCI2gvPOA7RzvNT\r\n         CbnQ==\r\nARC-Authentication-Results: i=1; mx.google.com;\r\n       dkim=pass header.i=@gmail.com header.s=20161025 header.b=gxAxSqva;\r\n       spf=pass (google.com: domain of pingY@gmail.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=Y@gmail.com;\r\n       dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com\r\nReturn-Path: <pingY@gmail.com>\r\nReceived: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41])\r\n        by mx.google.com with SMTPS id FAKE.\r\n        for <zXtest@gmail.com>\r\n        (Google Transport Security);\r\n        Tue, 27 Nov 2018 09:16:40 -0800 (PST)\r\nReceived-SPF: pass (google.com: domain of pingY@gmail.com designates 209.85.220.41 as permitted sender) client-ip=111.8115.111.11;\r\nAuthentication-Results: mx.google.com;\r\n       dkim=pass header.i=@gmail.com header.s=20161025 header.b=gxAxSqva;\r\n       spf=pass (google.com: domain of pingY@gmail.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=Y@gmail.com;\r\n       dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com\r\nDKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r\n        d=gmail.com; s=20161025;\r\n        h=mime-version:from:date:message-id:subject:to;\r\n        bh=DfCkM5yA0BCKRwEW2DYkDiGYlT4tuDfEz10cbS7lscs=;\r\n        b=gxAxSqvaKL/tnvzcAYWgKd7XNimR8vdxz7ashDknF2+nZLSQHYSHaIjdOzSX7dZFCw\r\n         yDl+9jo6P7RTupihLR/6xve7iJf3bbt98Q6BjDTNhOTE9n5cK/WTrrVh+vvu36P0CPEe\r\n         H+q2BYzltAX6hlXog3UaV9UZZ4wTzZUHICJYoOPCkwIwJz3C00HLA3xLbqvxTESisauh\r\n         Blah\r\n         +Bah+iCaGPLWIL9XIhT30BQaNEPLX0vxIh4pYhjrlT8\r\n         lPYA==\r\nX-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r\n        d=1e100.net; s=20161025;\r\n        h=x-gm-message-state:mime-version:from:date:message-id:subject:to;\r\n        bh=DfCkM5yA0BCKRwEW2DYkDiGYlT4tuDfEz10cbS7lscs=;\r\n        b=ACzy4UErTUjbqaUkxPA1yAc87d9gpnlsNKuKE/IHAfL6BY/zOyWZe1nNdTdpBKUPbf\r\n         1PK2L05DAfD+JZRC7xxhOrGhfKU6vES9jk50V/R0sJGgIWTz7Mp6JJIDR3ubMg/leASO\r\n         BBl5wsrePqO/hqNdTv6/GwnjEJrMRmlYG7CF/Yl5f5Xo8FdGnr65T7M6RjCBqfSvMmma\r\n         T5v9HcWz8jwpmdoHsD5cUf/hnpC+xIhzX0RlKb4U/TjHP6s7UJ0JrQrH6oDUQOdqniH1\r\n         1owpsiqS6lxU44jGkl1PbiSiryiRfkb6cMIf6aOwT30XFY4kZsGlOO5QN8i1/UbXFNl8\r\n         sM1w==\r\nX-Gm-Message-State: AA+aEWYeL+LxPlxaP3qr+R6Um7j/o66pbmbLcLI9rY/mB9OgwrdILrhZ\r\n\tabpjiMOwJyy4ihBPviIGHxV2JHXO/tGet/97T31uSyGRKMc=\r\nX-Google-Smtp-Source: ABC/BLANK/BLANK=\r\nX-Received: by 2002:adf:c44a:: with SMTP id FAKE.1.FAKE;\r\n Tue, 27 Nov 2018 09:16:40 -0800 (PST)\r\nMIME-Version: 1.0\r\nFrom: Y <pingY@gmail.com>\r\nDate: Wed, 28 Nov 2018 04:16:28 +1100\r\nMessage-ID: <CA+KiAqOmG37ysA-+JPgMopYpn41A12oGbNTXzp9dQDAmS5-tVQ@mail.gmail.com>\r\nSubject: Welcome to the X Portal\r\nTo: zXtest@gmail.com\r\nContent-Type: multipart/alternative; boundary="000000000000dd770b057ba89c59"\r\n\r\n--000000000000dd770b057ba89c59\r\nContent-Type: text/plain; charset="UTF-8"\r\n\r\nHi test1\r\nWelcome to the X Portal this si 2nd email\r\nY\r\n\r\n--000000000000dd770b057ba89c59\r\nContent-Type: text/html; charset="UTF-8"\r\n\r\n<div dir="ltr"><div dir="ltr"><div>Hi Y</div><div>Welcome to X this si 2nd email</div><div>Y<br></div></div></div>\r\n\r\n--000000000000dd770b057ba89c59--\r\n'

我已经阅读了有关阅读电子邮件信息的一些stackoverflow。即python imaplib to get gmail inbox subjects titles and sender name

他们大多认为返回的数据是元组,并且对此有些怀疑

   for response_part in data:
      if isinstance(response_part, tuple):
          msg = email.message_from_string(response_part[1])
          varSubject = msg['subject']
          varFrom = msg['from']

如何检索主题和收件人等信息。
自从我尝试过

以来,它似乎不是字典。
dTemp = {}
dTemp = raw_email
dTemp["Subject"]

最后一条语句给我一个错误

1 个答案:

答案 0 :(得分:1)

要解析电子邮件,最好使用内置的email.parser模块。

如果您将raw_email设为bytes,则可以使用email.parser.BytesParser()对其进行解析:

>>> parser = email.parser.BytesParser()
>>> parsed_message = parser.parsebytes(email_body, headersonly=True)  # or False...

然后请参阅email.message.Message上的文档,以从消息中访问数据,但通常它是这样工作的:

>>> parsed_message["Subject"]
'Welcome to the X Portal'
>>> parsed_message["From"]
'Y Ferdowsian <pingY@gmail.com>'
>>> parsed_message["To"]
'zXtest@gmail.com'