Piping emails using Postfix MTA for virtual users:
I was asked by my manager to configure ‘piping’ using postfix for virtual users. Now, when I say virtual users, it means:
1. User should not be present on the system physically.
2. User should not have a physical mailbox to store mails in.
3. User does not have any database account neither any database to store any mails on.
Most of the tutorials that I have come across through Google search refer to virtual domains and not virtual users. Here we will configure both, that is, virtual domains as well as virtual users who can actually receive emails from outer world and PIPE those emails to a program/script. The only caveat is if the email is lost somewhere while delivering, it will be lost forever, as there is no mailbox associated with them to, well, store those emails. Imagine that your email server is down for couple of hours and you are working your ass off to fix it, but during those hours, if any email is originated from any source for this virtual user, it will be eventually lost, why? You guessed it right! No mailbox to store it for later delivery.
This type of scenario is useful when you do not want to store the emails onto the server itself but want to pipe it to some program such as the help desk. For those, who do not know what help desk is, head over to: “www.kayako.com” for a basic insight. The setup is pretty straight forward and after having a look, you might say to yourself, “Hey! I know that already”. In that case, good for you, let me educate others who do not know 😉
Create 3 virtual domains as follows:
Add virtual email addresses (not the real ones, meaning no mailbox associated with them) such that emails for:
should go to there different test sites (and in turn fed to the scripts which will process them after receiving the mails)
So, we have scenario here that whenever we send email to “firstname.lastname@example.org“, it should go to the testing1 domain (testing1.virtual.internal); similarly, when we send email to “email@example.com“, it should go to the testing2 domain (testing2.virtual.internal) and so forth.
1. Edit the postfix configuration file, /etc/postfix/main.cf, and append the following to the last:
virtual_mailbox_domains = /usr/share/nginx/html/virtual/vdomains virtual_alias_maps = hash:/etc/postfix/virtual
2. Edit “/usr/share/nginx/html/virtual/vdomains” and add your virtual domains in this file, one virtual domain per line:
# # virtual domains for postfix # testing1.virtual.internal testing2.virtual.internal testing3.virtual.internal
NOTE: You can add the virtual domain to any file but make sure that it is mentioned in the configuration file (main.cf) under: virtual_mailbox_domains.
3. Edit “/etc/postfix/virtual” file to add the virtual email addresses. Now the trick is that virtual aliases do not work with piping, but local aliases do. So, we will first link virtual aliases to local dummy account and then link those dummy accounts to the script to which we want to fed our incoming emails to.
# Virtual email address dummy user which will be used in aliases file firstname.lastname@example.org support-testing1 email@example.com support-testing2 firstname.lastname@example.org support-testing3
NOTE: Run “postmap /etc/postfix/virtual” every time you edit the above file (/etc/postfix/virtual). It may be addition of another virtual account, deletion of account, etc.
4. Edit “/etc/aliases” file so that we can link our dummy users (local aliases) to the piped program/script.
support-testing1: "|/usr/share/nginx/html/virtual/testing1/console/index.php" support-testing2: "|/usr/share/nginx/html/virtual/testing2/console/index.php" support-testing3: "|/usr/share/nginx/html/virtual/testing3/console/index.php"
Do not forget to change the permissions on your piped program/script, they should be 755.
Restart ‘postfix’ and you will have a setup where if any email is received on one of the accounts above, that email will directly be fed into the script for further processing and it will not be stored on the server.
Pretty neat stuff;) Ain’t it!