The good old fetchmail is probably what you're looking for. Run your local/self-hosted email server and then use fetchmail as described here to fetch the email from the email provider and deliver into the local accounts. You also have getmail (does the same but is written in python), guide here, or go-getmail ...
Alternatively, and probably way better:
Postfix has a feature called ETRN service, documented here. It can be used to incoming emails queued deliver it to another server when a connection is available:
The SMTP ETRN command was designed for sites that have intermittent Internet connectivity. With ETRN, a site can tell the mail server of its provider to "Please deliver all my mail now". The SMTP server searches the queue for mail to the customer, and delivers that mail by connecting to the customer's SMTP server.
From what I know about it you might be able to:
- Configure just a SMTP/Postfix server on the cloud provider;
- Configure a full IMAP/SMTP server on the self-hosted / local machine;
- Configure the "cloud" Postfix to deliver all incoming email into your local / self-hosted Postfix using
relay_domains
here and here. - Setup ETRN in the "cloud" provider to deal with your local server being offline / unavailable;
- On the local machine create a simple bash script + systemd timer / cron like this:
nc -c 'echo "ehlo selfhosted.example.org";sleep 1;echo "etrn your-domain.example.org";sleep 1;echo "quit"' remote-cloud-server.example.org 25
This command will connect to the cloud server and ask it to deliver all queued email to the self-hosted instance. This can be setup to run every x minutes, or if you want to get fancy, when the network goes up with the network-online.target
target like described here. Note that the script isn't strictly necessary, is just guarantees that if the connection between servers goes down when it comes back you'll get all the queued email delivered right away.
The following links may also be of interest so your local / self-hosted email server can send email:
- http://www.postfix.org/STANDARD_CONFIGURATION_README.html#dialup
- https://linuxhint.com/configuring_postfix_relayhost/
- https://www.cyberciti.biz/faq/how-to-configure-postfix-relayhost-smarthost-to-send-email-using-an-external-smptd/
Now a note about NextCloud: their webmail is the worst possible solution, I wrote very detailed description of the issues here. Do yourself a favor and use Roundcube.