Send VERP compliant email using python and postfix

2k views Asked by At

I want to be able to send emails in python so that I can parse bounces. VERP seems to be the solution to this. However I don't receive the bounce notification when I set my from address (not the MIMEMultipart object's "From" value) to include the VERP delimiter.

Can I just form my own VERP sender (i.e. to send to [email protected], can I just set my From: address for that recipient "mysender+user-domain-com.mydomain.com") and expect it to work, or do I need to configure postfix to correctly parse the failed DSN somehow? My problem is receipt of the failure notice, not getting the message to deliver to valid recipients when I do this.

Maybe an example will help.

When I send email from [email protected], I receive bounce notices just fine.

When I send email from [email protected], I don't receive any bounce notices. (Although correctly addressed emails, e.g., to: [email protected] go through just fine.) Perl's Mail module has a Verp sub-module, does python have anything like this?

2

There are 2 answers

0
wilsotc On BEST ANSWER

You should use "Return-Path" not FROM as the address to which a bounced message is sent. Specifying the return path in an outbound message using Postfix doesn't require special configuration.

The configuration requirement for Postfix to handle variable inbound messages can be handled with the + in an address because the + and everything that follows are usually ignored. So setting Return-Path to [email protected] should be delivered to [email protected] with the To address [email protected]. Programmatic handling of the message could be done with the addition of a filter to the master.cf file.

Put this in the service list:

smtp      inet  n       -       -       -       -       smtpd
  -o content_filter=filter:dummy

and define the filter here:

filter    unix  -       n       n       -      10       pipe
  flags=Rq user=filter null_sender=
  argv=/home/filter/InboundProcessor.py -f ${sender} -- ${recipient}

This will cause the InboundProcessor.py script to receive all inbound messages via pipe. If the To address contains a + followed some bounce key value you can associate with an address that is no longer valid, you can mark that address inactive in your database.

0
Serge Ballesta On

In order to correctly deal with VERP, Postfix must be specially configured. The smtpd_authorized_verp_clients which lists clients allowed tu use VERP only configure the sending part. In fact it lists clients allowed to use the XVERP command which automatically adds +dest to enveloppe sender address.

But in its default configuration Postfix do not automatically delivers to sender the messages sent to sender+ext. For this to work you must add (or uncomment) recipient_delimiter = + in main.cf. In the default configuration, the line is present but commented out.

So if you can form your own VERP sender address, ie have an enveloppe sender address of [email protected], you have nothing more to do provided you have recipient_delimiter = + : destination SMTP server will send bounces to that address and your Postfix will correctly deliver it to [email protected].

I do not know any python package that would automagically add the XVERB command. So IMHO if you want it to be used automatically, you should follow the following recipe described in Postfix VERP Howto :

You can use the smtpd_command_filter feature to append XVERP to SMTP commands from legacy software. This requires Postfix 2.7 or later.

/etc/postfix/main.cf:
    smtpd_command_filter = pcre:/etc/postfix/append_verp.pcre
    smtpd_authorized_verp_clients = $mynetworks

/etc/postfix/append_verp.pcre:
    /^(MAIL FROM:<listname@example\.com>.*)/ $1 XVERP