Is there a way to get the DSN status of an email set with postfix, using php mail function or any other library ? I can parse the log for the id and DSN status, but that won't be quite good for the server, considering the flow of emails coming in. I need a way to get the status of the mail and store it in a DB or file.


Solution 1:

Once the PHP mail function concludes without an error code the message has been accepted by the MTA (postfix, sendmail or whatever is your favourite) the interaction between your script and the MTA has ended.

Once the SMTP message is in the mail queue it will be tried for immediate delivery and may actually be delivered immediately, or delivery could fail immediately, but the message can also sit in the queue (for minutes, hours or even days) due to transient problems before final delivery or failure.

In case of a (long temporary) failure the normal behaviour of a MTA would be to send a delivery notification to the sender. No failure notification would signify succesful remote delivery (where the message may still be (silently) discarded).

You can monitor the mailbox of that sender and for instance filter delivery notifications to a PHP script that will categorise them and upload them to your database.