Exemplo n.º 1
0
    def run(self, imap_obj: imaplib.IMAP4, attemps=0):
        """Sends IMAP APPEND command.

        Raises: ImapRuntimeError - if command executions failed

        :param imap_obj: imaplib.IMAP4
        :return: Namespaces
        """

        try:
            typ, data = imap_obj.append(self.__folder.imap_name(),
                                        self.__flags,
                                        imaplib.Time2Internaldate(time.time()),
                                        self._message.as_bytes())
            self.check_response(typ, data)
        except ImapTryCreate as _:
            if attemps > 0:
                raise RecursionError('Could not append message because folder '
                                     'does not exists and we could not create '
                                     'it')
            ImapCreateFolderCommand(self.__folder).run(imap_obj)
            self.run(imap_obj, 1)
        else:
            if b'APPENDUID' in data[0]:
                append_uid, *_ = data[0].partition(b']')
                self._message.uid = int(append_uid.split()[-1])
            else:
                self._message.uid = 0
        return self._message
def change_swa_settings(conn: imaplib.IMAP4, swa_options: dict):
    """Changes swa preference email or create a new one if message with
    preference does not exists.

    :param swa_options:
    :param conn: imaplib.IMAP4
    :return:
    """
    status, data = conn.select('#Scalix/Oddpost')
    if status != 'OK':
        raise Exception('Could not select folder #Scalix/Oddpost. '
                        'Imap response: {}'.format(data))

    orphan_uids = set()
    if not int(data[0]):
        #  there are no messages in mailbox fetching will fail so we will
        #  skip it
        email = create_preference_message()
    else:
        email, orphan_uids = find_swa_preference_email(conn)

    if not email:
        email = create_preference_message()

    # lets apply changes to the preferences
    for key, value in swa_options.items():
        find_option_and_change(email.preferences, key, value)

    # if we have UID for previous email jus tadd it to delete
    if email.uid:
        orphan_uids.add(email.uid)

    if orphan_uids:
        uids_del = b','.join([str(uid).encode() for uid in orphan_uids])
        status, data = conn.uid('STORE', uids_del.decode(), '+FLAGS',
                                '\\Deleted')
        if status != 'OK':
            warnings.warn(
                'Could not delete duplicate preference emails {}'
                '. Error {}.'.format(orphan_uids, data),
                RuntimeWarning
            )

    status, data = conn.append('#Scalix/Oddpost', '\\Seen',
                               imaplib.Time2Internaldate(time.time()),
                               email.as_bytes())
    if status != 'OK':
        if email.uid:
            conn.uid('STORE', email.uid, '-FLAGS', '\\Deleted')
        raise Exception('Could not save email. Error message {}'.format(data))

    conn.expunge()
Exemplo n.º 3
0
def append_email(mbox: IMAP4, msg: EmailMessage):
    return mbox.append(MAILBOX, None, Time2Internaldate(time()),
                       msg.as_bytes())