You should selfhost your mail (in 2026)

You should selfhost your mail (in 2026)

你应该在 2026 年自托管你的电子邮件

If you have spent a few days online in self-hosting or data-sovereignty communities there is one advice that always pops up: You can self-host anything but not your e-mail server! I do understand where they are coming from since there are horror stories around about blocklists and delivery problems but today I want to show you that it is in fact possible in 2026 and you might even be able to host it from home! 如果你在自托管或数据主权社区待过几天,总会听到一条建议:你可以自托管任何东西,唯独不能自托管邮件服务器!我理解这种观点的由来,因为关于黑名单和投递问题的恐怖故事比比皆是,但今天我想向你证明,在 2026 年这确实是可行的,甚至你可能直接在家中就能完成托管!

By design email is an open system and we should not accept that a few big corporations have all our mail and the internet to become less decentralized in the process. Since my small MSP company helps people and corporations on a daily basis to switch from Gmail and Microsoft to cloud alternatives or self-hosted solutions I thought I should put my money where my mouth is and also move away from Google Workspace. 从设计上讲,电子邮件是一个开放系统,我们不应接受少数几家大公司垄断我们所有的邮件,也不应接受互联网因此变得越来越中心化。由于我的小型 MSP(托管服务提供商)公司每天都在帮助个人和企业从 Gmail 和 Microsoft 迁移到云替代方案或自托管解决方案,我认为我应该言行一致,亲自从 Google Workspace 迁移出来。

At home or on a VPS? The small subset of self-hosters that are willing to self-host their mailserver is again divided between those who dare to host their mailservers at home and those, who will only use a VPS. Usually I’d suggest using a VPS but if your home internet checks these boxes, you can 100% host your own mailserver at home: 在家托管还是用 VPS?愿意自托管邮件服务器的少数群体中,又分为敢于在家托管的人和只愿意使用 VPS 的人。通常我会建议使用 VPS,但如果你的家庭网络满足以下条件,你完全可以在家托管自己的邮件服务器:

  • Static IPv4 address (make sure it’s not blacklisted)
  • Not behind a CGNAT
  • Ability to change the PTR record of your IP (usually though the support of your ISP)
  • Ability to open common mailserver Ports (25,143,465,587,993)
  • 拥有静态 IPv4 地址(确保未被列入黑名单)
  • 不在 CGNAT(运营商级 NAT)之后
  • 能够更改 IP 的 PTR 记录(通常通过 ISP 的支持)
  • 能够开放常见的邮件服务器端口(25, 143, 465, 587, 993)

If you checked all of these, then you are ready to host at home! But what about internet outages? If my modem goes down I’ll lose incoming mails, right? Depends on how long your outage is but usually: No! EMails are very robust as in if your mail server is down, the sender will retry to deliver the mail because in the early internet, outages were plentiful. If your internet is down less than 40% on any given day, it will still work fine. 如果你满足以上所有条件,那么你就可以在家托管了!但如果断网了怎么办?如果我的调制解调器坏了,我会丢失收到的邮件吗?这取决于你的断网时长,但通常来说:不会!电子邮件非常稳健,如果你的邮件服务器宕机,发送方会重试投递,因为在互联网早期,网络中断是常态。只要你每天的断网时间不超过 40%,它依然能正常工作。

What mailserver software should I use? So this depends on your needs really. There are multiple mailserver solutions out there with different ways to host it. A good starting point would be docker-mailserver which is a full mailserver suite deployable via Docker with sane defaults. But there are other solutions out there like: Stalwart, Mailcow. Or if you’re a purist you can set up your whole server yourself. Personally my mail is on my ISPConfig Server which I have maintained for years for legacy reasons. If I would set up a mail server from scratch today I’d go for docker-mailserver. 我应该使用什么邮件服务器软件?这实际上取决于你的需求。市面上有多种邮件服务器解决方案,托管方式各不相同。一个很好的起点是 docker-mailserver,它是一个可以通过 Docker 部署、配置合理的完整邮件服务器套件。当然还有其他解决方案,如 Stalwart 或 Mailcow。如果你是纯粹主义者,也可以完全手动配置服务器。就我个人而言,我的邮件托管在 ISPConfig 服务器上,出于历史原因,我已经维护它多年了。如果今天我要从零开始搭建邮件服务器,我会选择 docker-mailserver

