Python获取电子邮件标头组件以获取UID列表

时间:2019-02-19 02:02:53

标签: python imap

我正在使用python IMAP4,并且能够登录并获取具有给定主题的那些电子邮件的UID列表,我正在尝试获取给定UID列表(主题,从和到)的电子邮件标头组件: / p>

for i in range(0, len(self.myUIDList)):
    print ("self.myUIDList[i]= ", self.myUIDList[i])
    typ, msg_data = self.mail.fetch(str(self.myUIDList[i]), '(RFC822)')
    for response_part in msg_data:
        if isinstance(response_part, tuple):
            print (" response_part= ", response_part)
            msg = email.message_from_string(str(response_part[1]))
            print ("msg = ", msg)
            for header in ['subject', 'to', 'from']:
                print ('%-8s: %s' % (header.upper(), msg[header]))

来自stdOut消息是->

msg =  b'Delivered-To: xyz@gmail.com\r\nReceived: by 2002:a92:1f92:0:0:0:0:0 with SMTP id f18csp46699ilf;\r\n        Mon, 18 Feb 2019 17:15:55 -0800 (PST)\r\nX-Received: by 2002:adf:ee8e:: with SMTP id b14mr17952361wro.71.1550538955329;\r\n        Mon, 18 Feb 2019 17:15:55 -0800 (PST)\r\nARC-Seal: i=1; a=rsa-sha256; t=1550538955; cv=none;\r\n        d=google.com; s=arc-20160816;\r\n        b=Ig4GJteKrrxXSCgtKMl1/RusHOur5JIwXkHbjVxlLOCikOiWRf9gj7hKNFjHPI9KeU\r\n         0bWxxQP7MQ6PAwkkyoeRjmS2zzWKrJDWCW5gfFGK+WKmkHZdEiVXQdGY/DauBnp9xKkd\r\n         90L+V9sWODJTbc/CNs6n0J5vSt048NmQZ0h/N50b8qqzVDkAawk1e96ZQgRKh374RpBe\r\n         ONz9fWX+6JmKH0LIK/74A5ToYqPqoXkzZb2kDLKpA4uE4M21yyFOfElUK/KtHEUty96A\r\n         QEK3P4jMo1EsPC/EJJiLI3QX6UrM5pGDp6VYfOGRyEA/FEa92HJmpCtjbBma/yCPoCml\r\n         n8Cg==\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=0p9P/Lxpt/VioWHGd8YolCsO1GHfM/xl919m3ug0isU=;\r\n        b=OEIYZ/3xvJW6dWLmlH6Ey8Z4AEfGPsw8Qxz2VzUNT8bzdFrjFG+B+yCghT1/KKLNZR\r\n         X83hOlKZkwK/PKWcoVhp7W61Tgjinec/28pRtRAe/yNAUAWct7Fu1K5PFKjN06pu1LXt\r\n         ilfQmj5nT8XFd+xxdzr7bHgjJvhAG/EP2nexb3qIAZMGJhyZJUwKxJ+yyX486NAsqssC\r\n         L0UHY8wYOGx0dlypl1V2CMsGlMHNdvFTySoehkgAOfIQU3tr/9oqP6cM+Crqx5CDQ/5j\r\n         nPtN1scHGJ0EsOlfNa44LAqHjGNcUiu/GlDLbiKA3xpF11MroJeeaEgIusbwMxXRdkFU\r\n         X4cw==\r\nARC-Authentication-Results: i=1; mx.google.com;\r\n       dkim=pass header.i=@gmail.com header.s=20161025 header.b=WeaU1ge+;\r\n       spf=pass (google.com: domain of abc@gmail.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=abc@gmail.com;\r\n       dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com\r\nReturn-Path: <abc@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 k35sor9610388wre.35.2019.02.18.17.15.55\r\n        for <xyz@gmail.com>\r\n        (Google Transport Security);\r\n        Mon, 18 Feb 2019 17:15:55 -0800 (PST)\r\nReceived-SPF: pass (google.com: domain of abc@gmail.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41;\r\nAuthentication-Results: mx.google.com;\r\n       dkim=pass header.i=@gmail.com header.s=20161025 header.b=WeaU1ge+;\r\n       spf=pass (google.com: domain of abc@gmail.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=abc@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=0p9P/Lxpt/VioWHGd8YolCsO1GHfM/xl919m3ug0isU=;\r\n        b=WeaU1ge+l/nCwAWPfIKBDCFw1yL4XTzr04amS0OjTYEiwHI+YGajPnDvZa5NeCp+AY\r\n         blah blah

但是SUBJECT和To和FROM的结果为NONE?

SUBJECT : None
TO      : None
FROM    : None

我需要的只是标题主题,从和到

1 个答案:

答案 0 :(得分:0)

感谢Max

                msg = email.message_from_bytes(response_part[1])

是-> Message_from_bytes