def __create_msg__(self, mime_data, uid): # create object message = Message(uid=uid) # converts byte literal to string removing b'' raw_email_string = mime_data.decode('utf-8') imap_msg = message_from_string(raw_email_string) # set From addr message.rec_addr = unidecode(imap_msg.get('From')) # set subject tmp_sbj, tmp_charset = header.decode_header(imap_msg.get('Subject'))[0] if (tmp_charset is None): message.subject = tmp_sbj else: message.subject = unidecode(tmp_sbj.decode(tmp_charset)) # set date message.date = imap_msg.get('Date') # set folder - folder name is epochdate epoch = time.mktime(utils.parsedate(message.date)) folder_name = f'{int(epoch)}' message.folder = folder_name # set attachs and body # downloading attachments and body message = self.__include_attachs__(message, imap_msg) return message
def redirect_attached_msg(self, to_addr, from_addr=None, folder='', search_str='X-GM-RAW in:PROCESSAR', size_limit=3000000): # simplify imap_mngr = self.__IMAPManager__ smtp_mngr = self.__SMTPManager__ file_mngr = self.__FileManager__ atch_path = self.__attachs_path__ if (from_addr is None): from_addr = self.__from_addr__ # Login Imap imap_mngr.login() # Search message list_message = imap_mngr.search_message(search_str=search_str) # Loop in list of message for msg in list_message: log.info(f' Processing {msg.subject}') # Msg without attach if (len(msg.attachs) == 0): msg.subject = f'[ERRO: sem anexo]{msg.subject}' smtp_mngr.send_email(msg, to_addr, from_addr) imap_mngr.mark_message(msg) continue # Each attach per message for atch_name in msg.attachs: # Get attach path atch_path = msg.get_attach_path(atch_name) extension = atch_name.split('.')[-1].lower() size = file_mngr.verify_size(atch_path) if (size < size_limit): msg_child = copy_only_message(msg) msg_child.attachs.append(atch_name) smtp_mngr.send_email(msg_child, to_addr, from_addr) else: # switch case with if if (extension == 'pdf'): if ("SEPARAR" in msg.subject): fl_split = f's_{atch_name}' list_flsplit = file_mngr.split_attach( atch_path, fl_split) for fl in list_flsplit: msg_child = copy_only_message(msg) msg_child.attachs.append(fl) smtp_mngr.send_email(msg_child, to_addr, from_addr) levelCompress = 0 while (levelCompress <= 4): # compress file fl_compress = f'c_{atch_name}' file_mngr.compress(atch_path, fl_compress, power=levelCompress) # create msg msg_child = copy_only_message(msg) msg_child.attachs.append(fl_compress) atch_comp = msg_child.get_attach_path(fl_compress) sz_compress = file_mngr.verify_size(atch_comp) levelCompress = levelCompress + 1 if (sz_compress < size_limit): break if (sz_compress > size_limit): # Lets split into n Message msg_child = Message() msg_child.subject = f'ERRO: arquivo({atch_name})' \ 'ainda é grande' smtp_mngr.send_email(msg_child, to_addr, from_addr) else: smtp_mngr.send_email(msg_child, to_addr, from_addr) elif (extension == 'other'): pass else: msg_err = copy_only_message(msg) msg_err.subject = f'ERRO: arquivo({atch_name})' \ 'é grande e não é .pdf' smtp_mngr.send_email(msg_err, to_addr, from_addr) imap_mngr.mark_message(msg)
def send_error_email(self, msg_body, to_addr, from_addr): smtp_mngr = self.__SMTPManager__ msg = Message() msg.subject = '[ERRO internal]' msg.body = str(msg_body) smtp_mngr.send_email(msg, to_addr, from_addr)