我正在一个更大的程序中建立一个监视系统,该程序将通过设置的电子邮件地址检查电子邮件,以查找电子邮件地址和链接。当前,它仅从包含文件电子邮件的数组中查找电子邮件。但是,我也希望它查找链接。现在,如果找到了这两个,我希望它向接收到的邮件中找到的电子邮件发送电子邮件。此外,该程序将不断运行以阅读电子邮件,并且在阅读邮件后不会停止。
def sendHotmail(receivingEmail, name):
email = EmailMessage()
email['Subject'] = subject
email['From'] = myEmail
email['To'] = receivingEmail
if count == 0:
email.set_content('Hello {},\nI have attached the link to the servey that you will need to complete\n\n www.google.com'.format(name))
else:
email.set_content('Hello {},\nHere is the link to your certificate{}'.format(name, link))
s = smtplib.SMTP(host='smtp.live.com', port = 587)
s.starttls()
s.login(myEmail, myEmailPass)
s.send_message(email)
s.quit()
def monitorHotmail():
m = imaplib.IMAP4_SSL('imap-mail.outlook.com',993)
m.login(myEmail, myEmailPass)
print(m.list())
m.select("Inbox") # <-- response like ('OK', ['74']) --> total no. of mail in sent folder
while emailsSent != emailsReceived :
result, data = m.uid('search', None, "ALL") # search all email and return uids
if result == 'OK':
for num in data[0].split():
result, data = m.uid('fetch', num, '(RFC822)')
if result == 'OK':
email_message = email.message_from_bytes(data[0][1]) # raw email text including headers
sentBy = email_message['From']
sentBy = sentBy.split(' ')
if sentBy[2] == '<xxxxx@gmail.com>':
if email_message.is_multipart():
for payload in email_message.get_payload():
message = payload.get_payload()
for emailFound in emails:
if emailFound in message:
print(emailFound)
除了永久打印消息中找到的电子邮件,直到电子邮件被删除或程序停止并且未完成为止,它都能正常工作。
答案 0 :(得分:0)
将搜索更改为仅查找看不见的电子邮件。一旦您检索RFC822正文(假定为非偷窥),就会设置“已见”标志,因此,如果您的搜索仅查找“未见”,那么您只会看到每条消息一次。