Selfhosted email (sort of)

Hi!

I’d like to centralize my email handling. At the moment I’ve multiple accounts at different providers and configured one client to use POP3 to download copies which are then backed up. All other clients use IMAP and each client is configured with each external account. The emails at the external services are deleted every 14 days.

Now I’d like to host an internal service that fetches mail from all external providers and rehosts via IMAP. Ideally, the local IMAP would only require one account, but I don’t if/how that would work when sending. I think I’m mainly looking for the correct terminology, as googling only seems to return the “full selfhosted mail server”, which I don’t want.
Any tips on the necessary stack are appreciated as well. A long time ago I had the receiving pipeline working using fetchmail, procmail and dovecot. But I don’t know if these tools are still recommended/necessary and I didn’t figure out sending(relaying?) off to the external provider.

Thanks!
D

Fetchmail is probably what you want in order to collect all the mail into a single account. From there, it gets a bit more difficult. If the providers are external providers, you’ll need to do mail routing to make sure that email from you@xyz.com goes through the correct server… but while I’ve done this sort of thing based on the address of the recipient, that’s not what you want; you’re looking to do routing based on the email address of the sender identity (which is ALSO an issue, since most mail clients will use the same FROM: regardless of who the message was originally addressed to).

You could use Roundcube for a nice friendly web-based mail client that allows you to use multiple identities on the same account (thereby letting you easily switch between replying from you@abc.tld and you@xyz.tld), but I’m not sure how to go from there to routing the mail appropriately.

I think you can do this with Postfix filters, something like the following…

/etc/postfix/main.cf

header_checks = regexp:/etc/postfix/header_checks
relayhost = 2.2.2.2

/etc/postfix/header_checks

/^From: you@abc.tld/ FILTER smtp:1.1.1.1

If I’m reading the docs right, this would normally relay outbound mail through 2.2.2.2, but relay it through 1.1.1.1 if the From: address in the header is you@abc.tld.

I’ve never actually done this type of filtering in practice, though, so I offer no warranties express or implied. If your server blows up or your cat gets pregnant, don’t look at me. :slight_smile:

Let me know how you get on with this, would you?

Because I’m a masochist I handle my own email - a 3-node cluster in cheap VPS’ … Fairly typical setup, exim4 (don’t like postfix), two dovecot instances with realtime sync, spamassassin, mariadb with galera (3 nodes) for userids, spamassassin data, clamav, nginx for roundcube, etc.

All mail is backed up at home via mbsync, triggered by a systemd timer every hour

[Unit]
Description=mbsync backup from galah

[Service]
Type=oneshot
ExecStartPre=/usr/bin/test -d /stuff/Backups/Mailsystems/mbsync-backup
ExecStartPre=/usr/bin/test -f /stuff/Backups/Mailsystems/.mbsyncrc
ExecStart=/usr/local/bin/mbsync --config /stuff/Backups/Mailsystems/.mbsyncrc backup
Restart=no
RestartSec=30

[Install]
WantedBy=default.target

The .mbsyncrc config file has several sections …

  • MaildirStore to define the local directory to store the mail
  • IMAPStore to define the remote that we pull from via imap
  • Several channel stanzas to define the folders to pull/ignore
  • Group that groups several channels into a handy single entry, in this case backup

Works well, hands-off once set up. Can point a maildir-aware client at the backup dir and it just works.

Here’s a sanitized version of my .mbsyncrc

# Global configuration section
#   Values here are used as defaults for any following Channel section that
#   doesn't specify them.
Expunge Slave
Create Slave
Sync Pull
CopyArrivalDate yes

#
# Local copies of mail
#
MaildirStore halfwalker
Path /stuff/Backups/Mailsystems/mbsync-backup/halfwalker/
Inbox /stuff/Backups/Mailsystems/mbsync-backup/halfwalker/INBOX
SubFolders Verbatim

#
# Connections to galah to get mail
#
IMAPStore halfmail
Host halfmail.example.com
port 993
SSLType IMAPS
CertificateFile ~/.mbsync/halfmail.crt
User halfwalker@example.com
Pass SooperSekritPass


#
# Pulling just INBOX
#
Channel halfwalker_INBOX
Master :halfmail:
Slave :halfwalker:
Patterns "INBOX"
MaxMessages 0
SyncState *

#
# Pulling the rest of the folders - INBOX and Spam excluded here
# because they're pulled in channels above
#
Channel halfwalker_ham
Master :halfmail:
Slave :halfwalker:
Patterns * !INBOX !Spam
MaxMessages 0
SyncState *


#
# Pull ALL mail with "mbsync backup"
#
Group backup
Channel halfwalker_INBOX
Channel halfwalker_ham

I am downloading email to Thunderbird then backing up from there. I also create local folders so that I can remove mail over 2 years old from email provider to reduce attack surface should my email be compromised.

I have been looking too for another solution. I would love a docker container with a CLI email catcher similar to Thunderbird accessible through web browser. But for now, I am using a desktop app to download and backup email.