🦅🦅ENGLISH VERSION BELOW🦅🦅
Dieses python Skript lädt alle Mails von einem IMAP E-Mail Postfach herunter und speichert diese in einer .zip Datei. Die Datei kann dann z.B. für ein externes Backup genutzt werden.
Getestet wurde das Skript mit Python 3.13.2. Außerdem wird das python Modul imap-tools benötigt (pip install imap-tools).
Damit das Skript sich mit dem gewünschten IMAP Server verbinden kann, erstelle eine Kopie der Datei config.ini.example (Befindet sich im Ordner config) und benne sie um in config.ini. Anschließend trage die benötigten Informationen in die Konfigurationsdatei ein. Falls weitere Postfächer gesichert werden sollen, erstelle nach belieben weitere Kopien der Beispieldatei. Wenn beim Starten der Parameter --config_file=[Name der config Datei] mit angegeben wird, wird das entsprchende Postfach über die angegeben Daten gesichert.
imap_serverist der imap Server.- Google:
imap.gmail.com - Apple:
imap.mail.me.com - Mailbox.org:
imap.mailbox.org - Proton: Benötigt die bridge
^ Erfolgreich mit einer SSL Verbindung getestet. Das Skript kann, ohne weitere Einstellungen, mit einer SSL Verbindung genutzt werden. STARTTLS wird nicht unterstützt.
- Google:
imap_usernameist die Mailadresse oder der Benutzername (r_deEDV@example.com)imap_passwordkann ein normales Passwort sein oder ein App Token.imap_portDer Port. Standardmäßig993
Für die erste Ausführung empfehle ich die Variable list_Only_Folders auf True zu setzen, um die "wahren" und kompletten Ordnernamen / Pfade zu erhalten.
Falls später ein Ordner ausgeschlossen werden soll, muss dieser exakt so geschrieben werden wie dieser in der Ausgabe angegeben ist.
Die Ausgabe sollte in etwa so aussehen:
Archiv
Junk
Trash
Drafts
Sent
INBOX/CatchAll
INBOX/Kino
INBOX/Amazon
INBOX/Selfhosted
INBOX/Selfhosted/NAS
INBOX/Selfhosted/Paperless
INBOX
| Einstellung | Beschreibung | Zeile im Skript | Standardwert |
|---|---|---|---|
folders_to_exclude |
Schließt einen Ordner von der Sicherung aus. | Zeile 17 | folders_to_exclude = [""] |
list_Only_Folders |
Zeigt nur alle Ordner an und führt keine Sicherung aus. | Zeile 24 | False |
MailBox_folder_list |
Sichert nur einen spzifischen Ordner / Unterordner | Zeile 26 | |
ZIP_export_folder |
Wo die .zip Datei gespeichert werden soll. |
config.ini | export |
days_to_delete |
Löscht alle Sicherungen älter als x Tage. Deaktiviert mit 0. |
config.ini | 30 |
| Einstellung | Beschreibung | Standardwert |
|---|---|---|
--config_file= |
Gibt die config Datei an die genutzt werden soll. | config.ini |
Beispiel: --config_file=config_gmail.ini
Gibt an welche config Datei für die Sicherung genutzt werden soll. Der Name der Datei kann frei gewählt werden, jedoch muss diese sich in dem Unterordner config befinden. Wenn nichts angegeben wird, wird Standardmäßig die Datei config.ini genutzt.
This python Script downlaods all Mails from a imap Mailbox and saves them in a Folder as a .zip File for using them otherwise. Like an external Backup.
This Script is tested with Python 3.13.2 and you need the imap-tools python Module (pip install imap-tools).
Just copy / move the file config.ini.example (can be found in the folder config) to config.ini and fill in the needed information (values don`t need to be in ""). When you need to backup more than one Mailbox, just copy the example file and fill in the needed Information for the Mailbox.
imap_serveris, obviously, the imap Server.- Google:
imap.gmail.com - Apple:
imap.mail.me.com - Mailbox.org:
imap.mailbox.org - Proton: Needs the bridge
^ Successfully tested with an IMAP SSL Connection. The Script can be used as it is with an SSL Connection. But not usable with an STARTTLS Connection.
- Google:
imap_usernameis the mailaddress or the username (r_selfhosted@example.com)imap_passwordcan be a normal Password or an App Token.imap_portDefault Port value993
For the first run I recommend to set the Variable list_Only_Folders to True, to get the "real" and full foldernames / paths.
If you want to exclude an folder, you have to use the full name.
The export should look something like this:
Archiv
Junk
Trash
Drafts
Sent
INBOX/CatchAll
INBOX/Kino
INBOX/Amazon
INBOX/Selfhosted
INBOX/Selfhosted/NAS
INBOX/Selfhosted/Paperless
INBOX
| Setting | Description | Location | default |
|---|---|---|---|
folders_to_exclude |
Exclude a folder from the Backup | Script line 17 | folders_to_exclude = [""] |
list_Only_Folders |
Display only folders and skips the export | Script line 24 | False |
MailBox_folder_list |
Download a specific Folder and Subfolder | Script line 26 | empty |
ZIP_export_folder |
The folder for exported zip file. | config.ini | export |
days_to_delete |
Removes files older than x days. Disabled with value 0. |
config.ini | 30 |
| Setting | Description | default |
|---|---|---|
--config_file= |
Sets the config file. | config.ini |
Example: --config_file=config_gmail.ini
Specifies which config file should be used for the backup. The file name can be freely chosen, but it must be located in the config subfolder. If nothing is specified, the config.ini file is used by default.