We had this tool at my old job where we would enter in a source and target imap account and it would transfer all emails over imap.
I never got the name of the app but it was windows-only anyway. Not very scriptable. We do have imapsync, which is phenomenal, however I want to do my own. Also, imapsync source code is weird.
Anyway. Here's my python version.