简体   繁体   English

使用python和postfix发送符合VERP的电子邮件

[英]Send VERP compliant email using python and postfix

I want to be able to send emails in python so that I can parse bounces. 我希望能够使用python发送电子邮件,以便我可以分析跳动。 VERP seems to be the solution to this. VERP似乎是解决方案。 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. 但是,当我将发件人地址(不是MIMEMultipart对象的“发件人”值)设置为包括VERP分隔符时,我没有收到退信通知。

Can I just form my own VERP sender (ie to send to user@domain.com, 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? 我是否可以组建自己的VERP发件人(即发送至user@domain.com,我可以为该收件人设置“发件人:”地址“ mysender + user-domain-com.mydomain.com”),并希望它能正常工作,还是我需要配置后缀以某种方式正确解析失败的DSN? 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 myuser@domain.com, I receive bounce notices just fine. 从myuser@domain.com发送电子邮件时,我会收到退信通知。

When I send email from myuser+verp_identifier@domain.com, I don't receive any bounce notices. 从myuser+verp_identifier@domain.com发送电子邮件时,我没有收到任何退信通知。 (Although correctly addressed emails, eg, to: valid_user@gmail.com go through just fine.) Perl's Mail module has a Verp sub-module, does python have anything like this? (尽管正确寻址的电子邮件,例如发送到:valid_user@gmail.com也可以。) Perl的Mail模块具有Verp子模块,python是否有类似的东西?

You should use "Return-Path" not FROM as the address to which a bounced message is sent. 您应该使用“ Return-Path”而不是FROM作为退回邮件发送到的地址。 Specifying the return path in an outbound message using Postfix doesn't require special configuration. 使用Postfix在出站邮件中指定返回路径不需要特殊配置。

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. Postfix处理变量入站消息的配置要求可以使用地址中的+来处理,因为通常会忽略+及其后面的所有内容。 So setting Return-Path to joe+bounce@here.com should be delivered to joe@here.com with the To address joe+bounce@here.com. 因此,应将“返回路径”设置为joe+bounce@here.com,并使用收件人地址joe+bounce@here.com将其传递到joe@here.com。 Programmatic handling of the message could be done with the addition of a filter to the master.cf file. 可以通过在master.cf文件中添加过滤器来对消息进行编程处理。

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. 这将导致InboundProcessor.py脚本通过管道接收所有入站消息。 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. 如果“收件人”地址在某个退回键值后包含+,则可以将其与不再有效的地址相关联,可以在数据库中将该地址标记为无效。

In order to correctly deal with VERP, Postfix must be specially configured. 为了正确处理VERP,必须对Postfix进行特殊配置。 The smtpd_authorized_verp_clients which lists clients allowed tu use VERP only configure the sending part. smtpd_authorized_verp_clients其中列出了允许客户使用恩只VERP配置发送部分。 In fact it lists clients allowed to use the XVERP command which automatically adds +dest to enveloppe sender address. 实际上,它列出了允许使用XVERP命令的客户端,该命令会自动将+dest添加到信封发件人地址中。

But in its default configuration Postfix do not automatically delivers to sender the messages sent to sender+ext . 但是,在其默认配置中,Postfix 不会自动将发送到sender+ext的消息传递给sender For this to work you must add (or uncomment) recipient_delimiter = + in main.cf . 为此,您必须在main.cf添加(或取消注释) recipient_delimiter = + 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 myuser+verpid@domain.com , 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 myuser@domain.com. 因此,如果您可以形成自己的VERP发件人地址,即具有myuser+verpid@domain.com的信封发件人地址,则myuser+verpid@domain.com您有myuser+verpid@domain.com recipient_delimiter = + ,您就无事可做:目标SMTP服务器将向该地址发送退回邮件,并且您的Postfix会将其正确传送到myuser@domain.com。

I do not know any python package that would automagically add the XVERB command. 我不知道会自动添加XVERB命令的任何python软件包。 So IMHO if you want it to be used automatically, you should follow the following recipe described in Postfix VERP Howto : 因此,恕我直言,如果您希望自动使用它,则应遵循Postfix VERP Howto中所述的以下食谱:

You can use the smtpd_command_filter feature to append XVERP to SMTP commands from legacy software. 您可以使用smtpd_command_filter功能将XVERP附加到旧版软件的SMTP命令中。 This requires Postfix 2.7 or later. 这需要Postfix 2.7或更高版本。

/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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM