嗨所以我有这个程序,基本上进入收件箱,然后查看最新的电子邮件。扫描它,如果它有一个链接,它会给你一个通知,或者如果它有一个附件,它会发出通知。尽管我尝试将两个通知合并为一个,但一切工作正常。所以它更好一点,而不是一个通知显示,然后是另一个通知显示。如果电子邮件同时包含链接和附件。我更喜欢它只是一个通知,如果电子邮件有两个,然后单独通知,如果它们只包含一个链接或附件。我试图把它分成if和elif循环。
如果我在if语句的循环中执行'和',我可以获得两者的通知 - 例如
if any(word in html_text for word in word) and file_name == None:
fboth()
fboth()(这个函数告诉你有链接和附件,就像flink()和fattach())
虽然当我把我的其他选项作为elif:语句时,一个用于链接本身,一个用于附件本身。即使它发现第一个声明为True,它仍然会播出,它会警告电子邮件同时包含链接和附件。有没有阻止这个?我开始认为这是另一个if声明?但是我尝试了一些尝试失败,也许没有完全理解它。我的理论是否正确仍然使用for循环来解决它?
这是代码:
import imaplib
import email
import Tkinter as tk
import time
word = ["href=", "href", "<a href="] #list of strings to search for in email body
#connection to the email server
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('xxxx', 'xxxx')
mail.list()
latest_email_uid = ''
#Finding a link popup
def flink():
flink = tk.Tk()
flink.title("Found Link")
tk.Label(flink, text="Email has link in body\n" + "From: " + msg['From'] + "\n" + "Subject: " + msg['Subject'] + "\n" + "Date: " + msg['Date'], fg='yellow', bg='black').pack()
flink.after(5000, lambda: flink.destroy()) # time in ms
flink.mainloop()
#Finding an attachment popup
def fattach():
fattach = tk.Tk()
fattach.title("Found Attachment")
tk.Label(fattach, text= " Latest Email has attachment\n" + "Filename: " + file_name + "\n" + "Fron: " + msg['From'] + "\n" + "Subject: " + msg['Subject'] + "\n" + "Date: " + msg['Date'], fg='yellow', bg='black').pack()
fattach.after(5000, lambda: fattach.destroy()) # time in ms
fattach.mainloop()
while True:
mail.select("Inbox", readonly=True) # connect to inbox.
result, data = mail.uid('search', None, "ALL") # search and return uids instead
ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
if data[0].split()[-1] == latest_email_uid:
time.sleep(120) # value here once per minute is already considered fairly aggressive by many IMAP server admins. Depending on your application and expected
else:
latest_email_uid = data[0].split()[-1]
result, data = mail.uid('fetch', latest_email_uid, '(RFC822)') # fetch the email headers and body (RFC822) for the given ID7
raw_email = data[0][1]
# "---------------------------------------------------------"
# "Are there links in the email?"
# "---------------------------------------------------------"
msg = email.message_from_string(raw_email)
for part in msg.walk():
# each part is a either non-multipart, or another multipart message
# that contains further parts... Message is organized like a tree
if part.get_content_type() == 'text/html':
html_text = part.get_payload()
if any(word in html_text for word in word):
flink()
else:
pass
# "---------------------------------------------------------"
# "Are there attachments?"
# "---------------------------------------------------------"
for part in msg.walk():
attach = part.get_content_type()
file_name = part.get_filename()
if file_name == None:
pass
else:
fattach()
mail.close()
time.sleep(120)
编辑:好的所以我得到了这一点,我可以得到循环找到一个消息,只有一个链接在电子邮件正文(第一个elif),但没有其他工作。当发送一个应该使我的file_name!=现在没有的附件时,是吗?
msg = email.message_from_string(raw_email)
for part in msg.walk():
attach = part.get_content_type()
file_name = part.get_filename()
# each part is a either non-multipart, or another multipart message
# that contains further parts... Message is organized like a tree
if part.get_content_type() == 'text/html':
html_text = part.get_payload()
text = any(word in html_text for word in word)
if text == True and file_name != None:
print "file name and attachment"
elif text == True and file_name == None:
print "Just link in text"
elif text == False and file_name != None:
print "just an attachment"
你不必要地两次做walk
。只需在同一个循环中调用这两个函数。
maybe = False
msg = email.message_from_string(raw_email)
for part in msg.walk():
if part.get_content_type() == 'text/html':
html_text = part.get_payload()
if any(word in html_text for word in word):
maybe = True
else:
attach = part.get_content_type()
file_name = part.get_filename()
if file_name == None:
pass
else:
maybe = True
mail.close()
if maybe:
# display message
如果您希望消息更详细地反映是否存在链接或附件或两者,您可以将maybe
标志变量从简单的布尔值更改为例如一个set
,您传递给消息显示功能,以指示准确填充消息对话框的字符串。