My mail setup is perhaps a bit unusual:
- I like to read and send all my email via gmail
- I also want to keep all received and sent mail on my personal mail server
The final destination of all my email addresses is my personal mail server and a copy of every received mails is sent to gmail.
This setup has some technical challenges:
- Newer Exchange installations do typically not allow forwarding
- Newer Exchange installations do typically not serve pop, only imap
- Gmail cannot pull via imap
Enter getmail. Getmail is a neat little piece of software: It retrieves mail from remote mail servers via imap or pop and delivers either to a local mail store (mdir, mbox), via an external mail delivery agent (maildrop, procmail, sendmail) or to qmail.
There are lots of tutorials available on how to configure getmail, to achieve what I do:
- Pull mail from Exchange and inject into a local mbox plus gmail
- Pull mail from Zimbra and inject into a local mbox
The problem I’m going to solve in this post is that gmail times out when downloading very large amounts of mail – and apparently it does so by simply hanging up. This causes getmail to time out and exit uncleanly – and not writing to its database how far it got. When running getmail again, with
read_all = 0, it starts all over.
The solution is to send a sigint, not a sighup, to getmail before gmail hangs up, and run getmail again and again until it finishes. Here are two small scripts that do just that:
while true; do
getgmail.sh | grep -r '^ 0 messages (0 bytes) retrieved,' && break
timeout -2 120s getmail --rcfile=getmailrc.gmail