Example #1
0
def main():
    argparser = argparse.ArgumentParser(
        description="Dump a IMAP folder into .eml files")
    argparser.add_argument(
        '-l',
        dest='local_folder',
        help="Local folder where to create the email folders")
    argparser.add_argument(
        '-d',
        dest='days',
        help="Number of days back to get in the IMAP account",
        type=int)
    argparser.add_argument('-w',
                           dest='wkhtmltopdf',
                           help="The location of the wkhtmltopdf binary")
    argparser.add_argument('-a',
                           dest='specific_account',
                           help="Select a specific account to backup")
    args = argparser.parse_args()
    options = load_configuration(args)

    for account in options['accounts']:

        print('{}/{} (on {})'.format(account['name'], account['remote_folder'],
                                     account['host']))

        if account['remote_folder'] == "__ALL__":
            for folder_entry in get_folder_fist(account):
                folder_name = folder_entry.decode().replace("/",
                                                            ".").split(' "." ')
                print("Saving folder: " + folder_name[1])
                account['remote_folder'] = folder_name[1]
                save_emails(account, options)
        else:
            save_emails(account, options)
Example #2
0
def main():
    global alert_mail
    global error_alert_mail

    try:
        argparser = argparse.ArgumentParser(
            description="Dump a IMAP folder into .eml files")
        argparser.add_argument(
            '-l',
            dest='local_folder',
            help="Local folder where to create the email folders")
        argparser.add_argument(
            '-d',
            dest='days',
            help="Number of days back to get in the IMAP account",
            type=int)
        argparser.add_argument('-w',
                               dest='wkhtmltopdf',
                               help="The location of the wkhtmltopdf binary")
        argparser.add_argument('-a',
                               dest='specific_account',
                               help="Select a specific account to backup")
        args = argparser.parse_args()
        options = load_configuration(args)

        for account in options['accounts']:
            logging.warning('{}/{} (on {})'.format(account['name'],
                                                   account['remote_folder'],
                                                   account['host']))

            if account['remote_folder'] == "__ALL__":
                #basedir = options['local_folder']
                basedir = account['local_folder']
                for folder_entry in get_folder_fist(account):
                    folder_name = folder_entry.decode().replace(
                        "/", ".").split(' "." ')
                    account['remote_folder'] = folder_name[1]
                    account['local_folder'] = os.path.join(
                        basedir, account['remote_folder'])
                    logging.warning("Saving folder: %s to %s", folder_name[1],
                                    basedir)
                    if account[
                            'remote_folder'] != '"[Gmail]"' and not 'Spam' in account[
                                'remote_folder'] and not 'Junk' in account[
                                    'remote_folder']:
                        error_alert_mail = save_emails(account, options)
            else:
                logging.warning('Saving folder: %s to %s',
                                account['remote_folder'],
                                account['local_folder'])
                error_alert_mail = save_emails(account, options)
    except Exception as e:
        error_alert_mail += 1
        logging.exception("main crashed. Error: %s, %s erreurs de sauvegarde",
                          e, error_alert_mail)
        print(e)
    logging.critical("%s erreurs de sauvegarde", error_alert_mail)
    if error_alert_mail >= 1 and alert_mail != '.':
        os.system('echo \'Erreur sauvegarde mail IMAPBOX ' +
                  str(error_alert_mail) +
                  ' erreurs\' | mail -s \'Erreur sauvegarde mail\' ' +
                  alert_mail)