add method to read refused emails.
This commit is contained in:
@@ -0,0 +1,128 @@
|
|||||||
|
import datetime
|
||||||
|
import email
|
||||||
|
import imaplib
|
||||||
|
from builtins import list
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
from email.header import decode_header
|
||||||
|
from email.message import Message
|
||||||
|
|
||||||
|
from src.db.mongo_manager import MONGO_STORE_MANAGER
|
||||||
|
from src.mail.mail_constants import DOMAIN_163, DOMAIN_YAHOO, DOMAIN_SINA, IMAP_SERVER_163, YAHOO_IMAP_SERVER, \
|
||||||
|
IMAP_SERVER_SINA, AOL_IMAP_SERVER, DOMAIN_HOTMAIL, HOTMAIL_IMAP_SERVER
|
||||||
|
from src.pojo.mail.mail_pojo import MailPojo
|
||||||
|
|
||||||
|
REFUSED_SUBJECT_EN = 'Appointment follow up'
|
||||||
|
HERMES_EMAIL = "no-reply@hermes.com"
|
||||||
|
|
||||||
|
date_format = "%d-%b-%Y" # DD-Mon-YYYY e.g., 3-Mar-2014
|
||||||
|
|
||||||
|
|
||||||
|
class MailRefusedReader():
|
||||||
|
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 create_imap(self):
|
||||||
|
# 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)
|
||||||
|
elif DOMAIN_SINA in self.login:
|
||||||
|
imap = imaplib.IMAP4_SSL(IMAP_SERVER_SINA)
|
||||||
|
elif DOMAIN_HOTMAIL in self.login:
|
||||||
|
imap = imaplib.IMAP4_SSL(HOTMAIL_IMAP_SERVER)
|
||||||
|
else:
|
||||||
|
imap = imaplib.IMAP4_SSL(AOL_IMAP_SERVER)
|
||||||
|
return imap
|
||||||
|
|
||||||
|
def read_emails(self, mails_messages: list) -> list:
|
||||||
|
# create an IMAP4 class with SSL
|
||||||
|
imap = self.create_imap()
|
||||||
|
# authenticate
|
||||||
|
type, dat = imap.login(self.login, self.password)
|
||||||
|
print("type is {} for {}".format(type, self.login))
|
||||||
|
mail_list = []
|
||||||
|
print("read mails from {}".format(self.login))
|
||||||
|
mail_list.extend(self._get_messages_from_folder(imap, REFUSED_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 REFUSED_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 read_refused_emails():
|
||||||
|
mail_list = MONGO_STORE_MANAGER.get_destination_emails()
|
||||||
|
mails_messages = []
|
||||||
|
# read all the emails
|
||||||
|
with ThreadPoolExecutor(max_workers=100) as executor:
|
||||||
|
for mail in mail_list:
|
||||||
|
if DOMAIN_HOTMAIL not in mail.mail:
|
||||||
|
mail_reader = MailRefusedReader(mail.mail, mail.password)
|
||||||
|
executor.submit(mail_reader.read_emails, mails_messages)
|
||||||
|
if len(mails_messages) > 0:
|
||||||
|
for mail in mails_messages:
|
||||||
|
print(mail.mail_address)
|
||||||
|
print(mail.subject)
|
||||||
|
print(mail.body)
|
||||||
|
|
||||||
|
|
||||||
|
# check whether the url has already been clicked
|
||||||
|
if __name__ == '__main__':
|
||||||
|
read_refused_emails()
|
||||||
Reference in New Issue
Block a user