Add STARTTLS modes, source-mailbox flag, IMAP IDLE for destination, progress messages, and more error handling #13
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
First off, @Schluggi this is awesome. I was looking for a way to migrate email from one IMAP server to another IMAP server, and this is exactly what I was looking for! Awesome stuff.
I ran into a couple of problems while using it, mostly because of either large email mailboxes, weird email messages that seemed to be missing important headers, and oddities from the IMAP servers themselves (like a disconnect on a bad append attempt 🤷♂️)
So I added some features/fixes! Please see the included code here for:
The addition of a
--source-mailbox
flag to target specific mailboxes to sync from the source to the destination. This allowed me "slow roll" a particularly large sync, as well as debug certain folder issues quickly. It can be specified multiple times to sync any number of source mailboxes.Addition of STARTTLS support. I had to add this in order to sync to a server that only exposed port 143, and also ensure I still used an encrypted connection. If the flag
--source-use-starttls
and/or--destination-use-starttls
is specified a connection will be established then STARTTLS issued prior to any login attempt. If STARTTLS fails, an exit will occur.Addition of IMAP IDLE to the destination connection if
--destination-use-idle
is added to the command line. This will invoke IMAP IDLE after the initial connection and login check to keep the connection alive until the script is ready to copy emails over.Unfortunately, IMAP IDLE wasn't enough for my case (connection timeout when transferring to Gmail due to a massive source mailbox), so I added a re-connect to the destination after the looping over the source mailbox was completed. It feels a bit hacky, and could be cleaned up a bit by wrapping it in an if-statement I suppose, but it worked perfectly.
I also added in a couple more print messages to output status more regularly + tweaked error reporting a bit. Transferring a mailbox of 250k+ emails took a long time so it was helpful to see the folder + email count it was at during the source loop. I also had a couple errors that were tricky to debug and the added logging is for that.
I discovered that Gmail aborts the whole connection if an email transfer fails, so I added a re-connect in the error handling code as well. This could be wrapped in an if statement as well (check for connection loss first) I suppose, but it worked for me.
Finally - I updated the email copy success check to also looking for a message of
"(success)"
as that appears to be what Gmail returns instead of the"append completed"
that the code originally only looked for.All in all, a HUGE thank you for writing and open sourcing this, it was really helpful! One drawback I should acknowledge in my PR here is that my edits have made the screen output a bit more verbose.. This was very helpful for me, and hopefully it's helpful for others too, but maybe a log-level flag in the future? 🤔
Let me know what you think of the changes here?
Thanks again.