0

I am learning how to use procmail but at this point, I am not even sure it's the right tool for what I am trying to do.

So far, I have managed to get fetchmail to retrieve emails from a Google IMAP account and procmail to filter those emails into local folders I had previously created.

I am wondering though whether there is a way to get procmail to automatically create a new folder locally when an email from a new sender is being retrieved and to store that email into that folder.

So far, I have only found a website that describes the possibility of procmail creating automatically folders for mailing lists, but the recipe is something crazy using characters which I have no idea the meaning of, furthermore the official procmail website seems unreachable.

Please can you help? Thank you.

someuser
  • 13
  • 5
  • The Procmail website doesn't host a lot of documentation; you should be able to find the manual pages on many other sites. Maybe also try http://www.iki.fi/era/procmail/quickref.html – tripleee May 24 '18 at 16:01

1 Answers1

0

It's not clear what you expect the folder to be called, and what mailbox format you're using; but assuming maildir folders named by the sender's email terminus, try

Who=`formail -rtzxTo:`
:0
* ? mkdir -p "$Who"
$Who/

For an mbox folder, you don't need the directory check at all, because the folder is just a single text file, and you'd drop the final slash from the folder name. Mbox needs locking, so add a second colon after the zero.

Who=`formail -rtzxTo:`
:0:
$Who

Getting formail to create a reply and then extracting the To: header of the generated reply is a standard but slightly unobvious way to obtain just the email terminus for the sender of the input message.

The shell snippet mkdir -p dir creates dir if it doesn't already exist, and is a harmless no-op otherwise.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thank you @tripleee for your answer. I expect the folders to be called after the name of each sender. Say sender1 and sender2 (with whom I have never had any correspondence before) send emails to my Gmail account. I would like fetchmail to retrieve those emails and procmail to created the folders sender1/ and sender2/ and distribute the emails into those two forlders. I think the format of my mailboxes is mbox; if that's the case, does your recipe differ? What do you mean by the terms "email terminus"? – someuser Jun 06 '18 at 14:42
  • If you see `From: Sender McMailface ` the email terminus is the machine-readable part `sender1@example.net` – tripleee Jun 06 '18 at 14:53
  • Updated the answer with an mbox variant. – tripleee Jun 06 '18 at 15:00
  • You say that in the mbox case I don't need the directory check. Does that mean that even the mkdir command disappears (as you clearly show) and so there is no condition line in this recipe? – someuser Jun 06 '18 at 15:22
  • Yeah, exactly. Delivery is unconditional. – tripleee Jun 07 '18 at 04:19
  • The `formail` code is explained in some more detail in my answer to [your other question](https://stackoverflow.com/questions/50889697/configure-procmail-to-match-an-external-email-address-list-when-filtering-emails) – tripleee Jun 20 '18 at 17:18