Linux – Postfix how to triggering the script when outgoing email status is sent

linuxpostfix

I want to run a program when postfix has successfully sent out a mail (local or remote).
I would like to pass the headers to program and if possible also the destination ip or address (exclude spam filter delivery).

I just have an idea: Delivery Status Notification processing via uniqe transport program, but I'd prefer the above.

My goal is to be recorded lifetime (events) of email: it came, it went out (from, to, subject, datetime, message id, message status: bounce, sent). I would only need the state of the outgoing mail, because incoming and bounce program is working.

It is possible to trigger a program (similar to a transport pipe/spawn) or DSN "cheat" stay?

Thanks in advance for any reply!

Best Answer

This is only for logging message headers.

By default postix already logs most of this information, though not in one line. It doesn't, however, log the subject or message-id. We can add that in by using header_checks 5.
Note: for all mail and not just outgoing, use header_checks instead of smtp_header_checks.


#main.cf
smtp_header_checks = regexp:$config_directory/header-logging.cf


#header-logging.cf
/^subject:/      INFO
/^Subject:/      INFO
/^Message-Id:/   INFO

These messages will be logged to syslog in the facility specified by syslog_facility (default: mail).

Apr  9 15:37:25 mta-msa postfix/smtps/smtpd[14688]: DA75A231C600: client=192-0-2-5.example.com[192.0.2.5], sasl_method=LOGIN, sasl_username=foo
Apr  9 15:37:25 mta-msa postfix/cleanup[14686]: DA75A231C600: message-id=<000401d07315$bf0ea5a0$3d2bf0e0$@example.com>
Apr  9 15:37:25 mta-msa postfix/qmgr[14653]: DA75A231C600: from=<foo@example.com>, size=6495, nrcpt=1 (queue active)
Apr  9 15:37:26 mta-msa postfix/smtp[14675]: DA75A231C600: info: header Subject: RE: please email as you leave
Apr  9 15:37:26 mta-msa postfix/smtp[14675]: DA75A231C600: info: header Message-ID: <000401d07315$bf0ea5a0$3d2bf0e0$@example.com>
Apr  9 15:37:26 mta-msa postfix/smtp[14675]: DA75A231C600: to=<bar@example.org>, relay=mx.example.org[198.51.100.0]:25, delay=0.65, relays=0.01/0/0.33/0.31, dsn=2.0.0, status=sent (250 2.0.0 1tnx2ya0mh-1 Message accepted for delivery)
Apr  9 15:37:26 mta-msa postfix/qmgr[14653]: DA75A231C600: removed