Giter Club home page Giter Club logo

imapbackup's Introduction

Language grade: Python

imapbackup

A Python script for creating full backups of IMAP mailboxes

Background

This was first published around 2007 (probably earlier) on my personal site, and it was originally developed to work around the then rather limited (ok, inconsistent) Mac OS X Mail.app functionality and allow me to back up my old mailboxes in a fully standard mbox format (well, at least as much as mbox can be considered a standard...).

Somewhat to my surprise it was considered useful by quite a few people throughout the years, and contributions started coming in. Given that there seems to be renewed interest in this as a systems administration tool, I'm posting the source code here and re-licensing it under the MIT license.

Features

  • ZERO dependencies.
  • Copies every single message from every single folder (or a subset of folders) in your IMAP server to your disk.
  • Does incremental copying (i.e., tries very hard to not copy messages twice).
  • Tries to do everything as safely as possible (only performs read operations on IMAP).
  • Generates mbox formatted files that can be imported into Mail.app (just choose "Other" on the import dialog).
  • Optionally compresses the result files on the fly (and can append to them). Only available in Python 2.x
  • Is completely and utterly free (distributed under the MIT license).

Requirements

Python 2.x : imapbackup.py should work on Python 2.5 or any other 2.x version without any extra dependencies whatsoever. Python 3.x : imapbackup38.py requires python 3.8 and above.

Contributing

I am accepting pull requests, but bear in mind that one of the original goals of this script was to run on older Python versions, so as to save sysadmins stuck in the Dark Ages the trouble of installing a newer Python (much to my own amazement, this was originally written in Python 2.3).

I would be delighted to bring it fully up to date with Python 2.7.x/3.x, etc., and have considered adding multi-threading/multiprocessing to speed it up, but time is short enough as it is. If you feel up to the task just send me a pull request with a "new" main script with the target Python version as part of its name - something like imapbackup27.py, imapbackup39.py, etc.

Disclaimer

For tradition's sake, here goes:

IN NO EVENT WILL I BE LIABLE FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, THOSE RESULTING FROM LOST PROFITS, LOST DATA, LOST REVENUE OR BUSINESS INTERRUPTION) ARISING OUT OF THE USE, INABILITY TO USE, OR THE RESULTS OF USE OF, THIS PROGRAM. WITHOUT LIMITING THE FOREGOING, I SHALL NOT BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES THAT MAY RESULT FROM THE USE OF THIS SCRIPT OR ANY PORTION THEREOF WHETHER ARISING UNDER CONTRACT, NEGLIGENCE, TORT OR ANY OTHER LAW OR CAUSE OF ACTION. I WILL ALSO PROVIDE NO SUPPORT WHATSOEVER, OTHER THAN ACCEPTING FIXES AND UPDATING THE SCRIPT AS IS DEEMED NECESSARY.

imapbackup's People

Contributors

c-duv avatar danielruf avatar ezzra avatar janst123 avatar loicpirez avatar markfeit avatar mmachicao avatar pofilo avatar rcarmo avatar samsonjs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

imapbackup's Issues

imapbackup.py doesn't run on Fedora 32 (Python 3.8.3)

On Fedora Linux version: Fedora release 32 (Thirty Two)
I get the follwoing error:

/usr/bin/env: „python -u“: Datei oder Verzeichnis nicht gefunden
/usr/bin/env: Verwenden Sie -[v]S, um Optionen in #!-Zeichen zu übergeben

replacing #!/usr/bin/env python -u

with

#!/usr/bin/env -S python -u

solves the problem.

But then I get a lot of errors regarding the print statement:

Python version is:

python --version
Python 3.8.3

File ".../imap-backup/imapbackup.py", line 145
print "Deleting", filename
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Deleting", filename)?

File ".../imap-backup/imapbackup.py", line 163
print "New messages: 0"

SyntaxError: Missing parentheses in call to 'print'. Did you mean print("New messages: 0")?

