On the 25th of february 2010, I wrote a post explaining how to handle domain aliases with Postfix (http://vidax.net/blog/2010/02/domain-aliases-with-postfix/). I recently decided to add dynamic addresses support. Google uses this convenient feature. Let’s say your address is « james@domain.com ». Using the dynamic addresses, you will be able to route « james+anything@domain.com » to your real address.
I can figure out some applications:
- you may keep a track of the website you subscribed to. For instance, you register on « funnygames.com » and need to provide a valid email address. You may set « james+funnygames@domain.com »
- for professional use, you may organize lotteries « Send us your best picture ». Define a unique dynamic email address by competitor (for instance: lottery+f2dk320@domain.com) in order to link email to user’s account
- you may also provide a distant email access to your service. Users will be able to post to their account (timeline, wall) from their unique email address.
In order for this to work, you need to create a MySQL function
DELIMITER | DROP FUNCTION IF EXISTS `postfix`.`get_email_alias_dyn`| CREATE FUNCTION `postfix`.`get_email_alias_dyn`(myemail VARCHAR(255)) RETURNS TEXT CHARSET utf8 BEGIN DECLARE mygoto VARCHAR(255); DECLARE isdynamic INT(11); DECLARE sdomain VARCHAR(255); DECLARE ddomain VARCHAR(255); SELECT SUBSTRING(myemail, INSTR(myemail, '@')+1) INTO sdomain; SELECT INSTR(myemail, '+') INTO isdynamic; IF isdynamic > 0 THEN SELECT CONCAT(SUBSTRING(myemail, 1, isdynamic-1), '@', sdomain) INTO myemail; END IF; SELECT target_domain FROM alias_domain WHERE alias_domain = sdomain AND active = 1 LIMIT 1 INTO ddomain; IF ddomain IS NOT NULL THEN SELECT REPLACE(myemail, sdomain, ddomain) INTO myemail; END IF; SELECT goto FROM alias WHERE address = myemail AND active = 1 LIMIT 1 INTO mygoto; IF mygoto IS NOT NULL THEN RETURN mygoto; END IF; RETURN null; END|
Then update your postfix configuration
#vi /etc/postfix/conf/mysql-virtual_aliases.cf
and set
hosts = 127.0.0.1
user = postfix
password = password
dbname = postfix
query = SELECT get_email_alias_dyn('%s')
Restart postfix and give it a try.
Notice: the feature as described above will only work with Postfix working with MySQL.
Warning: it is highly recommended to test the MySQL function before altering the postfix configuration. In order to do so, open your favorite MySQL manager, create the function and issue « SELECT get_email_alias_dyn(‘james+DJKZHCJKSH@domain.com’) ». If the result is « james@domain.com » (james@domain.com must exist as a mailbox into DB), the feature will very likely work.