Email Bounce Detection

M

madhav

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: "(e-mail address removed)", then "_send()" in the
SMTPConnection class returns 550(Unknown recceipient). I checked the
same but its not returning 550 and return 250(receipient ok).
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.
 
M

Maric Michaud

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: "(e-mail address removed)", 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 :

Content-Type: multipart/report;
report-type=delivery-status;

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
Content-Type: message/delivery-status

Reporting-MTA: dns; aristote.info
X-Postfix-Queue-ID: 1D07D1409FF
X-Postfix-Sender: rfc822; (e-mail address removed)
Arrival-Date: Fri, 27 Jun 2008 09:10:14 +0200 (CEST)

Final-Recipient: rfc822; (e-mail address removed)
Original-Recipient: rfc822;[email protected]
Action: failed
Status: 5.0.0
Remote-MTA: dns; tyrande.nerim.net
Diagnostic-Code: smtp; 550 <[email protected]>: Recipient address
rejected:
User unknown in local recipient table


So, for example :

[48]: import email
[49]: bounce = email.message_from_string(open('ex.eml').read())
[50]: bounce.get_content_type()
...[50]: 'multipart/report'
[51]:
[52]: for i in bounce.get_payload() :
....: if i.get_content_type() == 'message/delivery-status' :
....: print i.get_payload()[1]['status']
....:
....:
5.0.0
 
M

madhav

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: "(e-mail address removed)", 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 :

Content-Type: multipart/report;
  report-type=delivery-status;  

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
Content-Type: message/delivery-status

Reporting-MTA: dns; aristote.info
X-Postfix-Queue-ID: 1D07D1409FF
X-Postfix-Sender: rfc822; (e-mail address removed)
Arrival-Date: Fri, 27 Jun 2008 09:10:14 +0200 (CEST)

Final-Recipient: rfc822; (e-mail address removed)
Original-Recipient: rfc822;[email protected]
Action: failed
Status: 5.0.0
Remote-MTA: dns; tyrande.nerim.net
Diagnostic-Code: smtp; 550 <[email protected]>: Recipient address
rejected:
    User unknown in local recipient table

So, for example :
[48]: import email
[49]: bounce = email.message_from_string(open('ex.eml').read())
[50]: bounce.get_content_type()

...[50]: 'multipart/report'
[51]:
[52]: for i in bounce.get_payload() :

   ....:    if i.get_content_type() == 'message/delivery-status' :
   ....:         print i.get_payload()[1]['status']
   ....:
   ....:
5.0.0

Thankyou somuch Maric. Will try the approach you have mentioned. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top