我可以打印每封电子邮件。我想要做的是搜索每个电子邮件中的字符串,如果它有字符串,则返回true。我怎样才能使msg
参与其中?
conn.select(readonly=1) # Select inbox or default namespace
(retcode, messages) = conn.search(None, '(UNSEEN)')
if retcode == 'OK':
for num in messages[0].split(' '):
print 'Processing :', messages
typ, data = conn.fetch(num,'(RFC822)')
msg = email.message_from_string(data[0][1])
typ, data = conn.store(num,'-FLAGS','\\Seen')
if retcode == 'OK':
print data,'\n',30*'-'
for line in msg:
if "Subject: Thanks for your Walmart.com Order" in line:
print line
conn.close()
答案 0 :(得分:1)
当你有一个电子邮件对象msg
时,你可以这样做:
message_lines = msg.get_payload().splitlines()
然后遍历每一行:
for line in message_lines:
if 'Walmart' in line:
pass # do whatever
但是,请注意有效载荷是什么......
另一个选择是让IMAP服务器完成工作:
(retcode, messages) = conn.search(None, '(UNSEEN) (TEXT walmart)')
这将对需要获取的消息数进行分组,然后然后如果需要,您可以应用更复杂的匹配...
还有可能按主题搜索(如果这最终是你想做的):
(retcode, messages) = conn.search(None, '(UNSEEN) (SUBJECT Thanks for your Walmart.com Order)')