Migration
Replace Gmail
Leaving Gmail is two separate decisions: leaving Google, and running a mail server. The first is easy and cheap. The second breaks in a way you cannot see.
Should you replace Gmail with a self-hosted mail server?
For most people, no. Buy your own domain and move to a paid mail provider instead: that gets you off Google, gets you an address nobody can take away, and keeps delivery working. Full self-hosting is the right answer only if you can open outbound port 25, set a PTR record matching your mail hostname, and accept that a delivery failure is usually silent. If you do self-host, relay outbound mail through an SMTP provider and keep receipt, filtering and storage on your own box.
Start here: you probably should not do this#
Every other migration here trades convenience for control, and the worst case is that something is annoying. Email is different: the worst case is that mail you send stops arriving, you never find out, and the damage is to relationships and invoices rather than to a service you can restart.
What you give up when you leave Gmail for a server you run:
- Delivery that just works. Google has spent twenty years building sending reputation. You start at zero on an IP address whose history you did not choose.
- Spam filtering at Google's scale. Rspamd is good. It is not trained on a billion mailboxes. Expect more spam in the inbox and, worse, occasional real mail in the spam folder.
- Instant search over 15 years and 200,000 messages. Dovecot with a full-text index gets close on a well-provisioned box. On a 2 GB VPS it does not.
- Recovery. If you lose the mailbox and the backup, nobody has a copy.
If your reason is privacy, note that most of your correspondence still passes through Gmail, because that is where the people you write to have their accounts. Self-hosting removes Google from one side of the conversation.
The failure mode: mail that stops arriving and never tells you#
This is the part that vendor-friendly guides skip, and it is the whole reason this page recommends against the obvious plan.
Port 25 is blocked. Your server needs outbound TCP 25 to talk to other mail servers directly. Nearly every residential ISP blocks it permanently, and several large cloud providers block it on new accounts and require a support request that is regularly refused. Port 587 to a relay does not substitute. Test before you buy anything:
nc -vz gmail-smtp-in.l.google.com 25PTR records are not yours to set. Reverse DNS lives with whoever owns the IP block, so it is a control panel field at Hetzner, OVH or DigitalOcean, and does not exist for a home connection at all. Gmail's sender guidelines require working forward and reverse DNS for sending IPs, and mailcow's docs require the PTR to match your mail hostname exactly, for IPv4 and IPv6 both. A mismatch is the most common reason a correct server gets rejected by Outlook.
SPF, DKIM and DMARC are table stakes. SPF says which hosts may send for your domain, DKIM signs each message with a key published in DNS, and DMARC says what receivers should do when the two disagree and gives you a reporting address. Publish all three before your first real message. DNS for self-hosters covers the layout.
And the failure is invisible. A receiving server is allowed to accept your message and then quietly file it in spam or drop it. No bounce comes back, and your sent folder is full of mail that looks delivered. People discover this three months later, from a client who says they replied to an email that never reached them. The only defenses are a DMARC rua address you read and a monthly test to accounts you control at Gmail, Outlook and Yahoo, checking the headers rather than the inbox.
The honest middle path#
For most people reading this, the right migration is not to a server in your house.
- Buy a domain you will keep, and pay for it a decade at a time. This is the entire point.
you@yourname.devis portable;you@gmail.comis Google's. - Point it at a paid mail provider. Fastmail, Migadu, Mailbox.org, Proton and Purelymail all cost less than a streaming subscription and none are advertising businesses. Delivery is their problem.
- Keep the exit open. Use IMAP, not a proprietary client, and take a local copy on a schedule so leaving is an
imapsyncrun rather than a negotiation.
That gets you off Google, gets you an address nobody can revoke, keeps mail arriving, and costs nothing in ongoing attention. When not to self-host makes the general version of this argument. If you skip the rest of this page, you have still won.
When full self-hosting is genuinely right#
It suits you if you have a VPS with controllable reverse DNS, outbound 25 open or a relay you are happy to pay for, you already run other services and back them up properly, and mail being down for a day would be irritating rather than expensive.
Do it with an outbound relay and stop treating that as a compromise. Send through Postmark, SES, Mailgun or your host's submission service on 587; receive, filter, store and search locally. You keep the domain, every mailbox, the archive, the spam policy and a twenty minute exit. You rent the one thing you cannot manufacture, which is reputation.
Getting your mail out of Gmail#
Do both of these. They serve different purposes.
Takeout, for the cold archive. At takeout.google.com, deselect everything, select Mail, and pick all mail or specific labels. You get mbox files (RFC 4155): plain text, messages concatenated, Gmail labels preserved as an X-Gmail-Labels header rather than as folders. Large accounts split into numbered archives and the links are time limited, so download them the day they land. Verify before you celebrate:
grep -c '^From ' All-mail-Including-Spam-and-Trash.mboxIMAP, for the live migration. Takeout flattens your folders; a server-to-server sync does not. Turn on two-factor authentication, generate an app password, then run imapsync:
imapsync \
--host1 imap.gmail.com --port1 993 --ssl1 \
--user1 you@gmail.com --password1 "$GMAIL_APP_PASSWORD" \
--host2 mail.example.org --port2 993 --ssl2 \
--user2 you@example.org --password2 "$NEW_PASSWORD" \
--exclude '^\[Gmail\]' --automap --skipcrossduplicatesExcluding [Gmail] skips All Mail, Important and Starred, which are views over messages you already copied and will otherwise duplicate everything. Google rate limits IMAP, so a 40 GB mailbox is a multi-day sync, not an evening. Run it, run it again, then look at DNS.
Contacts and calendar come out of Takeout too, as vCard and iCalendar. mailcow takes them through SOGo, Stalwart through CalDAV and CardDAV.
mailcow: dockerized
A complete mail server in 18 containers, with a UI that makes it survivable
3500 MB idle · ops load 4/5 · GPL-3.0 for mailcow's own code; bundled components keep their own licensesCommunicationStalwart
One Rust binary that is SMTP, IMAP, JMAP, CalDAV and CardDAV at the same time
150 MB idle · ops load 3/5 · AGPL-3.0 for the Community edition, dual licensed with the proprietary Stalwart Enterprise License for gated featuresWhich path for your situation#
| Your situation | Do this | Why |
|---|---|---|
| You want off Google, mail must work | Own domain, paid provider | All of the ownership, none of the deliverability risk |
| Home connection, dynamic IP | Do not self-host outbound | Port 25 is blocked and PTR does not exist |
| VPS with PTR, 6 GiB, want webmail and calendar | mailcow: dockerized | Best-documented stack, SOGo included, relay outbound at first |
| VPS with 1 to 2 GiB, comfortable with pre-1.0 | Stalwart | One Rust binary, 150 MB idle, JMAP and CalDAV built in |
| Small business, customers on the line | Paid provider | A silent delivery failure costs more than the hosting saves |
| Compliance requires messages stay on your hardware | Self-host with a relay | Storage and filtering local, sending reputation rented |
mailcow vs Stalwart decides between the two servers in detail.
What it costs and what breaks#
Cost. A VPS with usable reverse DNS is roughly 5 to 15 euros a month for mailcow's documented 6 GiB floor, or 4 to 6 for Stalwart. Add a relay and backup storage and call it 100 to 250 euros a year, against 30 to 60 for a paid provider. You are not saving money. mailcow scores 4 out of 5 on operational load and Stalwart 3, and neither number includes watching DNS, reputation and blocklists forever.
What breaks. A failed certificate renewal takes IMAP down on every phone in the house at once. A host distribution upgrade can install an MTA that steals port 25, which the mailcow docs call out for Debian 12 to 13 specifically. Your backup silently omits mailcow's crypt volume and restores unreadable ciphertext, or you rsync Stalwart's RocksDB store live and get a copy that will not open. Read Backups that actually restore and Backing up a running database before the first message arrives, and put a check on port 993 and your MX in Uptime Kuma.
The point of no return#
Everything above is reversible. Changing the MX record is not, in the sense that mail delivered to the new server after the switch exists nowhere else.
Sequence it like this:
- Stand the server up on
mail.yourdomain.tldwith the full DNS set: A, AAAA, MX, SPF, DKIM, DMARC with anruayou read, and a matching PTR. Pick that hostname carefully, because mailcow cannot change it later. - Drop the MX TTL to 300 a day before cutover so a mistake costs five minutes.
- Run
imapsyncto completion, twice. - Switch the MX. Send and receive test mail against Gmail, Outlook and Yahoo, and read the headers for
spf=pass,dkim=passanddmarc=pass. - Leave Gmail forwarding for at least 30 days and keep the account alive for a year. Then audit your password manager, Vaultwarden or otherwise, and move every account whose recovery address is still the Gmail one.
What to do next#
Run the port 25 test and check whether your provider lets you set reverse DNS. Fifteen minutes, and it decides this for you. If either fails, buy the domain, pick a paid provider, and call the project finished.
If both pass, read mailcow vs Stalwart to pick the software, then A security baseline for a home server before mail exists on the box. When you move it to different hardware later, Moving a service to a new machine applies with one addition: the DKIM keys and the PTR record move with the service, or nothing you send verifies. The rest of the Communication category covers the other Google services you are still using.
Questions#
Can I self-host email at home?
You can receive mail at home if your ISP passes inbound port 25 and you have a stable address, and plenty of people do exactly that. Sending is the problem. Residential ISPs almost universally block outbound 25, you cannot set reverse DNS for an address you do not own, and residential ranges are on blocklists by policy rather than by behavior. The workable home setup is inbound and storage local, outbound through an authenticated relay on port 587.
What format does Google Takeout export Gmail in?
mbox, the format defined in RFC 4155: one plain text file with every message concatenated and separated by From lines. Gmail's labels are preserved as an X-Gmail-Labels header on each message rather than as folders, so importing into a normal IMAP server flattens your organization unless you write a script. Takeout splits large exports into numbered archives and the download links are time limited, so start the download the day it is ready.
Will my email silently stop working?
That is the characteristic failure, yes. A receiving server can accept your message and then file it in spam or discard it, and neither action produces a bounce, so your sent folder looks perfect. You find out weeks later when someone mentions they never got your invoice. Publish a DMARC record with an rua address you actually read, and send test messages to accounts you control at Gmail, Outlook and Yahoo every month.
Is using an SMTP relay still self-hosting?
Yes, and it is what most working self-hosted mail actually does. You keep the domain, the mailboxes, the archive, the spam policy and the ability to change providers in twenty minutes. What you rent is sending reputation on somebody else's IP addresses, which is the one component you cannot build on a new address and cannot repair once it is damaged. Relays cost single-digit amounts per month at personal volumes.
How long should I keep the Gmail account after migrating?
At least a year. Takeout is a snapshot, not a service: anything that arrives after the export exists only in Gmail. Keep the account alive and forwarding, keep two-factor on it, and do not delete it while services you have forgotten about still use it for password resets. Audit your password manager for every account whose recovery address is the Gmail one before you touch the MX record.
What actually decides whether my mail gets delivered?
Reverse DNS matching your sending hostname, a valid SPF record, DKIM signatures that verify, a DMARC policy, and the accumulated reputation of your sending IP. Gmail's sender guidelines require SPF or DKIM and working forward and reverse DNS for every sender, not just bulk ones. The first four are configuration you can get right in an afternoon. The fifth is time, and there is no way to buy it or appeal it.
Sources#
- Google Takeout, export your Google data
- Google Account Help, how to download your Google data
- Gmail Help, email sender guidelines (SPF, DKIM, DMARC, PTR)
- Gmail Help, add Gmail to another email client (IMAP settings)
- RFC 4155, the application/mbox media type
- RFC 7489, DMARC
- mailcow docs, DNS prerequisites including PTR
- mailcow docs, system prerequisites and required ports
- Stalwart Docker installation, ports and volumes
Published . Last reviewed . Found something out of date? Tell us and we will fix it and log the change.