Postfix/Dovecot: Sorting incoming email with "address extension" ("user+folder@host") into IMAP folders - how to?

5 posts / 0 new
Last post
#1 Mon, 05/16/2011 - 11:34
Locutus

Postfix/Dovecot: Sorting incoming email with "address extension" ("user+folder@host") into IMAP folders - how to?

Hey!

Is there a way to instruct Postfix/Dovecot via Virtualmin/Webmin to auto-sort incoming email with a recipient of user+folder@example.com (apparently called "address extension" in Postfix lingo) into the IMAP "folder" of "user"?

I know this is somehow possible, since we use that setup at my University. But I can't find out how to configure that. Thanks in advance for help!

Regards, Frank

Mon, 05/16/2011 - 13:37
andreychek

Howdy,

Hmm, I don't know of a simple way to do that :-)

I'm sure it's possible, but I'm not sure of a way to accomplish that without using fancy procmail rules to interpret the username and folder.

However, you could do just that -- in /etc/procmailrc, you could develop a rule to test on the username, and if it's in the format user+folder@domain.tld, to pull out the user and the folder, and deliver to the proper user and location.

-Eric

Mon, 05/16/2011 - 13:48
Locutus

Ooh, okay I thought there was an easy Postfix option to do that. I'll do some net digging, or might ask around at the admins at university how they configured this stuff. :)

How would I instruct Procmail to "deliver to the proper user and location"? As in, how can I access IMAP folders there? As far as I see it, "IMAP folders" are simply subdirectories of the user's Maildir, right?

Mon, 05/16/2011 - 13:57
andreychek

Howdy,

It's possible there's some super-simple way of handling that which I hadn't run into before :-)

There could certainly be a tool or app out there for solving exactly that problem.

As far as instructing procmail how to do it -- you'd do that through the construction of some very fancy procmail rules :-)

You'd have to build some regular expressions to test on the email address, and if it's in the right format, you'd have that regular expression pull the username and foldername.

There's a number of examples in the "procmailex" manpage that might help you get started.

-Eric

Mon, 05/16/2011 - 15:37
Locutus

Thanks for your insight Eric! :)

I did some googling and experimentation, and found out a way to do this, with Procmail indeed.

Nice thing is: No "fancy rules" to pull the username and foldername from the address are required, because Postfix already supplies the extension, i.e. everything after the (configurable) character "+", in an environment variable named $EXTENSION when calling external programs for local delivery.

So, first Postfix needs to be reconfigured to pass the extension in a variable to Procmail. That's done by changing the mailbox delivery command to:

/usr/bin/procmail-wrapper -o -a $DOMAIN -a "$EXTENSION" -d $LOGNAME

Thus, $DOMAIN will be passed as $1 in .procmailrc (although I think this variable isn't even used right now, at least I could find no reference to it in Virtualmin's Procmail config) and $EXTENSION will become $2.

IMPORTANT! The extension needs to be put in quotes, because it can be empty, and that would break the command like ("-a -a -d ..." which causes Procmail to fail).

CAVEAT and BUG in Webmin: The Procmail module will remove the quotes in that command line when I enter it through the Local Delivery input mask; I had to directly edit the /etc/postfix/main.cf file!

Then I put this in the user's .procmailrc:

EXT=$2
:0
* EXT ?? ^^test1^^|^^test2^^
$HOME/Maildir/.$EXT/

The first line unconditionally puts the second passed parameter into the local Procmail variable "EXT". Needs to be done, cause you can't access the $2 directly. Then it will do a regular expression check if the extension is "test1" or "test2" (don't want email senders to be able to create arbitrary IMAP folders for the user! :) ). If so, the mail is filed into the subdirectory .$EXT of the user's Maildir.

Have not yet tried IMAP subfolders. Might work if you use the dot character that separates the subfolder directory names in the email address, like "user+folder.subfolder@domain".

Then, the folder names must be all lowercase, because Postfix seems to have a bug which causes the extension text to be converted to lowercase when it is applied to the virtual domain mapping table.

Topic locked