简体   繁体   English

电子邮件的python IMAP内容包含一个字符串

[英]python IMAP content of email contains a string

I am able to log in a gmail account with python IMAP 我可以使用python IMAP登录Gmail帐户

imap = imaplib.IMAP4_SSL('imap.gmail.com')
imap.login(myDict["emailUsername"], myDict["emailPassword"])
imap.select(mailbox='inbox', readonly=False)
resp, items = imap.search(None, 'All')
email_ids = items[0].split()
latest_email_id = email_ids[-1]

resp, data = imap.fetch(latest_email_id, "(UID)")
print ("resp= ", resp, " data=", data)
#msg_uid = parse_uid(data[0])
match = pattern_uid.match(data[0].decode("utf-8"))
#print ("match= ", match)
msg_uid = match.group('uid')

I need to make sure that the UID for the last email I have contains a certain string (XYZ). 我需要确保我收到的最后一封电子邮件的UID包含某个字符串(XYZ)。 I am NOT looking for header subject but the content of email. 我不是在寻找标题主题,而是在寻找电子邮件的内容。 How can I do that ? 我怎样才能做到这一点 ?

There's a couple ways you could go: 您可以采取几种方法:

  1. Fetch the message and walk through the text body parts looking for your string -- example at Finding links in an emails body with Python 提取消息并遍历文本正文部分以查找字符串-例如, 在使用Python在电子邮件正文查找链接的示例

  2. Get the server to do the search by supplying 'latest_email_id' and your search criteria back to the server in a UID SEARCH command. 通过在UID SEARCH命令中向服务器提供'latest_email_id'和您的搜索条件,来使服务器进行搜索。 For Gmail, you can even use the X-GM-RAW attribute to use the same syntax support by the GMail web interface. 对于Gmail,您甚至可以使用X-GM-RAW属性通过GMail网络界面使用相同的语法支持。 See https://developers.google.com/gmail/imap/imap-extensions for details of that. 有关详细信息,请参见https://developers.google.com/gmail/imap/imap-extensions

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

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