getmail with imap idle as a systemd service
My mail provider strato runs an imap service but the space is too small. That's why I host my own dovecot server on ubuntu 18.04. Fetching mail into dovecot happens pretty much instantaneous, thanks to the following system service.
To fetch new mail from the provider, I have getmail configured to stay connected to the imap mailbox and wait for new mail to arrive. The provider supports the imap idle command.
This is my getmail configuration
[retriever] type = SimpleIMAPSSLRetriever server = imap.strato.de #port = 995 username = firstname.lastname@example.org password = xxxxx [destination] type = Maildir path = /home/me/Maildir/ user = me filemode = 0600 [options] delete = true read_all = true received = true delivered_to = false #verbose = 2
This file contains your password. You should
chmod go-rwx /home/me/.getmail/rcfile so nobody can see it but you and root.
Try it out
When I run
$ /usr/bin/getmail --rcfile=/home/me/.getmail/rcfile --idle INBOX
Getmail will take all mail from my inbox on the provider side and put them into my own dovecot mailbox. Thanks to the
--idle INBOX parameter, it will keep waiting for new mail. This way, I get it almost instantly without using up a lot of resources.
To fully automate this and have it run in the background, we need a few lines of systemd configuration. Of course you can pick any other name for your service
[Unit] Description=Getmail Wants=network-online.target After=network-online.target [Service] User=me Type=simple ExecStart=/usr/bin/getmail --rcfile=/home/me/.getmail/rcfile --idle INBOX Restart=always RestartSec=30 [Install] WantedBy=multi-user.target
The restart part is in case getmail stops waiting. Then the service will be restarted after 30 seconds. So far this runs well. Systemd might give up restarting after so many futile attempts. That's why I put 30 seconds of delay in. Just in case there is an issue with the internet connection or with strato.
Now we have a service that we can control like other daemons.
$ service getmail_me start $ service getmail_me status ● getmail_me.service - Getmail Loaded: loaded (/lib/systemd/system/getmail_me.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2018-12-15 19:07:19 UTC; 1s ago Main PID: 18173 (getmail) Tasks: 1 (limit: 4915) CGroup: /system.slice/getmail_me.service └─18173 /usr/bin/python2 /usr/bin/getmail --rcfile=/home/me/.getmail/rcfile --idle INBOX Dez 15 19:07:19 linuxserver2 systemd: Started Getmail.
Have it start at boot time
This command will enable this service so it fires when you boot your system.
$ systemctl enable getmail_me.service Created symlink /etc/systemd/system/multi-user.target.wants/getmail_me.service → /lib/systemd/system/getmail_me.service.
For multiple users
If you have more than one user, you could simply copy and adjust the above systemd service, or you can write one with parameters.
[Unit] Description=Getmail User %I Wants=network-online.target After=network-online.target [Service] User=%i Type=simple ExecStart=/usr/bin/getmail --rcfile=/home/%i/.getmail/rcfile --idle INBOX Restart=always RestartSec=30 [Install] WantedBy=multi-user.target
@ in the filename and the
%I in the script.
Now, you can run commands like
$ systemctl enable email@example.com $ systemctl enable firstname.lastname@example.org $ systemctl enable email@example.com $ systemctl status firstname.lastname@example.org
and the corresponding getmail daemons get started. Of course, you still need to set up the rcfiles with the passwords in the home directories of your users for this to work.