Files
appointment_tool/src/mail/mail_confirmation.py
T
2022-10-23 13:00:38 +02:00

158 lines
6.7 KiB
Python

import datetime
import email
import imaplib
from concurrent.futures import ThreadPoolExecutor
from email.header import decode_header
from email.message import Message
from builtins import list
from src.db.mirgration.migration_tools import migre_accepted_appointment
from src.db.mongo_manager import MONGO_STORE_MANAGER
from src.notification.AcceptedResultPojo import get_accepted_result_from
from src.notification.mailer import Mailer
from src.pojo.ResultEnum import ResultEnum
from src.pojo.mail.mail_pojo import MailPojo, MailAddress
AOL_IMAP_SERVER = "imap.aol.com"
IMAP_SERVER_163 = "imap.163.com"
YAHOO_IMAP_SERVER = "imap.mail.yahoo.com"
CONFIRMATION_SUBJECT_FR = 'Votre rendez-vous est'
CONFIRMATION_SUBJECT_EN = 'appointment is confirmed'
HERMES_EMAIL = "no-reply@hermes.com"
DOMAIN_YAHOO = "yahoo.com"
DOMAIN_163 = "163.com"
date_format = "%d-%b-%Y" # DD-Mon-YYYY e.g., 3-Mar-2014
class MailConfirmationReader():
def __init__(self, login, password):
self.login = login
self.password = password
@staticmethod
def show_folders(imap):
for i in imap.list()[1]:
l = i.decode().split(' "/" ')
print(l[0] + " = " + l[1])
def read_emails(self, mails_messages: list) -> list:
# create an IMAP4 class with SSL
if DOMAIN_163 in self.login:
imap = imaplib.IMAP4_SSL(IMAP_SERVER_163)
elif DOMAIN_YAHOO in self.login:
imap = imaplib.IMAP4_SSL(YAHOO_IMAP_SERVER)
else:
imap = imaplib.IMAP4_SSL(AOL_IMAP_SERVER)
# authenticate
imap.login(self.login, self.password)
mail_list = []
print("read mails from {}".format(self.login))
# self.show_folders(imap)
mail_list.extend(self._get_messages_from_folder(imap, CONFIRMATION_SUBJECT_FR))
mail_list.extend(self._get_messages_from_folder(imap, CONFIRMATION_SUBJECT_EN))
# close the connection and logout
imap.close()
imap.logout()
mails_messages.extend(mail_list)
return mail_list
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)))
for i in data[0].split():
# fetch the email message by ID
res, msg = imap.fetch(i.decode("utf-8"), "(RFC822)")
body = ''
for response in msg:
if isinstance(response, tuple):
# parse a bytes email into a message object
msg = email.message_from_bytes(response[1])
# decode the email subject
subject, subject_encoded = decode_header(msg["Subject"])[0]
received_date = msg["Date"]
if isinstance(subject, bytes):
# if it's a bytes, decode to str
subject = subject.decode(subject_encoded)
# decode email sender
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)
# if the email message is multipart
if msg.is_multipart():
# iterate over email parts
for part in msg.walk():
try:
# get the email body
payloads = part.get_payload()
if isinstance(payloads, list):
for payload in payloads:
if isinstance(payload, Message):
body = body + payload.get_payload(decode=True).decode("iso-8859-1")
# print(body)
except Exception as Error:
print(Error)
else:
body = msg.get_payload(decode=True).decode()
print(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
mail_messages.append(mail)
return mail_messages
def clean(text):
# clean text for creating a folder
return "".join(c if c.isalnum() else "_" for c in text)
def accept_appointment_found(accepted_result_list: list):
mailer = Mailer()
for reserve in accepted_result_list:
mailer.send_email(get_accepted_result_from(reserve), to_all=True)
MONGO_STORE_MANAGER.update_reserve_result(reserve.id, ResultEnum.ACCEPTED)
if len(accepted_result_list) > 0:
migre_accepted_appointment(str(datetime.date.today()))
def read_mails_and_find_confirmation_contacts():
mail_list = MONGO_STORE_MANAGER.get_destination_emails()
# mail_address3 = MailAddress(mail="ciyuexie@aol.com", password="czezlmmyypokdfce")
# mail_list = [mail_address3]
mails_messages = []
# read all the emails
with ThreadPoolExecutor(max_workers=30) as executor:
for mail in mail_list:
mail_reader = MailConfirmationReader(mail.mail, mail.password)
executor.submit(mail_reader.read_emails, mails_messages)
accepted_appointment_list = []
if len(mails_messages) > 0:
successful_items = MONGO_STORE_MANAGER.get_all_successful_items_for_day()
successful_items.extend(MONGO_STORE_MANAGER.get_all_successful_items_for_yesterday())
for mail in mails_messages:
message_body = mail.body
for item in successful_items:
if item.id in message_body and item.id != "welcome":
item.message = message_body
accepted_appointment_list.append(item)
elif "10:30" in message_body and (item.email == mail.mail_address or item.email in message_body):
item.message = message_body
accepted_appointment_list.append(item)
print(mail.mail_address)
print(mail.subject)
print(mail.body)
accept_appointment_found(accepted_appointment_list)
# check whether the url has already been clicked
if __name__ == '__main__':
read_mails_and_find_confirmation_contacts()