# IMAP Email Migration — SiteGround → MXroute

## Source Server Quirks (SiteGround)
- IMAP host: `c1113726.sgvps.net:993`
- Folder separator is `.` (not `/`)
- Folders are subfolders of INBOX (e.g. `INBOX.Trash`, `INBOX.Sent Messages`, `INBOX.Deleted Messages`)
- The LIST response format is nonstandard: `(\HasChildren) "." INBOX` — folder names often appear as `.` 
- Folder names with spaces use quotes in SELECT: `select('"INBOX.Sent Messages"')`
- Explicit folder names to check: `INBOX`, `INBOX.Trash`, `INBOX.spam`, `INBOX.Junk`, `INBOX.Drafts`, `INBOX.Sent`, `INBOX.Sent Messages`, `INBOX.Deleted Messages`, `INBOX.Archive`
- Do NOT rely on `LIST` response parsing to discover all folders — the SiteGround/CoreMail server returns many `.` (root) entries that mask subfolders. Use a hardcoded list of known folder names and test each with SELECT.

## Migration Pattern
```python
old = imaplib.IMAP4_SSL(OLD_HOST, 993)
old.login(email, old_pw)
new = imaplib.IMAP4_SSL(NEW_HOST, 993)
new.login(email, new_pw)

# Hard-code folder mappings
FOLDERS = [
    ("INBOX", "INBOX"),
    ("INBOX.Deleted Messages", "Trash"),
    ("INBOX.Sent Messages", "Sent"),
    ("INBOX.Sent", "Sent"),
    ("INBOX.Trash", "Trash"),
    ("INBOX.Drafts", "Drafts"),
    ("INBOX.spam", "INBOX.spam"),
    ("INBOX.Junk", "Junk"),
    ("INBOX.Archive", "Archive"),
]

for src, dst in FOLDERS:
    old.select('"' + src + '"')
    # Search ALL, fetch each UID with (RFC822), append to destination
    # new.append(folder, flags, date_time, email_bytes)
```

## After Migration Checklist
- DNS: MX records must point to new host. Verify with `dig +short MX domain.com`
- SPF: Must include new mail provider. Typical MXroute SPF: `v=spf1 include:mxroute.com -all`
- Test both IMAP login and send capabilities before removing old provider
- If old provider had SMTP as part of its plan, the SMTP sends may stop immediately on migration even though IMAP continues working during overlap

## IMAP-to-IMAP Copy Pitfalls
- COPY command only works between folders on the same server. Use FETCH + APPEND to cross servers.
- Append preserves email content but may not preserve flags (read/unread). Read emails will appear as unread on the new server.
- Use `uid` for FETCH to avoid race conditions with concurrent incoming mail.
- Set a reasonable timeout (30s+ per FETCH for large messages). Batch in small groups for progress visibility.
