can read confirmation mails
This commit is contained in:
@@ -18,13 +18,13 @@ from src.notification.mailer import Mailer
|
|||||||
from src.pojo.ResultEnum import ResultEnum
|
from src.pojo.ResultEnum import ResultEnum
|
||||||
from src.pojo.mail.mail_pojo import MailPojo, MailAddress
|
from src.pojo.mail.mail_pojo import MailPojo, MailAddress
|
||||||
|
|
||||||
CONFIRMATION_SUBJECT_FR = 'rendez-vous est'
|
CONFIRMATION_SUBJECT_FR = 'Votre=20rendez-vous=20est=20confirm=C3'
|
||||||
CONFIRMATION_SUBJECT_EN = 'confirmed'
|
CONFIRMATION_SUBJECT_EN = 'confirmed'
|
||||||
HERMES_EMAIL = "no-reply@hermes.com"
|
HERMES_EMAIL = "no-reply@hermes.com"
|
||||||
|
|
||||||
date_format = "%d-%b-%Y" # DD-Mon-YYYY e.g., 3-Mar-2014
|
date_format = "%d-%b-%Y" # DD-Mon-YYYY e.g., 3-Mar-2014
|
||||||
|
|
||||||
FRENCH_CONFIRMED_MESSAGE = "Nous avons le plaisir de vous confirmer votre rendez-vous"
|
FRENCH_CONFIRMED_MESSAGE = "Nous aurons le plaisir de vous accueillir"
|
||||||
|
|
||||||
|
|
||||||
class MailConfirmationReader():
|
class MailConfirmationReader():
|
||||||
@@ -63,16 +63,13 @@ class MailConfirmationReader():
|
|||||||
search_terms = 'SINCE "{}"'.format(
|
search_terms = 'SINCE "{}"'.format(
|
||||||
datetime.datetime.today().strftime(
|
datetime.datetime.today().strftime(
|
||||||
date_format))
|
date_format))
|
||||||
print("search terms is " + search_terms)
|
|
||||||
imap.select_folder(folder)
|
imap.select_folder(folder)
|
||||||
messages = imap.search(['SINCE', datetime.datetime.today()])
|
messages = imap.search(['SINCE', datetime.datetime.today()])
|
||||||
print("%d messages from our best friend" % len(messages))
|
|
||||||
for uid, message_data in imap.fetch(messages, 'RFC822').items():
|
for uid, message_data in imap.fetch(messages, 'RFC822').items():
|
||||||
try:
|
try:
|
||||||
email_message = email.message_from_bytes(message_data[b'RFC822'])
|
email_message = email.message_from_bytes(message_data[b'RFC822'])
|
||||||
from_address = email_message.get('FROM')
|
from_address = email_message.get('FROM')
|
||||||
subject = email_message.get('subject')
|
subject = email_message.get('subject')
|
||||||
# print("{}, {},{}".format(from_address, subject, email_message))
|
|
||||||
body = ""
|
body = ""
|
||||||
if "no-reply@hermes.com" in from_address:
|
if "no-reply@hermes.com" in from_address:
|
||||||
for part in email_message.walk():
|
for part in email_message.walk():
|
||||||
@@ -81,12 +78,11 @@ class MailConfirmationReader():
|
|||||||
body = body + str(part.get_payload(decode=True).decode("utf-8"))
|
body = body + str(part.get_payload(decode=True).decode("utf-8"))
|
||||||
elif part.get_content_type() == "text/plain":
|
elif part.get_content_type() == "text/plain":
|
||||||
body = body + part.get_payload()
|
body = body + part.get_payload()
|
||||||
logger.info("mail is {} and subject is {}".format(self.login, subject))
|
logger.info("mail is {} and subject is {}, body is {}".format(self.login, subject, body))
|
||||||
if CONFIRMATION_SUBJECT_FR in subject or CONFIRMATION_SUBJECT_EN in subject or "Votre_rendez-vous_est_confirm" in 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 = MailPojo(subject=subject, body=body, from_address=from_address)
|
||||||
mail.isImapClient = True
|
mail.isImapClient = True
|
||||||
logger.info("subject is {}".format(subject))
|
mail.mail_address = self.login
|
||||||
logger.info("body is {}".format(body))
|
|
||||||
mail_messages.append(mail)
|
mail_messages.append(mail)
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
print(error)
|
print(error)
|
||||||
@@ -96,9 +92,6 @@ class MailConfirmationReader():
|
|||||||
def _get_messages_from_folder(self, imap, subject, folder="INBOX") -> list:
|
def _get_messages_from_folder(self, imap, subject, folder="INBOX") -> list:
|
||||||
imap.select(folder)
|
imap.select(folder)
|
||||||
mail_messages = []
|
mail_messages = []
|
||||||
# typ, data = imap.search(None, '(SUBJECT "{}" SINCE "{}")'.format(subject,
|
|
||||||
# datetime.datetime.today().strftime(
|
|
||||||
# date_format)))
|
|
||||||
typ, data = imap.search(None, '(SINCE "{}")'.format(
|
typ, data = imap.search(None, '(SINCE "{}")'.format(
|
||||||
datetime.datetime.today().strftime(
|
datetime.datetime.today().strftime(
|
||||||
date_format)))
|
date_format)))
|
||||||
@@ -240,7 +233,7 @@ def read_mails_and_find_confirmation_contacts(all_mails=False):
|
|||||||
# mail_list = [mail_address3]
|
# mail_list = [mail_address3]
|
||||||
|
|
||||||
|
|
||||||
init_logger()
|
# init_logger()
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger()
|
||||||
logger.addHandler(logging.StreamHandler(stream=sys.stdout))
|
logger.addHandler(logging.StreamHandler(stream=sys.stdout))
|
||||||
# check whether the url has already been clicked
|
# check whether the url has already been clicked
|
||||||
|
|||||||
Reference in New Issue
Block a user