diff --git a/mail/mail_reader_all_contacts.py b/mail/mail_reader_all_contacts.py index 4020b5f..4676803 100644 --- a/mail/mail_reader_all_contacts.py +++ b/mail/mail_reader_all_contacts.py @@ -231,8 +231,9 @@ class MailReader: imap.select(folder) mail_messages = [] - # 搜索符合条件的所有邮件ID - search_query = '(SUBJECT "{}" SINCE "{}")'.format(subject, datetime.datetime.today().strftime(DATE_FORMAT)) + # 搜索符合条件的所有邮件ID(服务器端同时过滤发件人,减少无关邮件下载量) + search_query = '(FROM "{}" SUBJECT "{}" SINCE "{}")'.format( + HERMES_EMAIL, subject, datetime.datetime.today().strftime(DATE_FORMAT)) typ, data = imap.search(None, search_query) ids = data[0].split() @@ -303,7 +304,7 @@ class MailReader: print("{}: search terms is {}".format(self.login, search_terms)) imap.select_folder(folder) - messages = imap.search(['SINCE', datetime.datetime.today()]) + messages = imap.search(['SINCE', datetime.datetime.today(), 'FROM', HERMES_EMAIL]) print("{}: {} messages from our best friend".format(self.login, len(messages))) if len(messages) == 0: @@ -314,45 +315,37 @@ class MailReader: try: email_message = email.message_from_bytes(message_data[b'RFC822']) - # 获取发件人和主题 from_address = email_message.get('FROM') or "" subject = email_message.get('subject') or "" - # 检查是否是Hermes邮件 - hermes_mail_address = "no-reply@hermes.com" - if (hermes_mail_address in from_address or - "outlook.com" in from_address or - "hotmail" in from_address): + # 提取邮件正文(委托给 imap_proxy_reader.extract_body) + body = extract_body(email_message) - # 提取邮件正文(委托给 imap_proxy_reader.extract_body) - body = extract_body(email_message) + # 检查是否是预约验证邮件 + if (VALIDATION_URL_SUBJECT_FR in subject or + VALIDATION_URL_SUBJECT_EN in subject or + "Votre=20demande=20de=20rendez-vous" in subject or + "Votre demande de rendez-vous" in body): - # 检查是否是预约验证邮件 - if (VALIDATION_URL_SUBJECT_FR in subject or - VALIDATION_URL_SUBJECT_EN in subject or - "Votre=20demande=20de=20rendez-vous" in subject or - "Votre demande de rendez-vous" in body): + mail = MailPojo( + subject=subject, + body=body, + from_address=from_address + ) + mail.isImapClient = True - mail = MailPojo( - subject=subject, - body=body, - from_address=from_address - ) - mail.isImapClient = True + print("email is {}".format(self.login)) + print("subject is {}".format(subject)) - print("email is {}".format(self.login)) - print("body is {}".format(body)) - print("subject is {}".format(subject)) + # 设置收件人地址 + if len(mail.to_address) == 0: + if "outlook.com" in from_address or "hotmail.com" in from_address: + # 转发邮件 + mail.to_address = extract_email_from_from_address(from_address) + else: + mail.to_address = self.login - # 设置收件人地址 - if len(mail.to_address) == 0: - if "outlook.com" in from_address or "hotmail.com" in from_address: - # 转发邮件 - mail.to_address = extract_email_from_from_address(from_address) - else: - mail.to_address = self.login - - mail_messages.append(mail) + mail_messages.append(mail) except Exception as error: print("Error trying to read email_Message for {}: {}".format(self.login, error))