def parse_extra(self, imap: imaplib.IMAP4_SSL, num: str, parsed_items: List[dict]) -> None: """Add GMail labels to parsed_items""" try: # we use GMail IMAP Extensions # https://developers.google.com/gmail/imap/imap-extensions#access_to_gmail_labels_x-gm-labels _, data = imap.fetch(num, "(X-GM-LABELS)") # it seems that there is nothing to help parsing in standard lib # thus we use some regex to get our labels data_bytes = data[0] if not isinstance(data_bytes, bytes): raise ValueError(f"Unexpected data type: {type(data_bytes)}") data_str = data_bytes.decode("utf-7") match_labels_str = RE_LABELS_STR.search(data_str) if match_labels_str is None: raise ValueError( f"Can't find the expected label string in data: {data_str:r}" ) labels_str = match_labels_str.group(1) labels = [(m.group("quoted") or m.group("unquoted")).replace('\\"', '"') for m in RE_LABEL.finditer(labels_str)] for parsed_item in parsed_items: subjects = parsed_item.setdefault("subject", []) for label in labels: subjects.append({ "name": label, "qcode": label, "scheme": "gmail_label" }) except Exception: logger.exception("Can't retrieve GMail labels")
def get_latest_email(imap_session: imaplib.IMAP4_SSL) -> Message: status, messages = imap_session.select("Matt") latest_email_id = int(messages[0]) logging.info("Fetching latest email") res, message = imap_session.fetch(str(latest_email_id), "(RFC822)") for response in message: if isinstance(response, tuple): return email.message_from_bytes(response[1])
def find_timepie_attachments( imap: imaplib.IMAP4_SSL, should_ignore_msg: t.Callable[[MsgId], bool], sender: str, is_gmail: bool = False, ) -> t.Mapping[MsgId, t.Sequence[Ping]]: status: t.Any details: t.Any status, details = imap.select('INBOX') if status != 'OK': raise RuntimeError(f'select failed: {details}') query = (_build_gmail_query(sender) if is_gmail else f'HEADER FROM {json.dumps(sender)}') status, details = imap.search(None, query) if status != 'OK': raise RuntimeError(f'bad status on search: {status}') msg_ids = set(details[0].decode('utf-8').split(' ')) logger.debug(f'pre-filter msg_ids = {msg_ids}') msg_ids = {m for m in msg_ids if not should_ignore_msg(m)} logger.debug(f'post-filter msg_ids = {msg_ids}') result = {} for msg_id in sorted(msg_ids): logger.info(f'fetching msg {msg_ids}') msg_info: t.Any status, msg_info = imap.fetch(msg_id, '(RFC822)') if status != 'OK': raise RuntimeError(f'bad status on fetch: {status}') msg = email.message_from_bytes(msg_info[0][1]) try: result[msg_id] = _find_timepie_part(msg) except ValueError: logger.info(f'msg {msg_id} has no timepie part') continue return result
def searchFolder(imapServer: imaplib.IMAP4_SSL, searchQuery, type, folder): imapServer.select(folder) searchResult = imapServer.search(searchQuery, type) ids = searchResult[1][0].decode("utf-8").split() foundMessages = [] for i in ids: emailData = imapServer.fetch(str(i), '(RFC822)') print("=======================================") for response_content in emailData: arr = response_content[0] if isinstance(arr, tuple): msg = email.message_from_string(str(arr[1], 'utf-8')) foundMessages.append({ "From": msg['From'], "Subject": msg['Subject'], "Date": msg["Date"], "Message-ID": msg["Message-ID"] }) imapServer.store(str(i), '-FLAGS', '\Flagged') return foundMessages
def gen_unseen_mbank_emails( database: KsiemgowyDB, mail: imaplib.IMAP4_SSL, imap_filter: str ) -> T.Iterator[Message]: """Connects to imap_server using login and password from the arguments, then yields a pair (mail_id_as_str, email_as_eml_string) for each of e-mails coming from mBank.""" mail.select("inbox") _, data = mail.search(None, imap_filter) mail_ids = data[0] id_list = mail_ids.split() for mail_id in reversed(id_list): _, data = mail.fetch(mail_id, "(RFC822)") for mail_number, response_part in enumerate(data): if not isinstance(response_part, tuple): continue msg = email.message_from_string(response_part[1].decode()) mail_key = f'{msg["Date"]}_{mail_number}' if database.was_imap_id_already_handled(mail_key): continue LOGGER.info("Handling e-mail id: %r", mail_id) yield msg database.mark_imap_id_already_handled(mail_key)