Hung Vi
Hung Vi

Reputation: 490

Postfix - How to parse bounced message

I'm very new to Postfix. I tried to google about how to handle bounced messages for two days but my question is not anserwed.

I want to handle bounced messages with an external program, and AFAIK I can do with these steps:

  1. Configure main.cf with transport_maps properties

    transport_maps = hash:/etc/postfix/transport

  2. List item

    [email protected] bounce-pipe:

  3. Configure master.cf

    bounce-pipe unix - n n - - pipe user=bounce argv=/etc/postfix/mailpipe.py

  4. Create python/ruby script to parse bounced messages with 3rd party lib

It seems that the script is called when receiving a message. For example, I sent an email to non-existence (i.e [email protected]) and then I got this information in console

mail    | Dec 14 02:15:52 mail postfix/pickup[1897]: DEB292A16D9: uid=0 from=<root>
mail    | Dec 14 02:15:52 mail postfix/cleanup[1955]: DEB292A16D9: message-id=<[email protected]>
mail    | Dec 14 02:15:52 mail opendkim[146]: DEB292A16D9: no signing table match for '[email protected]'
mail    | Dec 14 02:15:52 mail opendkim[146]: DEB292A16D9: no signature data
mail    | Dec 14 02:15:52 mail postfix/qmgr[1898]: DEB292A16D9: from=<[email protected]>, size=283, nrcpt=1 (queue active)
mail    | Dec 14 02:15:52 mail postfix/smtp[1957]: connect to gmail-smtp-in.l.google.com[2404:6800:4008:c04::1b]:25: Cannot assign requested address
mail    | Dec 14 02:15:53 mail postfix/smtp[1957]: Trusted TLS connection established to gmail-smtp-in.l.google.com[74.125.204.27]:25: TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
mail    | Dec 14 02:15:54 mail postfix/smtp[1957]: DEB292A16D9: to=<[email protected]>, relay=gmail-smtp-in.l.google.com[74.125.204.27]:25, delay=4.2, delays=2.6/0.02/1.1/0.52, dsn=5.7.1, status=bounced (host gmail-smtp-in.l.google.com[74.125.204.27] said: 550-5.7.1 [103.23.147.162] The IP address sending this message does not have a 550-5.7.1 PTR record setup. As a policy, Gmail does not accept messages from 550-5.7.1 IPs with missing PTR records. Please visit 550-5.7.1  https://support.google.com/mail/answer/81126#authentication for more 550 5.7.1 information. o7si2148670pgr.491 - gsmtp (in reply to end of DATA command))
mail    | Dec 14 02:15:54 mail postfix/cleanup[1955]: 9185A2A16DB: message-id=<[email protected]>
mail    | Dec 14 02:15:54 mail postfix/qmgr[1898]: 9185A2A16DB: from=<>, size=3010, nrcpt=1 (queue active)
mail    | Dec 14 02:15:54 mail postfix/bounce[1967]: DEB292A16D9: sender non-delivery notification: 9185A2A16DB
mail    | Dec 14 02:15:54 mail postfix/qmgr[1898]: DEB292A16D9: removed
mail    | Dec 14 02:15:54 mail postfix/pipe[1968]: 9185A2A16DB: to=<[email protected]>, relay=bounce-pipe, delay=0.05, delays=0.03/0.01/0/0.01, dsn=2.0.0, status=sent (delivered via bounce-pipe service (20171214-021554))
mail    | Dec 14 02:15:54 mail postfix/qmgr[1898]: 9185A2A16DB: removed

The question is, where are bounced messages stored or how to pass them to my python script as parameter. Bounced messages should be formatted like this: https://github.com/sisimai/set-of-emails/blob/master/mailbox/mbox-0

Upvotes: 0

Views: 4397

Answers (3)

Ralf Hildebrandt
Ralf Hildebrandt

Reputation: 541

The messages are given to your script by means of "stdin" (standard input). Your script needs to exit with exit status 0 (since that signals a "successful delivery" to Postfix).

Upvotes: 0

Hung Vi
Hung Vi

Reputation: 490

I found the answer for the question How to pipe incoming email (message) to external program,

I just simply configure aliases after installing postfix (/etc/aliases)

anyname: "| /tmp/your_external_script.sh"

And then renew aliases:

newaliases

If your script is a ruby program, then you can read all the incoming emails from stdin.

It looks like this: http://davidsj.co.uk/blog/postfix-pipe-emails-to-a-script/

Upvotes: 0

tripleee
tripleee

Reputation: 189789

A well-formed bounce message contains a number of machine-readable headers. See e.g. https://en.wikipedia.org/wiki/Bounce_message#Format for a brief introduction with hints for where to look for more information, including ultimately the pertinent RFC 6522

A bounce message is just a message. It is stored in the recipient's mailbox. For example, if you attempt to send a message and it fails, Postfix will attempt to store the resulting bounce in your inbox, just like any other incoming message to you.

If you are only looking to automate processing of messages to a particular user (root or otherwise), Postfix already knows how to deliver to Procmail or Maildrop, both of which easily allow you to run arbitrary scripts on your incoming email, conditionally on message content (hint: look for the very specific DSN header fields from the RFC) or unconditionally.

Your question is very vague on what exactly you want to accomplish. For a start, you will need to understand basic MIME structure, including the concept of a "body part" and how each body part has its own set of headers and a (potentially empty) body (payload), and how these can be represented in a multitude of different content encodings. The Python standard email library was recently revamped; for new development efforts, make sure you target 3.6+ (though technically the new implementation was already present in 3.4, not just the default).

Upvotes: 1

Related Questions