The naming conventions of Maildirs seem pretty elaborate. I downloaded a message from Outlook's web interface and added it to a Maildir by naming it added and putting it in a new folder. mbsync and mu4e seemed happy enough with the result, but I wouldn't be surprised if I ran into trouble by making a habit out of this. Is there a program to add a file to a Maildir in the right way? Ideally such an operation wouldn't require configuring and running a full-blown mail server on my machine.
- 135
2 Answers
What you're looking for is a mail delivery agent ("MDA") with maildir support. There are a couple of programs that fit the bill:
procmailcan do this, of course, but I don't think it's maintained anymore. It may still be packaged for whatever distribution you're using.maildropfrom the courier-mta mail server can deliver mail to maildir format.
Most mail servers have support for delivery to maildir format mailboxes, but it's not usually split out in such a way that you can use it outside the context of the mailserver.
Wikipedia lists a few more.
Looking at the man pages for the two programs I mentioned here, in both cases you have to provide them with a filter file telling them where to deliver messages. With procmail you can embed that on the command line; assuming that I wanted to deliver a message on stdin to a Maildir format mailbox named mail in my current directory, I could run:
procmail -m <(printf ":0\n$PWD/mail/\n") < msg
Using maildrop I would need to pass it an actual file (it checks and won't run if the filter file is not a regular file):
echo "to $PWD/mail" > filter
maildrop filter < msg
- 34,737
I confirm @larsks's answer about procmail and maildrop limitations: both tend to require some shell-based wrapping. Additionally, both come with heavy filtering features that are irrelevant when the target maildir is known already.
But it is worth mentioning two other tools: safecat and mdeliver.
safecat is probably THE "program to add a file to a Maildir in the right way" since it "implements Professor Daniel Bernstein's maildir algorithm". That 2003 algorithm was later said to be too complex and too paranoid, but as far as I know, it still works.
safecat suffers from two caveats:
- one must mention both
maildir/tmpandmaildir/newdirectories on the command-line, which is useful but feels redundant. - those directories must exist before running the command.
mdeliver (found e.g. in the mblaze Debian package) is very straightforward: mdeliver /path/to/maildir < msg.
Like safecat, mdeliver expects the directories to exist.
In the end, it seems anyone looking for a simple, lightweight, no-filtering, no-configuration Mail Delivery Agent that drops emails in a Maildir directory and creates it if necessary is bound to write a shell wrapper around one of these tools.
- 31
procmailas described in the other answer has appeared to work well, but your suggestions will be helpful for whenprocmailinevitably stops working in the future due to lack of maintenance. – Kodiologist Dec 03 '23 at 18:58