1

Topic: User aliases with regular expressions

======== Required information ====
- iRedMail version: 0.9.0
- Store mail accounts in which backend (LDAP/MySQL/PGSQL): MySQL
- Web server (Apache or Nginx): Apache
- Linux/BSD distribution name and version: Ubuntu 14.04.02 (Lubuntu on Banana Pi)
- Related log if you're reporting an issue:
====

I've seen several queries about email address aliases based on a regular expression several times, but no real answer other than "create an alias for each possible address" which won't work for me.

What I needed was for anything that started with, for example, "foo." to be redirected to "foo@example.com".  So if I wanted to sign up at, say, Costco, with an email address, I could use "foo.costco@example.com" and it would go to my real address of "foo@example.com"

I made some simple configuration changes to make this work:

1) First I added an "isregex" column to the alias table:  ALTER TABLE alias ADD COLUMN isregex TINYINT(1) NOT NULL DEFAULT 0;

2) Next I created a new file, /etc/postfix/mysql/virtual_alias_maps_regexp.cf.  Most of this is the same as all the other files in the directory.  The difference is the "query" line:

query = SELECT alias.goto from alias,domain WHERE '%s' REGEXP CONCAT('^',alias.address,'$') AND SUBSTRING(alias.address,1,1) != '@' AND alias.isregex = 1 AND alias.domain='%d' AND alias.domain=domain.domain AND alias.active=1 and domain.backupmx=0 domain.active=1

3) add a reference to the above created file to the virtual_alias_maps entry in /etc/postfix/main.cf file.  I added it between the entries for "virtual_alias_maps.cf" and "domain_alias_maps.cf".  The part I added was:

proxy:mysql:/etc/postfix/mysql/virtual_alias_maps_regexp.cf,

4) Finally, I inserted the new alias into the database:

INSERT INTO alias (address, goto, domain, isregex) VALUES ('foo\..+@example.com', 'foo@example.com', 'example.com', 1);

This alias matches anything that starts with "foo" and a dot, followed by one or more other characters and then @example.com.

And it all works.  Now I can use emails such as "foo.costco@example.com" and "foo.bestbuy@example.com" and I can know who is selling their contacts to spammers.  BTW, the reason I don't want to just use "costco@example.com" and "bestbuy@example.com" is because I might have different users each using their own aliases:  "foo.costco@example.com" and "bar.costco@example.com".

----

Spider Email Archiver: On-Premises, lightweight email archiving software developed by iRedMail team. Supports Amazon S3 compatible storage and custom branding.

2

Re: User aliases with regular expressions

TimT wrote:

What I needed was for anything that started with, for example, "foo." to be redirected to "foo@example.com".  So if I wanted to sign up at, say, Costco, with an email address, I could use "foo.costco@example.com" and it would go to my real address of "foo@example.com"

You should use '+' in this case. foo+costco@example.com.

3

Re: User aliases with regular expressions

ZhangHuangbin wrote:

You should use '+' in this case. foo+costco@example.com.

No, this uses true regular expressions.  "foo+costco" would match "fo" and "costco" with one or more o's in between.  I think you're thinking of the way qmail does extensions maybe?  Or is there already a feature in postfix that does something like I want to do without the patch I made?

4

Re: User aliases with regular expressions

Postfix features extensions (so does Dovecot)
sending to
foo+costco@domain
will send it to
foo@domain and deliver it to his costco folder
so its a step up from your solution as it automatically sorts.
You can change the + delimiter to something else if you wish too through 2 config changes, but by default its a + symbol.

Sure its not true regexp, but its enough to do your examples.

5

Re: User aliases with regular expressions

The actual regular expression I use is "foo[_\-\.].+@example.com", which means "foo_abc", "foo-def", and "foo.xyz" will all be delivered to the indicated mailbox.  I just simplified it for the sake of the example.  Note also that a regular expression will allow you to use a suffix as well:  ".+[_\-\.]foo@example.com" will match both "duck.foo" and "bear.foo".

So with your example, you're saying "foo+<anything>@example.com" will be delivered to the "foo@example.com" mailbox, even though <anything> hasn't been defined in any alias, right?  If that's the case, this would probably suit my needs.  I'm not sure I'd want them going to separate folders... I use a *lot* of these.  But I'll look into it.

Thanks for the info.

6

Re: User aliases with regular expressions

That is correct yes, if you have a catch all account this may be broken but has been fixed in 0.9.1

7

Re: User aliases with regular expressions

7t3chguy wrote:

Postfix features extensions (so does Dovecot)
sending to
foo+costco@domain
will send it to
foo@domain and deliver it to his costco folder
so its a step up from your solution as it automatically sorts.
You can change the + delimiter to something else if you wish too through 2 config changes, but by default its a + symbol.

Sure its not true regexp, but its enough to do your examples.

Is this feature enabled by default in iRedmail?  I'd like to take advantage of it.  What is the feature called? 

Thank you!
Mike

8

Re: User aliases with regular expressions

mmarcott wrote:

Is this feature enabled by default in iRedmail?

Dear Mike,

Could you please clearly explain what feature you're looking for? And please post in a new forum topic.