use imapClient

This commit is contained in:
2024-12-05 23:31:14 +01:00
parent f1cf40198d
commit d0d3a30377
3 changed files with 60 additions and 39 deletions
+21 -12
View File
@@ -1,5 +1,7 @@
import datetime
import email
import logging
import sys
from builtins import list
from concurrent.futures import ThreadPoolExecutor
from email.header import decode_header
@@ -8,6 +10,7 @@ from imapclient import IMAPClient
from src.db.mirgration.migration_tools import migre_accepted_appointment
from src.db.mongo_manager import MONGO_STORE_MANAGER
from src.logs.AppLogging import init_logger
from src.mail.mail_constants import create_imap, show_folders
from src.notification.AcceptedResultPojo import get_accepted_result_from
from src.notification.SignalSender import SignalSender
@@ -78,12 +81,12 @@ class MailConfirmationReader():
body = body + str(part.get_payload(decode=True).decode("utf-8"))
elif part.get_content_type() == "text/plain":
body = body + part.get_payload()
print("mail is {} and subject is {}".format(self.login, subject))
logger.info("mail is {} and subject is {}".format(self.login, subject))
if CONFIRMATION_SUBJECT_FR in subject or CONFIRMATION_SUBJECT_EN in subject or "Votre_rendez-vous_est_confirm" in subject:
mail = MailPojo(subject=subject, body=body, from_address=from_address)
mail.isImapClient = True
print("subject is {}".format(subject))
print("body is {}".format(body))
logger.info("subject is {}".format(subject))
logger.info("body is {}".format(body))
mail_messages.append(mail)
except Exception as error:
print(error)
@@ -93,9 +96,12 @@ class MailConfirmationReader():
def _get_messages_from_folder(self, imap, subject, folder="INBOX") -> list:
imap.select(folder)
mail_messages = []
typ, data = imap.search(None, '(SUBJECT "{}" SINCE "{}")'.format(subject,
datetime.datetime.today().strftime(
date_format)))
# typ, data = imap.search(None, '(SUBJECT "{}" SINCE "{}")'.format(subject,
# datetime.datetime.today().strftime(
# date_format)))
typ, data = imap.search(None, '(SINCE "{}")'.format(
datetime.datetime.today().strftime(
date_format)))
for i in data[0].split():
# fetch the email message by ID
res, msg = imap.fetch(i.decode("utf-8"), "(RFC822)")
@@ -114,8 +120,8 @@ class MailConfirmationReader():
from_address, subject_encoded = decode_header(msg.get("From"))[0]
if isinstance(from_address, bytes):
from_address = from_address.decode(subject_encoded)
print("From:", from_address)
print("Subject:", subject)
logger.info("From:{}".format(from_address))
logger.info("Subject: {}".format(subject))
# if the email message is multipart
if msg.is_multipart():
# iterate over email parts
@@ -132,7 +138,7 @@ class MailConfirmationReader():
print(Error)
else:
body = msg.get_payload(decode=True).decode()
print(body)
logger.info(body)
if CONFIRMATION_SUBJECT_FR in subject or CONFIRMATION_SUBJECT_EN in subject:
mail = MailPojo(subject=subject, body=body, from_address=from_address)
mail.mail_address = self.login
@@ -156,7 +162,7 @@ def accept_appointment_found(accepted_result_list: list):
for user in _all_register_account:
if user.mail == result.email:
result.account_password = user.password
mailer.send_email(result, to_all=False)
mailer.send_email(result, to_all=True)
MONGO_STORE_MANAGER.update_reserve_result(reserve.id, ResultEnum.ACCEPTED, reserve.message)
# sginal.send_result(result)
@@ -173,8 +179,8 @@ def find_confirmation_contacts_for_today():
for _item in _all_appointments_today:
for _mail in _all_mail_list:
if _mail.mail == _item.mail:
if _item.url_validated is True:
_mail_list_to_scan.append(_mail)
# if _item.url_validated is True:
_mail_list_to_scan.append(_mail)
break
print("Found {} emails to scan".format(len(_mail_list_to_scan)))
return _mail_list_to_scan
@@ -234,6 +240,9 @@ def read_mails_and_find_confirmation_contacts(all_mails=False):
# mail_list = [mail_address3]
init_logger()
logger = logging.getLogger()
logger.addHandler(logging.StreamHandler(stream=sys.stdout))
# check whether the url has already been clicked
if __name__ == '__main__':
# read_mails_and_find_confirmation_contacts()