File ".../imap-backup/imapbackup.py", line 206
print ": %s total, %s for largest message" % (pretty_byte_count(total),
^
SyntaxError: invalid syntax

File "/.../imap-backup/imapbackup.py", line 220
print "File %s: not found" % filename
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("File %s: not found" % filename)?

File "/..../imap-backup/imapbackup.py", line 245
print "WARNING: Message #%d in %s" % (i, filename),
^
SyntaxError: invalid syntax

File "/..../imap-backup/imapbackup.py", line 258
print "WARNING: Message #%d in %s" % (i, filename),
^
SyntaxError: invalid syntax

File "/..../imap-backup/imapbackup.py", line 266
print ": %d messages" % (len(messages.keys()))
^
SyntaxError: invalid syntax

File "/.../imap-backup/imapbackup.py", line 311
print ":",
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(":", end=" ")?

File "/..../imap-backup/imapbackup.py", line 314
print "%d messages" % (len(messages.keys()))
^
SyntaxError: invalid syntax

File "/.../imap-backup/imapbackup.py", line 415
print ": %s folders" % (len(names))
^
SyntaxError: invalid syntax

File "/.../imap-backup/imapbackup.py", line 422
print "Usage: imapbackup [OPTIONS] -s HOST -u USERNAME [-p PASSWORD]"
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Usage: imapbackup [OPTIONS] -s HOST -u USERNAME [-p PASSWORD]")?

File "/..../imap-backup/imapbackup.py", line 584
print "WARNING:", warning
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("WARNING:", warning)?

File "/.../imap-backup/imapbackup.py", line 617
print "Connecting to '%s' TCP port %d," % (
^
SyntaxError: invalid syntax

File "/.../imap-backup/imapbackup.py", line 635
print "Logging in as '%s'" % (config['user'])
^
SyntaxError: invalid syntax

File "/.../imap-backup/imapbackup.py", line 636
server.login(config['user'], config['pass']))
^
SyntaxError: invalid syntax

Handling of "Umlaute"/ special characters in folder names may be broken

I'm using the latest "38" version with Python 3.7 on Mac OS X

(by the way: IMHO there simply should be a single imapbackup "Python3" version with a minimum requirement, e.g. Python 3.5, instead making a separate imapbackup version for each Python sub version. That's a bit weird 😁 )

Anyway… I'm backing up an IMAP mailbox where the mail provider has localized names for the standard folders, e.g. "Entwürfe" for a "Drafts" folder. Those names don't make it properly, but remain encoded in some form), e.g.:

$ mkdir mybackup
$ cd mybackup
$ ../imapbackup.py --server=mail.example.com --user=example --pass=1234567890
Connecting to 'mail.example.com' TCP port 143
Logging in as 'example'
Finding Folders /(\HasNoChildren \Sent) "/" Gesendet
(\HasNoChildren \Trash) "/" Papierkorb
(\HasNoChildren \Junk) "/" Spam
(\HasNoChildren \Drafts) "/" Entw&APw-rfe
(\HasNoChildren) "/" INBOX
Finding Folders: 5 folders
Folder "Gesendet": 
0 messages
File Gesendet.mbox: not found
New messages: 0
Folder "Papierkorb": 
0 messages
File Papierkorb.mbox: not found
New messages: 0
Folder "Spam": 
0 messages
File Spam.mbox: not found
New messages: 0
Folder "Entw&APw-rfe": 
0 messages
File Entw&APw-rfe.mbox: not found
New messages: 0
Folder "INBOX": 
2 messages
File INBOX.mbox: not found
Downloading 2 new messages to INBOX.mbox: 3.51 KB total, 2.81 KB for largest message
Disconnecting

$ ls -l
total 8
-rw-r--r--  1 turtlesrock  wheel     0 Aug 21 00:15 Entw&APw-rfe.mbox
-rw-r--r--  1 turtlesrock  wheel     0 Aug 21 00:15 Gesendet.mbox
-rw-r--r--  1 turtlesrock  wheel  3668 Aug 21 00:15 INBOX.mbox
-rw-r--r--  1 turtlesrock  wheel     0 Aug 21 00:15 Papierkorb.mbox
-rw-r--r--  1 turtlesrock  wheel     0 Aug 21 00:15 Spam.mbox

Note the strange Entw&APw-rfe.mbox which should be Entwürfe.mbox

(Extra Note: The generated filenames probably should be more properly sanitized too, e.g. filtered from potentially dangerous character sequences, unprintables, or OS incompatible characters. Else a rouge server can do some harm. Basically threat those values from the server as untrusted user input into the script.)

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2: ordinal not in range(128)

Hi, I got this error why trying the script today:

Python 3.8.10 shipped with Ubuntu 20.04

Folder INBOX/Sent: 
6540 messages
File INBOX.Sent.mbox -Traceback (most recent call last):
  File "./imapbackup38.py", line 758, in <module>
    main()
  File "./imapbackup38.py", line 670, in main
    fil_messages = scan_file(filename, config['overwrite'], config['nospinner'], basedir)
  File "./imapbackup38.py", line 226, in scan_file
    for message in mbox:
  File "/usr/lib/python3.8/mailbox.py", line 109, in itervalues
    value = self[key]
  File "/usr/lib/python3.8/mailbox.py", line 73, in __getitem__
    return self.get_message(key)
  File "/usr/lib/python3.8/mailbox.py", line 781, in get_message
    msg.set_from(from_line[5:].decode('ascii'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2: ordinal not in range(128)

instalacion?

Hi,
I was wondering if this could be applied to backup zimbra mailboxes?
to another ext4 or NTFS device?
Thank you

Option to ignore a list of folders?

Some IMAP servers provides non-e-mails as mailboxes. For instance Exchange has:

  • Calendar
  • Contacts
  • Tasks
  • RSS Feeds
  • Journal
  • Also: Sync Issues

When backuping such mailboxes with imapbackup it can fails, or even if it does not, maybe you don't want to backup theses folders.

There is an option to pass folder(s) to backup, but none for the one to ignore

If desired I can provide a small PR introducing the --ignored-folders option with the following changes:

@@ -675,12 +675,16 @@ def main():
          server = connect_and_login(config)
          names = get_names(server, config['compress'], config['thunderbird'],
                            config['nospinner'])
+         ignored_dirs = []
          if config.get('folders'):
              dirs = map(lambda x: x.strip(), config.get('folders').split(','))
              if config['thunderbird']:
                  dirs = [i.replace("Inbox", "INBOX", 1) if i.startswith("Inbox") else i
                          for i in dirs]
              names = filter(lambda x: x[0] in dirs, names)
+         elif config.get('ignored-folders'):
+             # Use "--ignored-folders" option's list only if "--folders" option was not set
+             ignored_dirs = map(lambda x: x.strip(), config.get('ignored-folders').split(','))
  
          # for n, name in enumerate(names): # *DEBUG
          #   print n, name # *DEBUG
@@ -690,6 +694,11 @@ def main():
          for name_pair in names:
              try:
                  foldername, filename = name_pair
+ 
+                 # Skip ignored directory
+                 if foldername in ignored_dirs:
+                     continue
+ 
                  fol_messages = scan_folder(
                      server, foldername, config['nospinner'])
                  fil_messages = scan_file(filename, config['compress'],

Long-running backup fails by timeout

I am trying to backup a ~25Go mailbox and the script terminates (during backup) with one of the following errors:

  • ERROR: 110 Connection timed out
  • ERROR: 104 Connection reset by peer
  • ERROR: command: FETCH => socket error: EOF

I guess it's because of the size of the mailbox?
Is there a way to define a longer wait time and/or reconnect to server?

Maildir support?

I wonder whether anyone thought about supporting maildir (in addition to mbox) to make the backup less susceptible to errors.

Thoughts?

Authentication Fails with Complicated Password String

I'm attempting to run imapbackup on my Gmail account (IMAP has been enabled already), but am running into an issue as my password is long with many special characters in it, and I keep getting authentication failed.

These are the IMAP settings for Gmail (once you've enabled IMAP in your Settings > See all settings > Forwarding and POP/IMAP > Enable IMAP):

image

The command I'm trying to run, using these settings, looks like this (note password and email have been changed, so this will not work as is, but simulates what I'm attempting):

python imapbackup38.py --folders="Starred" --ssl --server="imap.gmail.com" --user="[email protected]" --pass="qN03s@dSGabr33%pQjW@re%tqNIf&c^M4JDbl!Czx$v$r1GVI#@wTr^AwTbfdkglX"

This results in an output that looks like:

Connecting to 'imap.gmail.com' TCP port 993, SSL
Logging in as '[email protected]'
ERROR: b'[AUTHENTICATIONFAILED] Invalid credentials (Failure)'

I have tried the following commands with the same issue:

Putting an r before the password string

python imapbackup38.py --folders="Starred" --ssl --server="imap.gmail.com" --user="[email protected]" --pass=r"qN03s@dSGabr33%pQjW@re%tqNIf&c^M4JDbl!Czx$v$r1GVI#@wTr^AwTbfdkglX"

Escaping all special characters in the string

python imapbackup38.py --folders="Starred" --ssl --server="imap.gmail.com" --user="[email protected]" --pass="qN03s\@dSGabr33\%pQjW\@re\%tqNIf\&c\^M4JDbl\!Czx\$v\$r1GVI\#\@wTr\^AwTbfdkglX"

How can I get it to parse this kind of password string?

exclude trash folder?

Hi
I was wondering if its possible to exclude the trash folder?
I see the option -f but this option would be inbox and sent but if someone adds a folder outside of inbox it wont backup

Thank you

Location saving?

Currently running the script saves the mbox files in the current directory.

NOTE: mbox files are created in the current working directory.
So i thought i would copy the script in different folder to backup a few emails and created a bash script but even though it saves in the root directory


python /media/usb/emailbak2/imapbackup.py --thunderbird -s mail.mydomain.com -u [email protected] -p mypass -e 

python /media/usb/emailbak3/imapbackup.py --thunderbird -s mail.mydomain.com -u [email protected] -p mypass -e 

Not sure if possible to tell the imapbackup.py to save in another directory?

Thanks

Any support for IMAP flags?

I'm new to imapbackup and wonder whether there is any support for IMAP flags? If not, do they get trashed or ignored or somehow influence the resulting mbox database?

error on backing up

Hi
Currently was trying to run the imapbackup.py but getting this error

Current command line

python imapbackup.py --thunderbird -s mail.domain.com.com -u [email protected] -p mypass -e

Thank you

Finding Folders: 9 folders
Folder Archivo: 0 messages
File Archivo: 0 messages
New messages: 0
Folder Deleted Messages: 6 messages
File Deleted Messages: 135 messages
Downloading 6 new messages to Deleted Messages: 266.85 KB total, 55.41 KB for largest message
Folder Drafts: 9 messages
File Drafts: 9 messages
New messages: 0
Folder INBOX:
Traceback (most recent call last):
  File "imapbackup.py", line 792, in <module>
    main()
  File "imapbackup.py", line 713, in main
    (err, desc) = e
ValueError: need more than 1 value to unpack

warning issue?

HI,
I was wondering if someone else has had this issue before, on the logs it shows

Wed Aug 12 19:27:01 -05 2020 WARNING: Message #1885 in Inbox has a malformed Message-Id header.
Wed Aug 12 19:27:01 -05 2020
Wed Aug 12 19:27:01 -05 2020 WARNING: Message #2080 in Inbox has a malformed Message-Id header.
Wed Aug 12 19:27:01 -05 2020
Wed Aug 12 19:27:01 -05 2020 WARNING: Message #2467 in Inbox has a malformed Message-Id header.
Wed Aug 12 19:27:01 -05 2020
Wed Aug 12 19:27:01 -05 2020 WARNING: Message #2757 in Inbox has a malformed Message-Id header.
Wed Aug 12 19:27:01 -05 2020
Wed Aug 12 19:27:01 -05 2020 WARNING: Message #2759 in Inbox has a malformed Message-Id header.
Wed Aug 12 19:27:01 -05 2020
Wed Aug 12 19:27:01 -05 2020 WARNING: Message #2773 in Inbox has a malformed Message-Id header.
Wed Aug 12 19:27:01 -05 2020
Wed Aug 12 19:27:01 -05 2020 WARNING: Message #2853 in Inbox has a malformed Message-Id header.
Wed Aug 12 19:27:01 -05 2020
Wed Aug 12 19:27:01 -05 2020 WARNING: Message #2854 in Inbox has a malformed Message-Id header.
Wed Aug 12 19:27:01 -05 2020
Wed Aug 12 19:27:01 -05 2020 WARNING: Message #2855 in Inbox has a malformed Message-Id header.
Wed Aug 12 19:27:01 -05 2020
Wed Aug 12 19:27:01 -05 2020 WARNING: Message #2856 in Inbox has a malformed Message-Id header.
Wed Aug 12 19:27:01 -05 2020
Wed Aug 12 19:27:01 -05 2020 WARNING: Message #2865 in Inbox has a malformed Message-Id header.
Wed Aug 12 19:27:01 -05 2020
Wed Aug 12 19:27:01 -05 2020 WARNING: Message #2887 in Inbox has a malformed Message-Id header.
Wed Aug 12 19:27:01 -05 2020
Wed Aug 12 19:27:01 -05 2020 WARNING: Message #2893 in Inbox has a malformed Message-Id header.
Wed Aug 12 19:27:01 -05 2020
Wed Aug 12 19:27:01 -05 2020 WARNING: Message #2894 in Inbox has a malformed Message-Id header.
Wed Aug 12 19:27:01 -05 2020
Wed Aug 12 19:27:01 -05 2020 WARNING: Message #2905 in Inbox has a malformed Message-Id header.

Not sure if those messages did not copy?

Fails when mailbox contains quotation mark

imapbackup.py 1.4h fails when a mailbox in the account contains quotation marks.

I was archiving a former user (have used imapbackup numerous times previously), and it failed with the following errors:

../imapbackup.py -s imap.server.net -u [email protected] -p password
Connecting to 'imap.server.net' TCP port 143
Logging in as '[email protected]'
Finding Folders -Traceback (most recent call last):
File "../imapbackup.py", line 715, in
main()
File "../imapbackup.py", line 604, in main
config['nospinner'])
File "../imapbackup.py", line 350, in get_names
lst = parse_list(row)
File "../imapbackup.py", line 313, in parse_list
row = row.strip()
AttributeError: 'tuple' object has no attribute 'strip'

I logged into the user's account, and realized they had used quotation marks in several mailbox names. I removed the quotation marks and imapbackup proceeded without issue.

I have re-tested by creating a "test" mailbox (quotes included) in my own account, and running imapbackup. It fails with the same errors above. Removed the "test" mailbox fixed the problem.

I do not know if there is any standard recommendation against using quotation marks in a mailbox name, so I presume imapbackup should be able to escape them appropriately.

Running imapbackup 1.4h on Mac OS X Server 10.11.6 with Python 2.7.10.

mailbox.org: `Error in IMAP command LIST`

Running python imapbackup38.py -f INBOX --ssl -s imap.mailbox.org -u [email protected] yields the following:

Password:
Connecting to 'imap.mailbox.org' TCP port 993, SSL
Logging in as '[email protected]'
Finding Folders |ERROR: LIST command error: BAD [b'Error in IMAP command LIST: Invalid reference (0.001 + 0.000 secs).']

I'm sure the folder name is correct (screenshot), and the CLI help says the folders argument is a comma-separated list, so I'm not sure what I could be doing wrong if this isn't a bug.

image

icloud: AttributeError: 'int' object has no attribute 'strip'

Connecting to 'imap.mail.me.com' TCP port 993, SSL
Logging in as '<user>'
Finding Folders /() "/" "CARS"
<snip_many_more_folders>
Finding Folders: 81 folders
Folder "CARS": 
70 messages
File CARS.mbox: 1 messages
Downloading 70 new messages to CARS.mbox |Traceback (most recent call last):
  File "imapbackup/imapbackup38.py", line 793, in <module>
    main()
  File "imapbackup/imapbackup38.py", line 714, in main
    download_messages(server, filename, new_messages, config['overwrite'], config['nospinner'], config['thunderbird'], basedir)
  File "imapbackup/imapbackup38.py", line 182, in download_messages
    text_bytes = data_bytes.strip().replace(b'\r', b'')
AttributeError: 'int' object has no attribute 'strip'

python 3.10.6
Linux ripper 5.19.7-arch1-1 #1 SMP PREEMPT_DYNAMIC Mon, 05 Sep 2022 18:09:09 +0000 x86_64 GNU/Linux

missing error output for unknown options

I have tried python3 imapbackup38.py --mbox-dir=backups/2021-06-04 --ssl --no-spinner --server=example.com --user=example

Shouldn't this provide the following output?
"Unknown option: --no-spinner"

Fails to create folder with special characters

The email server is surgemail, and has a semi invisible System Recycling Folder. You only typically see it when using IMAP.

I get the below error when it tries to create this folder. I assume it doesnt like a character somewhere

Finding Folders: 62 folders
Traceback (most recent call last):
File "/mnt/user/backups/email/imapbackup/imapbackup.py", line 749, in
main()
File "/mnt/user/backups/email/imapbackup/imapbackup.py", line 648, in main
create_folder_structure(names)
File "/mnt/user/backups/email/imapbackup/imapbackup.py", line 626, in create_folder_structure
os.mkdir(disk_foldername)
OSError: [Errno 2] No such file or directory: '-System Recycling-.sbd/#Lists.sbd'

Non utf8 content in raw email

I had an email with non utf-8 compatible character in the subject.

The subject was something like: command n° 193
But the ° is a problem as it appears like: command n� 193

The error was:

Traceback (most recent call last):
  File "/imapbackup38.py", line 283, in scan_folder
    msg_id = MSGID_RE.match(header).group(1)
AttributeError: 'NoneType' object has no attribute 'group'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/imapbackup38.py", line 761, in <module>
    main()
  File "/imapbackup38.py", line 671, in main
    fol_messages = scan_folder(
  File "/imapbackup38.py", line 297, in scan_folder
    data_str = str(data[0][1], 'utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 145: invalid start byte

I'm sending a PR to fix this. We use the replace option of str() to replace any non utf8 character.
The result with the PR looks like: command n? 193 and the mail is correctly retrieved and the script continues to download mails.
This is not perfect because the subject is altered, but at least the script doesn't crash and the mail is partly retrieved.

ERROR: command: FETCH => socket error: EOF

Hello,

I know the last update was 3 years ago, but still I have to say it is one of my favorite scripts when it comes to creating MBOX backups.

I'm just facing the issue right now that I want to back up the "All Mail" label from a huge Gmail inbox ( 200 GB / 5,500,000 emails).

When I'm running the script, I get already the "ERROR: command: FETCH => socket error: EOF" during the initialization phase.

To overcome this, asked AI to help me to adjust the "connect_and_login" function. At first, it looked quite promising. The download started and roughly 1 GB was downloaded during 2,5 h.

def connect_and_login(config):
    """Connects to the server and logs in.  Returns IMAP4 object."""
    try:
        assert not (('keyfilename' in config) ^ ('certfilename' in config))
        if config['timeout']:
            socket.setdefaulttimeout(config['timeout'])

        server = None
        while True:
            try:
                if config['usessl'] and 'keyfilename' in config:
                    print("Connecting to '%s' TCP port %d," % (
                        config['server'], config['port']),)
                    print("SSL, key from %s," % (config['keyfilename']),)
                    print("cert from %s " % (config['certfilename']))
                    server = imaplib.IMAP4_SSL(
                        config['server'], config['port'], config['keyfilename'], config['certfilename'])
                elif config['usessl']:
                    print("Connecting to '%s' TCP port %d, SSL" % (
                        config['server'], config['port']))
                    server = imaplib.IMAP4_SSL(config['server'], config['port'])
                else:
                    print("Connecting to '%s' TCP port %d" % (
                        config['server'], config['port']))
                    server = imaplib.IMAP4(config['server'], config['port'])

                # speed up interactions on TCP connections using small packets
                server.sock.setsockopt(
                    socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

                print("Logging in as '%s'" % (config['user']))
                r, d = server.login(config['user'], config['pass'])
                assert r == 'OK', 'login failed'

                # If login is successful, break out of the loop
                break
            except (socket.error, imaplib.IMAP4.error) as e:
                if isinstance(e, socket.error):
                    print("ERROR: could not connect to '%s' (%s)" % (
                        config['server'], e))
                else:
                    print("ERROR:", e)

                # If there's a socket error, try reconnecting
                if server is not None:
                    try:
                        server.logout()
                    except:
                        pass
                server = None
                time.sleep(1)  # Wait for a second before attempting to reconnect

        # Return the connected server object
        return server

    except socket.gaierror as e:
        (err, desc) = e
        print("ERROR: problem looking up server '%s' (%s %s)" % (
            config['server'], err, desc))
        sys.exit(3)

But after 2,5 h the download was interrupted, and I saw again the same error: "ERROR: command: FETCH => socket error: EOF"

As I was running a backup with a different tool at the same time for the same account and this tool also stopped, I'm assuming the Google server interrupted the connection.

My question, how the "ERROR: command: FETCH => socket error: EOF" be resolved, and the script adjusted in that manner, that if the connection is lost or an error occurs it is not terminating, instead it tries to reconnect and continues?

In the end, I would like to use this script to back up the account unattended over the next 5 - 10 days.

Thanks in advance.

Can't have space in `--folders` name?

Output from my terminal:

D:\Projects\Windows Shell>python ..\imapbackup\imapbackup38.py -f "Archived,Elysia Park" --ssl -s imap.mailbox.org -u [email protected] -d mailbox.org
Password:
Connecting to 'imap.mailbox.org' TCP port 993, SSL
Logging in as '[email protected]'
Finding Folders /(\HasNoChildren \UnMarked) "/" "Elysia Park"
(\HasNoChildren \UnMarked) "/" Archived
(\HasNoChildren \UnMarked) "/" Accounts
(\HasNoChildren \UnMarked) "/" Receipts
(\HasNoChildren \UnMarked \Junk) "/" Junk
(\HasNoChildren \UnMarked \Trash) "/" Trash
(\HasNoChildren \UnMarked \Drafts) "/" Drafts
(\HasNoChildren \UnMarked \Sent) "/" Sent
(\HasNoChildren) "/" INBOX
Finding Folders: 9 folders
Folder Elysia Park:
SELECT failed: [b"Mailbox doesn't exist: Elysia (0.001 + 0.000 secs)."]
Folder Archived:
8 messages
File Archived.mbox: 8 messages
New messages: 0
Disconnecting

(minor side note: a linebreak after "Finding Folders" would keep the folder listing nicely laid out)

As you can see there's a folder with a space in its name and it shows up verbatim in the listing, but the SELECT apparently can't handle it and truncates what comes after the space.

Thank you!

imapbackup.py saved my arse today with some ancient mail servers. It's old but still gold. Thank you very much! (Sorry for abusing the issue tracker for this)

IOError: [Errno 5] Input/output error

We encounter some problems regarding with I/O and this is the output:

File INBOX.ACCOUNTS.mboxTraceback (most recent call last): File "/usr/local/bin/imapbackup", line 749, in <module> main() File "/usr/local/bin/imapbackup", line 655, in main config['overwrite'], config['nospinner']) File "/usr/local/bin/imapbackup", line 225, in scan_file for message in mailbox.PortableUnixMailbox(mbox): File "/usr/local/lib/python2.7/mailbox.py", line 2072, in next self._search_end() File "/usr/local/lib/python2.7/mailbox.py", line 2095, in _search_end line = self.fp.readline() IOError: [Errno 5] Input/output error

it might get a workaround by using >/dev/null 2>&1 at the end of the command line. Might as well update the script

UnicodeEncodeError: 'ascii' codec can't encode character '\xa3'

Hi there,

Ran across an issue with certain language characters in the password, specifically "£" in this case.

Password:
Connecting to 'imap.test.com' TCP port 143
Logging in as '[email protected]'
Traceback (most recent call last):
  File "/home/suders/imapbackup/imapbackup38.py", line 793, in <module>
    main()
  File "/home/suders/imapbackup/imapbackup38.py", line 671, in main
    server = connect_and_login(config)
  File "/home/suders/imapbackup/imapbackup38.py", line 615, in connect_and_login
    server.login(config['user'], config['pass'])
  File "/usr/lib/python3.9/imaplib.py", line 610, in login
    typ, dat = self._simple_command('LOGIN', user, self._quote(password))
  File "/usr/lib/python3.9/imaplib.py", line 1230, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib/python3.9/imaplib.py", line 987, in _command
    arg = bytes(arg, self._encoding)
UnicodeEncodeError: 'ascii' codec can't encode character '\xa3' in position 12: ordinal not in range(128)

Unsure how to fix, seems to be an issue with imaplib and what it is being passed. Research suggests that it may be that imaplib is expecting a series of UTF-8 bytes instead of a raw unicode string, but I'm far from knowledgeable on this.

Can't run on python 3.9.5 windows

I have no experience with python so perhaps I'm making a rookie mistake, but since the project says it has 0 dependencies I wouldn't know what it could be.

I think the following output from my terminal says it all:

PS D:\Downloads\Edge\imapbackup-master> python
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

PS D:\Downloads\Edge\imapbackup-master> python imapbackup38.py
Traceback (most recent call last):
  File "D:\Downloads\Edge\imapbackup-master\imapbackup38.py", line 58, in <module>
    from six import string_types
ModuleNotFoundError: No module named 'six'

The same thing happened with python 3.8.5. Any tips, please?

UTF8 issue with folders?

It seems I get Entw&APw-rfe instead of Entwürfe in my case, but the folder is definitely shown as Entwürfe in my webmail client, Thunderbird and so on.

I am using imapbackup38.py (if this information is relevant).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.