如何使回复用户的电子邮件机器人不回复自动回复并陷入邮件循环
我有一个回复用户的机器人。但有时当我的机器人发送回复时,用户或其电子邮件提供商会自动回复(假期消息、退回消息、邮件守护程序错误等)。这是来自用户的新消息(我的机器人认为),然后它会回复。邮件循环!
我希望我的机器人只回复来自真人的真实电子邮件。我目前正在过滤掉承认批量优先或来自邮件列表或自动提交标头等于“自动回复”或“自动生成”的电子邮件(请参阅下面的代码)。但我想有一种更全面或更标准的方法来处理这个问题。 (我很高兴看到 Perl 之外的其他语言的解决方案。)
注意:记住让你自己的机器人声明它是自动响应的!包含
Auto-Submitted: auto-reply
在机器人电子邮件的标题中。
我用于避免邮件循环的原始代码如下。仅当 realmail 返回 true 时才回复。
sub realmail {
my($email) = @_;
$email =~ /\nSubject\:\s*([^\n]*)\n/s;
my $subject = $1;
$email =~ /\nPrecedence\:\s*([^\n]*)\n/s;
my $precedence = $1;
$email =~ /\nAuto-Submitted\:\s*([^\n]*)\n/s;
my $autosub = $1;
return !($precedence =~ /bulk|list|junk/i ||
$autosub =~ /(auto\-replied|auto\-generated)/i ||
$subject =~ /^undelivered mail returned to sender$/i
);
}
(主题检查肯定是不必要的;我只是在出现问题时一次添加了这些检查,上面的内容现在似乎有效,所以我不想碰它,除非有明确更好的东西。)
I have a bot that replies to users. But sometimes when my bot sends its reply, the user or their email provider will auto-respond (vacation message, bounce message, error from mailer-daemon, etc). That is then a new message from the user (so my bot thinks) that it in turn replies to. Mail loop!
I'd like my bot to only reply to real emails from real humans. I'm currently filtering out email that admits to being bulk precedence or from a mailing list or has the Auto-Submitted header equal to "auto-replied" or "auto-generated" (see code below). But I imagine there's a more comprehensive or standard way to deal with this. (I'm happy to see solutions in other languages besides Perl.)
NB: Remember to have your own bot declare that it is autoresponding! Include
Auto-Submitted: auto-reply
in the header of your bot's email.
My original code for avoiding mail loops follows. Only reply if realmail returns true.
sub realmail {
my($email) = @_;
$email =~ /\nSubject\:\s*([^\n]*)\n/s;
my $subject = $1;
$email =~ /\nPrecedence\:\s*([^\n]*)\n/s;
my $precedence = $1;
$email =~ /\nAuto-Submitted\:\s*([^\n]*)\n/s;
my $autosub = $1;
return !($precedence =~ /bulk|list|junk/i ||
$autosub =~ /(auto\-replied|auto\-generated)/i ||
$subject =~ /^undelivered mail returned to sender$/i
);
}
(The Subject check is surely unnecessary; I just added these checks one at a time as problems arose and the above now seems to work so I don't want to touch it unless there's something definitively better.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
RFC 3834 提供了一些关于您应该做什么的指导,但这里有一些具体的指导原则:
将信封发件人设置为与自动回复器不同的电子邮件地址,这样退回邮件就不会反馈到系统中。
我总是在数据库中存储电子邮件响应从特定地址发送到另一个地址的时间的密钥。在任何情况下,我都不会在 10 分钟内多次回复同一地址。仅此一项就停止了所有循环,但并不能确保良好的行为(对邮件列表的自动回复很烦人)。
确保添加其他人匹配的标题的任何排列以停止循环。这是我使用的列表:
这是我用来避免循环并尝试发挥良好作用的一些标头正则表达式:
如果其中任何一个是信封发件人,我也会避免响应:
RFC 3834 provides some guidance for what you should do, but here are some concrete guidelines:
Set your envelope sender to a different email address than your auto-responder so bounces don't feed back into the system.
I always store in a database a key of when an email response was sent from a specific address to another address. Under no circumstance will I ever respond to the same address more than once in a 10 minute period. This alone stopped all loops, but doesn't ensure nice behavior (auto-responses to mailing lists are annoying).
Make sure you add any permutation of header that other people are matching on to stop loops. Here's the list I use:
Here are some header regex's I use to avoid loops and to try to play nice:
I also avoid responding if any of these are the envelop senders:
这确实听起来像是CPAN 中可能提供的模块,但我在五分钟的搜索中没有找到任何明显相关的内容。 Mail::Lite::Mbox::Processor 看起来可能做你想做的事:
但它的文档非常稀疏,以至于无法立即明显看出它是否提供了这些示例函数本身,或者您是否必须提供驱动它们的代码。
但无论如何,如果您还没有检查过 CPAN,那么如果我想做这样的事情,我会从那里开始。
That really sounds like something that's probably available as a module from CPAN, but I didn't find anything clearly relevant in five minutes of searching. Mail::Lite::Mbox::Processor looks like it might do what you want:
but its docs are sparse enough that it isn't immediately obvious whether it provides those example functions itself or if you have to provide the code to drive them.
In any case, though, if you haven't already checked CPAN, that's where I would start if I wanted to do something like this.
我这里的回答只涉及反弹,这更简单。
使用 DSN(递送状态通知) 标识符将帮助您检测 DSN/退回邮件。它应该转到 Return-Path 而不是 Reply-To。
以下是典型的 DSN 消息的示例。标头信息包括消息 ID、内容类型具有特定值(传递状态)等。
无法为您提供任何 Perl 代码,只是我的 2 美分想法。
PS:请注意,并非所有邮件服务器或 MTA 都符合这一点,但我想大多数都符合这一点。
My answer here only deals with bounces which is more straightforward.
Using DSN (Delivery Status Notification) identifier will help you detect a DSN/bounced message. It should go to Return-Path and not Reply-To.
Here's a sample of a typical DSN message. The header information includes the message id, content type has specific values (delivery-status) etc.
Not able to provide you any codes in perl, just my 2 cents of idea.
PS: Do note that not all mail servers or MTA conforms to this, but I guess most do.
应该有一个标准的方法来处理这个问题,但问题是您必须假设发送自动回复的系统符合该标准,而大多数时候,它们根本不符合该标准。
如何获得您回复的地址?我希望您没有使用
From:
标头。首先检查Reply-to:
标头,如果不存在,请使用Return-path:
。但无论你做什么,你只需要记录你发送给谁的内容,并将你的机器人限制在每次消息的合理值。
There should be a standard way of dealing with this, but the problem is that you'd have to assume that systems that send auto-replies comply to that standard, when most the time, they just don't.
How do you get the address that you reply to? I hope you aren't using the
From:
header. Check theReply-to:
header first and if that doesn't exist, use theReturn-path:
.But whatever you do, you will simply have to keep a log of what you sent to whom and throttle your bot to some sensible value of messages per time.