Email Bounce Detection
maric at aristote.info
Fri Jun 27 09:34:22 CEST 2008
Le Friday 27 June 2008 09:03:15 madhav, vous avez écrit :
> Hello everybody, I need a mechanism to detect email bounces. I tried
> browsing through smtplib implementation and found not helpful in this
> case. Actually it is said in the documentation that if a mail is sent
> to say: "somerandomname at randomorg.com", then "_send()" in the
> SMTPConnection class returns 550(Unknown recceipient). I checked the
> same but its not returning 550 and return 250(receipient ok).
This is not the same scenario, the SMTP server that will respond 550 is the MX
for the domain, if you send your mails using an outgoing SMTP server, it
can't know in advance the list of remote adressesbut will bounce your message
when receiving the 550 from the remote MX.
> Later, I tried getting the smtp error code from the mail body of a
> bounced msg. the "Message" class doesnot have any error code attribute
> by default. We need to get it from the mailbody(by regex ing), which i
> think is not a valid solution, as "550" can occur anywhere in the body
> of any message otherthan the bounced msg also.
> Please help me regarding this.
> Thanks in advance.
Bounces are multipart messages easily parsed with the email package of the
stdlib (dont' use regexes here).
Abounce message is of type :
a delivery-status imply that one of the subpart of the message is a
message/delivery-status with all needed infos set in the headers. This one is
taken from a real example :
Content-Description: Delivery report
Reporting-MTA: dns; aristote.info
X-Postfix-Sender: rfc822; maric at aristote.info
Arrival-Date: Fri, 27 Jun 2008 09:10:14 +0200 (CEST)
Final-Recipient: rfc822; unknownadress at nerim.net
Original-Recipient: rfc822;unknownadress at nerim.net
Remote-MTA: dns; tyrande.nerim.net
Diagnostic-Code: smtp; 550 <unknownadress at nerim.net>: Recipient address
User unknown in local recipient table
So, for example :
>>>: import email
>>>: bounce = email.message_from_string(open('ex.eml').read())
>>>: for i in bounce.get_payload() :
....: if i.get_content_type() == 'message/delivery-status' :
....: print i.get_payload()['status']
More information about the Python-list