IRedMail/FAQ/Pipe.Incoming.Email.For.Certain.User.To.External.Script

From iRedMail

(Difference between revisions)
Jump to: navigation, search
(Configure Postfix)
Line 5: Line 5:
This tutorial explains how to pipe incoming email for certain user to external script.
This tutorial explains how to pipe incoming email for certain user to external script.
-
= Configure Postfix =
+
= Configure Postfix to use your external script as transport =
-
We need to edit the Postfix config file /etc/postfix/main.cf to handle an additional transport map.
+
Update file /etc/postfix/master.cf to use your external script as Postfix transport. Add below line at the bottom of file /etc/postfix/master.cf:
-
 
+
-
Edit /etc/postfix/main.cf, prepend 'hash:/etc/postfix/transport,' in Postfix setting "transport_maps".
+
-
{{cfg|main.cf|<pre>
+
-
transport_maps = hash:/etc/postfix/transport, [...OMIT EXISTING SETTINGS HERE...]
+
-
</pre>}}
+
-
 
+
-
Update file /etc/postfix/transport, add below line:
+
-
{{cfg|/etc/postfix/transport|<pre>
+
-
# Pipe incoming mails for user "rt@example.com" to external script.
+
-
# External script is defined as Postfix transport "external-pipe" in /etc/postfix/master.cf.
+
-
rt@example.com    external-pipe
+
-
</pre>}}
+
-
 
+
-
Execute command to create (or update) transport db:
+
-
{{cmd|<pre>
+
-
# postmap hash:/etc/postfix/transport
+
-
</pre>}}
+
-
 
+
-
Update file /etc/postfix/master.cf to define new Postfix transport for external script. Add below line at the bottom of file /etc/postfix/master.cf:
+
{{cfg|/etc/postfix/master.cf|<pre>
{{cfg|/etc/postfix/master.cf|<pre>
Line 35: Line 16:
Note: You can use some macros to replace with corresponding information from the Postfix queue manager delivery request. Refer to Postfix manual page for more detail: http://www.postfix.org/pipe.8.html
Note: You can use some macros to replace with corresponding information from the Postfix queue manager delivery request. Refer to Postfix manual page for more detail: http://www.postfix.org/pipe.8.html
-
Note that the 2nd line needs to be right under the first and must start with whitespace, and the first line cannot start with whitespace or postfix will fail with 'unexpected command-line argument' errors. Also make sure the user= line is pointing to a valid user with permissions to execute the script. This user must not be the postfix or root user or the pipe will fail.
+
Note: that the 2nd line needs to be right under the first and must start with whitespace, and the first line cannot start with whitespace or postfix will fail with 'unexpected command-line argument' errors. Also make sure the user= line is pointing to a valid user with permissions to execute the script. This user must not be the postfix or root user or the pipe will fail.
Of course you can add parameters after script. for example, dovecot delivers mails locally with its MDA program:
Of course you can add parameters after script. for example, dovecot delivers mails locally with its MDA program:
Line 43: Line 24:
</pre>}}
</pre>}}
-
Now restart Postfix service to make it work.
+
Now restart Postfix service to make this new transport available:
{{cmd|<pre>
{{cmd|<pre>
# /etc/init.d/postfix restart
# /etc/init.d/postfix restart
</pre>}}
</pre>}}
 +
 +
= Update per-user transport to use this new transport =
 +
 +
We need to update per-user transport setting, so that all emails delivered to this user will be piped to this new transport - your script.
 +
 +
* If you have iRedAdmin-Pro-LDAP installed, please go to user profile page, under tab '''Advanced''', set '''Relay/Transport setting''' to 'external-pipe'. Screenshot for your reference: http://screenshots.iredmail.googlecode.com/hg/iredadmin/user_profile_relay.png
 +
* If you have iRedAdmin-Pro-MySQL or iRedAdmin-Pro-PGSQL installed, please go to user profile page, under tab '''Relay''', set '''Relay/Transport setting''' to 'external-pipe'.
 +
* If you don't have iRedAdmin-Pro installed, you have to manually update LDAP/MySQL/PgSQL database to use this new transport.
 +
** For OpenLDAP backend, please login to phpLDAPadmin, please add new attribute '''mtaTransport''' for your user, set its value to 'external-pipe'.
 +
** For MySQL/PostgreSQL backend, please execute below command with SQL command line tool (Replace 'user@domain.ltd' by the real email address):
 +
{{cmd|<pre>
 +
sql> USE vmail;
 +
sql> UPDATE mailbox SET transport='external-pipe' WHERE username='user@domain.ltd';
 +
</pre>}}
 +
 +
That's all.

Revision as of 23:58, 19 April 2012

Contents


Summary

This tutorial explains how to pipe incoming email for certain user to external script.

Configure Postfix to use your external script as transport

Update file /etc/postfix/master.cf to use your external script as Postfix transport. Add below line at the bottom of file /etc/postfix/master.cf:

File: /etc/postfix/master.cf
external-pipe         unix    -       n               n               -               -       pipe
    flags= user=vmail:vmail argv=/path/to/your/external/script.sh

Note: You can use some macros to replace with corresponding information from the Postfix queue manager delivery request. Refer to Postfix manual page for more detail: http://www.postfix.org/pipe.8.html

Note: that the 2nd line needs to be right under the first and must start with whitespace, and the first line cannot start with whitespace or postfix will fail with 'unexpected command-line argument' errors. Also make sure the user= line is pointing to a valid user with permissions to execute the script. This user must not be the postfix or root user or the pipe will fail.

Of course you can add parameters after script. for example, dovecot delivers mails locally with its MDA program:

File: master.cf
dovecot unix    -       n       n       -       -      pipe
    flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/deliver -f ${sender} -d ${user}@${domain}

Now restart Postfix service to make this new transport available:

Terminal:
# /etc/init.d/postfix restart

Update per-user transport to use this new transport

We need to update per-user transport setting, so that all emails delivered to this user will be piped to this new transport - your script.

  • If you have iRedAdmin-Pro-LDAP installed, please go to user profile page, under tab Advanced, set Relay/Transport setting to 'external-pipe'. Screenshot for your reference: http://screenshots.iredmail.googlecode.com/hg/iredadmin/user_profile_relay.png
  • If you have iRedAdmin-Pro-MySQL or iRedAdmin-Pro-PGSQL installed, please go to user profile page, under tab Relay, set Relay/Transport setting to 'external-pipe'.
  • If you don't have iRedAdmin-Pro installed, you have to manually update LDAP/MySQL/PgSQL database to use this new transport.
    • For OpenLDAP backend, please login to phpLDAPadmin, please add new attribute mtaTransport for your user, set its value to 'external-pipe'.
    • For MySQL/PostgreSQL backend, please execute below command with SQL command line tool (Replace 'user@domain.ltd' by the real email address):
Terminal:
sql> USE vmail;
sql> UPDATE mailbox SET transport='external-pipe' WHERE username='user@domain.ltd';

That's all.

Personal tools