Avoid Mickey Mouse Email Loops
Say you have an application (i.e. bot, script, automaton, service) polling an email account (POP3 or IMAP) for emails. And say that your application sends an automated response back to the sender of the email. Be careful. You can easily get into situations that will create an email loop or worse yet the Sorcerer’s Apprentice Syndrome.
After sending email from an automaton it is quite common for the receiving mail servers to bounce emails back or accounts to temporarily respond with an “Out of Office” response. If your service is not smart enough to ignore automated responses to it’s own emails? Well. The streams will cross and poorly written email services will implode under the weight of your unending email loop. Kittens die. System administrators call you. This is bad.
RFC 3834 comes to the rescue with this sage advice:
An automatic responder MUST NOT blindly send a response for every message received.
Thank you Captain Obvious. Thankfully this RFC sets up a standard way to detect and inform your email service in order to avoid the dreaded email loop.
Being a good email citizen
The RFC provides guidance on informing your senders that you are indeed an automation. The crux of the guidance is that your email response should include the Auto-Submitted email header.
Return-Path: <admin@kevin>
Received: from Kevin ([192.168.0.106])
by KEVIN
with hMailServer ; Mon, 12 Jan 2009 10:21:47 -0600
Auto-Submitted: auto-replied
X-FC-SentBySECNET: Dovetail-Email-Agent-Service-Instance-001
From: Dovetail Software Email Agent <admin@kevin>
To: barney <barney@kevin>
Date: Mon, 12 Jan 2009 16:21:47 GMT
Message-ID: <e75d9c16520b461e8d10a1f63c029639@Kevin>
Subject: RE: send me an automated response? – About case 12
To accomplish this I created a little extension method used when creating emails. Note, the MailMessage type is part of an extinct .Net Email API we are currently saddled with:
public static readonly string AutoSubmittedHeaderTitle = "Auto-Submitted"; public static void ShouldBeAutoReplied(this MailMessage message) { message.Headers.Add(AutoSubmittedHeaderTitle, "auto-replied"); }
Being a good email consumer
On the flip side your email application should have a plan for what to do when it gets an automated message. For our usage we’ve updated our Email Agent service to detect and ignore auto submitted emails. Again I use an extension method to test each email.
public static bool IsAutoSubmitted(this MailMessage message) { return (message.Headers.Contains(AutoSubmittedHeaderTitle)/ message.Headers[AutoSubmittedHeaderTitle].Value != "no"); }