Esempio n. 1
0
def fetch_mails_info(imap_account, mail_set=None, decode=True, limit=None):
    """Retrieve information for every mail in mail_set

    .. versionadded:: 0.2

    :param imap_account: imaplib.IMAP4 or imaplib.IMAP4_SSL instance
    :param mail_set: List of mail UID
    :param decode: Wether we must or mustn't decode mails informations
    :param limit: Return only last mails
    """
    flags_re = re.compile(FLAGS_RE)
    mail_id_re = re.compile(MAIL_ID_RE)
    uid_re = re.compile(UID_RE)

    if mail_set is None:
        mail_set = fetch_uids(imap_account, limit=limit)
    elif isinstance(mail_set, six.string_types):
        mail_set = mail_set.split()

    mails_data = fetch.fetch(imap_account, mail_set,
                             ['BODY.PEEK[HEADER]', 'FLAGS', 'UID'])
    if mails_data is None:
        return

    for mail_data in mails_data:
        flags_match = flags_re.match(mail_data[0])
        mail_id_match = mail_id_re.match(mail_data[0])
        uid_match = uid_re.match(mail_data[0])
        if mail_id_match is None or flags_match is None or uid_match is None:
            continue

        flags = flags_match.groupdict().get('flags').split()
        mail_id = mail_id_match.groupdict().get('mail_id').split()[0]
        mail_uid = uid_match.groupdict().get('uid').split()[0]

        mail = email.message_from_string(mail_data[1])
        if decode is True:
            for header_name, header_value in mail.items():
                header_new_value = []
                for value, encoding in header.decode_header(header_value):
                    if value is None:
                        continue
                    try:
                        decoded_value = codecs.decode(value,
                                                      encoding or 'utf-8',
                                                      'ignore')
                    except TypeError:
                        log.debug(u'Can\'t decode {} with {} encoding'.format(
                            value, encoding))
                        decoded_value = value
                    header_new_value.append(decoded_value)
                mail.replace_header(header_name, ' '.join(header_new_value))

        yield dict([
            ('flags', flags),
            ('id', mail_id),
            ('uid', mail_uid),
            ('from', mail['from']),
            ('to', mail['to']),
            ('date', mail['date']),
            ('subject', mail.get('subject', '')),
        ])
Esempio n. 2
0
def fetch_mails_info(imap_account, mail_set=None, decode=True, limit=None):
    """Retrieve information for every mail in mail_set

    .. versionadded:: 0.2

    :param imap_account: imaplib.IMAP4 or imaplib.IMAP4_SSL instance
    :param mail_set: List of mail UID
    :param decode: Wether we must or mustn't decode mails informations
    :param limit: Return only last mails
    """
    flags_re = re.compile(FLAGS_RE)
    mail_id_re = re.compile(MAIL_ID_RE)
    uid_re = re.compile(UID_RE)

    if mail_set is None:
        mail_set = fetch_uids(imap_account, limit=limit)
    elif isinstance(mail_set, six.string_types):
        mail_set = mail_set.split()

    mails_data = fetch.fetch(imap_account, mail_set,
                             ['BODY.PEEK[HEADER]', 'FLAGS', 'UID'])
    if mails_data is None:
        return

    for mail_data in mails_data:
        flags_match = flags_re.match(mail_data[0])
        mail_id_match = mail_id_re.match(mail_data[0])
        uid_match = uid_re.match(mail_data[0])
        if mail_id_match is None or flags_match is None or uid_match is None:
            continue

        flags = flags_match.groupdict().get('flags').split()
        mail_id = mail_id_match.groupdict().get('mail_id').split()[0]
        mail_uid = uid_match.groupdict().get('uid').split()[0]

        mail = email.message_from_string(mail_data[1])
        if decode is True:
            for header_name, header_value in mail.items():
                header_new_value = []
                for value, encoding in header.decode_header(header_value):
                    if value is None:
                        continue
                    try:
                        decoded_value = codecs.decode(value, encoding
                                                      or 'utf-8', 'ignore')
                    except TypeError:
                        log.debug(u'Can\'t decode {} with {} encoding'.format(
                            value, encoding))
                        decoded_value = value
                    header_new_value.append(decoded_value)
                mail.replace_header(header_name, ' '.join(header_new_value))

        yield dict([
            ('flags', flags),
            ('id', mail_id),
            ('uid', mail_uid),
            ('from', mail['from']),
            ('to', mail['to']),
            ('date', mail['date']),
            ('subject', mail.get('subject', '')),
        ])
Esempio n. 3
0
 def test_fetch_wrong(self):
     self.imap_account = imaplib.IMAP4_SSL()
     assert fetch.fetch(self.imap_account, None) is None
     assert fetch.fetch(self.imap_account, []) is None
Esempio n. 4
0
 def test_fetch_wrong(self):
     self.imap_account = imaplib.IMAP4_SSL()
     assert fetch.fetch(self.imap_account, None) is None
     assert fetch.fetch(self.imap_account, []) is None