简体   繁体   English

Python在imap电子邮件中搜索字符串

[英]Python search imap email for a string

New to python, having some trouble getting past this. python的新手,遇到一些麻烦。
Am getting back emails from gmail via imap (with starter code from https://yuji.wordpress.com/2011/06/22/python-imaplib-imap-example-with-gmail/ ) and want to search a specific email (which I am able to fetch) for a specific string. 我正在通过imap从gmail取回电子邮件(具有来自https://yuji.wordpress.com/2011/06/22/python-imaplib-imap-example-with-gmail/的入门代码),并且想要搜索特定的电子邮件(我能够提取)的特定字符串。 Something like this 像这样

ids = data[0]
id_list = ids.split()
ids = data[0]
id_list = ids.split()
latest_email_id = id_list[-1]
result, data = mail.fetch(latest_email_id, "(RFC822)") 
raw_email = data[0][1] 

def search_raw():
    if 'gave' in raw_email:
        done = 'yes'
    else:
        done = 'no'

and it always sets done to no. 并且总是将完成设置为否。 Here's the output for the email (for the body section of the email) 这是电子邮件的输出(电子邮件的正文部分)

Content-Type multipart/related;boundary=1_56D8EAE1_29AD7EA0;type="text/html"
--1_56D8EAE1_29AD7EA0
Content-Type text/html;charset="UTF-8"
Content-Transfer-Encoding base64

PEhUTUw+CiAgICAgICAgPEhFQUQ+CiAgICAgICAgICAgICAgICA8VElUTEU+PC9USVRMRT4KICAg
ICAgICA8L0hFQUQ+CiAgICAgICAgPEJPRFk+CiAgICAgICAgICAgICAgICA8UCBhbGlnbj0ibGVm
dCI+PEZPTlQgZmFjZT0iVmVyZGFuYSIgY29sb3I9IiNjYzAwMDAiIHNpemU9IjIiPlNlbnQgZnJv
bSBteSBtb2JpbGUuCiAgICAgICAgICAgICAgICA8QlI+X19fX19fX19fX19fX19fX19fX19fX19f
X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fXzwvRk9OVD48L1A+CgogICAgICAg
ICAgICAgICAgPFBSRT4KR2F2ZQoKPC9QUkU+CiAgICAgICAgPC9CT0RZPgo8L0hUTUw+Cg==
--1_56D8EAE1_29AD7EA0--

I know the issue is the html, but can't seem to figure out how to parse the email properly. 我知道问题是html,但似乎无法弄清楚如何正确解析电子邮件。

Thank you! 谢谢!

The text above is base64 encoding. 上面的文本是base64编码。 Python has a module named base64 which gives you the ability to decode it. Python有一个名为base64的模块,它使您能够对其进行解码。

import base64
import re


def has_gave(raw_email):
    email_body = base64.b64decode(raw_email)
    match = re.search(r'.*gave.*', email_body , re.IGNORECASE)
    if match:
        done = 'yes'
        print 'match found for word ', match.group()
    else:
        done = 'no'
        print 'no match found'

    return done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM