Postfix SMTP Relay Based on Alias (From Address) – Configuration Guide

emailpostfix

I recently noticed when I add an alias to a Gmail account they ask me for a remote SMTP server, username and password. Then, whenever I try to send an email using that alias it gets relayed by Gmail to the provided SMTP server.

Is there a way I can accomplish this setup on a Postfix install?

To clarify, on my server there's a virtual user person@exemple.com (with the aliases: external.person@xpto.com and external.other@corpx.com) if he sends an email:

  • With a from address of person@exemple.com => server will do the default delivery;
  • With a from address of external.person@xpto.com => server will relay email using smtp.xpto.com (with proper credentials);
  • With a from address of external.other@corpx.com => server will relay email using smtp.corpx.com (with proper credentials);

Ideally it would be nice If I could have a MySQL table with the external alises (for every virtual user) containing the external SMTP server domain, port, username and password. => This way I could setup a small web interface so my users could all their own external alises…

Thank you.

Best Answer

I guess you can achieve this by tuning the sender_dependent_default_transport_maps or sender_dependent_relayhost_maps Postfix parameters. For example:

# /etc/postfix/main.cf
sender_dependent_default_transport_maps = hash:/etc/postfix/sender_maps.cf
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sender_credentials.cf
smtp_sasl_tls_security_options = noanonymous
smtp_sender_dependent_authentication = yes
smtp_tls_security_level = may

# /etc/postfix/sender_maps.cf
external.person@xpto.com smtp:[smtp.xpto.com]:587
external.other@corpx.com smtp:[smtp.corpx.com]

# /etc/postfix/sender_credentials.cf
external.person@xpto.com xptouser:xptopassword
external.other@corpx.com corpxuser:corpxpassword

This example uses static hash tables. If your Postfix installation supports mysql_table(5), you can use MySQL queries instead.

I am unable to test this solution now. I hope it works.

Related Topic