What about my domain? As with your IP you should check if your domain is listed on any spam list but you also need a few DNS records so you don’t run into delivery troubles. Your Mailserver software will probably guide you through all that but just for completeness you’ll need these records: 关于域名呢?和 IP 一样,你应该检查你的域名是否被列入任何垃圾邮件黑名单,同时你还需要配置一些 DNS 记录,以免遇到投递问题。你的邮件服务器软件可能会引导你完成这些设置,但为了完整起见,你需要以下记录:

  • SPF: The Sender Policy framework defines which servers can send from your Domain. Usually something like v=spf1 mx a ~all is good enough.
  • DKIM: This is a cryptographic signature your email server adds to the mail headers of every sent mail. Your domain needs to have a specific entry but the name and value will be communicated by your mailserver.
  • DMARC: This entry is basically an extension to SPF and DKIM and should prevent email spoofing of your domain. If you are unsure what to put in here, use a DMARC generator.
  • MX: The MX records tell other mailservers where to deliver the emails on your domain.
  • SPF: 发送方策略框架,定义了哪些服务器可以代表你的域名发送邮件。通常 v=spf1 mx a ~all 就足够了。
  • DKIM: 这是你的邮件服务器添加到每封外发邮件头部的加密签名。你的域名需要有一个特定的条目,其名称和值由你的邮件服务器提供。
  • DMARC: 该条目基本上是 SPF 和 DKIM 的扩展,旨在防止你的域名被冒用。如果你不确定如何填写,可以使用 DMARC 生成器。
  • MX: MX 记录告诉其他邮件服务器将邮件投递到你域名的何处。

Also as mentioned before the IP address you use for your mailserver should have a PTR Record which can only be set by your ISP or the VPS provider. It allows the IP of the server to resolve to a domain (usually it’s the other way around) and it must show to the hostname you use for your mailserver. eg mail.yourdomain.com. 此外,如前所述,你用于邮件服务器的 IP 地址应该有一个 PTR 记录,这只能由你的 ISP 或 VPS 提供商设置。它允许服务器的 IP 解析为一个域名(通常情况是反过来的),并且它必须指向你用于邮件服务器的主机名,例如 mail.yourdomain.com

After you have set up your domain and server I highly recommend to test mail delivery with https://www.mail-tester.com/ which checks if all the records are correct and your server is behaving the way it should. That tool saved me lots of times. 在设置好域名和服务器后,我强烈建议使用 https://www.mail-tester.com/ 测试邮件投递情况,它会检查所有记录是否正确,以及你的服务器行为是否符合预期。这个工具曾多次拯救过我。

What about spam? Here comes the fun part! Spam used to be the biggest issue with self-hosted mailservers. It was so bad that I’d say it was the biggest reason people moved away from self-hosted or even (externally) hosted webmail. But something has changed in the last 2 years that solved the spam problem for self-hosters and allowed us to host our own mailservers again: Using local LLMs to fight the spam. 关于垃圾邮件呢?有趣的部分来了!垃圾邮件曾经是自托管邮件服务器最大的问题。它糟糕到我认为这是人们放弃自托管甚至放弃(外部)托管网页邮箱的主要原因。但在过去两年里,情况发生了变化,解决了自托管者的垃圾邮件问题,让我们能够再次托管自己的邮件服务器:使用本地大语言模型(LLM)来对抗垃圾邮件。

My selfhosted antispam service of choice is rspamd. It starts out as any other antispam system by having blacklists, ip checks, dns checks and keyword lookouts but there is one plugin in particular that completely fixed my spam problem: The GPT plugin. This little plugin allows you to classify an email using a large language model to determine if a mail is spam or not. Since we’re self-hosters and we do this also for privacy it would not make any sense to use an external LLM API to analyze all of our private emails - this is where local models come in. My model of choice for email classification is Gemma 4 12B QAT which can run on a GPU or on CPU with as little as 7GB of RAM or VRAM and is a very capable multilingual model. 我选择的自托管反垃圾邮件服务是 rspamd。它起初和其他反垃圾邮件系统一样,拥有黑名单、IP 检查、DNS 检查和关键词查找功能,但有一个插件彻底解决了我的垃圾邮件问题:GPT 插件。这个小插件允许你使用大语言模型对邮件进行分类,以确定其是否为垃圾邮件。既然我们是自托管者,且出于隐私考虑,使用外部 LLM API 来分析我们所有的私人邮件是毫无意义的——这就是本地模型发挥作用的地方。我用于邮件分类的首选模型是 Gemma 4 12B QAT,它可以在 GPU 或 CPU 上运行,仅需 7GB 的内存或显存,是一个非常强大的多语言模型。