Fetch all Mail from an IMAP server with Postfix to distribute on local IMAP server

dovecotemailimappostfixsmtp

The situation:

The constellation: We have a managed, dedicated server from a professional provider. We currently have Tobit David.fx server in use within our local network which fetches all E-Mails from the online server via IMAP which are sent to the domain. We would like to replace David.fx with an Ubuntu server which runs Postfix and Dovecot on it.

Our desired system would look like this:

Our users access their mailboxes via an ordinary client (such as Thunderbird) which connects to and fetches the mails for the configured address from our server within our local network.

That local server should do nothing else than fetch all the E-Mail from the IMAP mailbox online and put them in the appropriate mailbox on itself.

Outgoing mail should be sent like that, too, only the other way around. Clients use their mail program which is configured to send via SMTP on the local network server. That server actually does not sent the mails by itself but uses the online server to sent them.

So like if the online server would act like kind of a proxy or tunnel. Setting up forwarding on the online server to our local mail server is not an option.

Am I right to investigate further how to accomplish this with Postfix and Dovecot based on the described situation above? Are they suitable for such scenario? I find a vast amount of resources to read, but because of the same terminology mostly different problems are described in.

PS: I wanted to provide a sketch of it, but because of lacking reputation on ServerFault I could not do yet.

Best Answer

Here the proposed mail-flow-diagram for your case.

External mail server
   |        ▲
   |        |
  POP3     SMTP
   |        |
   ▼        |
Local mail server

First, you need setup working local mail server with postfix and dovecot. The important parameter for postfix is never list your local domain in relay_domains. You can use virtual_mailbox_domains or mydestination for local domain.

Second, you need to populate/synchronize the user from external mail server to local. So, external mail server has the valid recipient list for receiving email and local mail server has user database for authentication and management.

For outgoing email flow, just setup SMTP submission service with SASL. Then setup relayhost in postfix to your external mail server. You should also setup postfix SMTP client with single username or other method. It would depends on your provider mail server.

For incoming email flow, you should use cron/other daemon method to periodically check INBOX in external mail server, download new messages via POP3 (don't forget to delete it after download) and put it in the right mailbox. Fetchmail will be suitable for this task.