postfix - pipe (call script)

 

說明

 

pipe - Postfix delivery to external command

flags=BDFORXhqu.>

B      Append a blank line at the end of each message.

D      Prepend a "Delivered-To: recipient" message header

F      Prepend a "From sender time_stamp" envelope header

0      Prepend an "X-Original-To: recipient" message header

R      Prepend a Return-Path: message header

X      Indicate that the external command  performs final  delivery.
         (log 會出 delivered, 沒有它時, 會出 relayed)

q      Quote whitespace and other special characters in the command-line $sender, 

        $original_recipient  and $recipient address localparts

user=username:groupname     <-- 用 username 及 groupname 身份執行

argv=command... (required)    <-- The command to be executed.

macro

macro expands to the X

  • ${client_address}
  • ${client_helo}
  • ${sender}                       # null sender => MAILER-DAEMON
  • ${recipient}                    # the complete recipient address
  • ${original_recipient}
  • ${size}

 


Example

 

master.cf

# mail script
email_script unix - n n - - pipe
  flags=DOFR user=mailscript argv=/home/mailscript/mail_handle.php
  $(sender) -- $(recipient) -- ${original_recipient} -- ${size}

 


mail_handle.php

 

#!/usr/bin/php
<?php
    $mypath = '/home/web/vmail_script/';
    $fd = fopen("php://stdin", "r");
    $email = "";
    while (!feof($fd)) {
            $email .= fread($fd, 1024);
    }
    fclose($fd);

    // mail contents
    $fh = fopen($mypath . 'test1.txt', 'a');
    fwrite($fh, $email."EOF");
    fclose($fh);

    // mail info from $_ENV
    ob_start();
    echo "SERVER\n"; print_r($_SERVER);
    echo "EVN\n"; print_r($_ENV);
    $content=ob_get_contents();
    ob_clean();
    file_put_contents($mypath . "test2.txt", $content);
?>

注意行 script 的 user permission 要有 x

Remark

ob_start() - turn output buffering on.

While output buffering is active no output is sent from the script (other than headers),

instead the output is stored in an internal buffer.

ob_get_contents() - Gets the contents of the output buffer without clearing it.

 


Postfix Setting

 

################################### vmail script setting ###################################

# groupadd -g 50000 vmail
# useradd -u 50000 -g 50000 vmail -d /var/vmail -m
# chown vmail. /var/vmail/
# chmod 770 /var/vmail/

virtual_gid_maps = static:50000
virtual_uid_maps = static:50000
virtual_minimum_uid = 50000
virtual_mailbox_base = /var/vmail
# Main Setting
virtual_transport = virtual

virtual_mailbox_maps = static:mydomain/vmail
virtual_mailbox_domains = static:mydomain
transport_maps = hash:/etc/postfix/transport

/etc/postfix/remote_mailbox.cf

a@b             email_script

 

解釋:

virtual_mailbox_domains (default: $virtual_mailbox_maps)

    This parameter expects the same syntax as the mydestination configuration parameter.

    The Postfix virtual(8) mailbox delivery agent looks up the user mailbox pathname,

    uid and gid via separate tables that are searched with the recipient's mail address.

    Maildir style delivery is turned on by terminating the mailbox pathname with "/"

    The virtual_mailbox_base parameter specifies a prefix for all virtual mailbox pathnames

virtual_mailbox_base

A prefix that the virtual(8) delivery agent prepends to all pathname results from $virtual_mailbox_maps table lookups.

If the lookup result ends in a slash ("/"), maildir-style delivery is carried out, otherwise the path is assumed to specify a UNIX-style mailbox file.

virtual_gid_maps (default: empty)
    Lookup tables with the per-recipient group ID for virtual(8) mailbox delivery.
    This parameter is specific to the virtual(8) delivery agent. It does not apply when mail is delivered with a different mail delivery program.

virtual_minimum_uid (default: 100)

The minimum user ID value that the virtual(8) delivery agent accepts as a result from $virtual_uid_maps table lookup. Returned values less than this will be rejected, and the message will be deferred.

 


"D" flag

 

default_destination_recipient_limit (default: 50) => 所以會令 "D" flag 無效 !

因此, 我們要加入設定

dovecot_destination_recipient_limit = 1

P.S.

dovecot 是在 master.cf 設定好的 program

 

 

 

Creative Commons license icon Creative Commons license icon