Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4055950
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:45:56+00:00 2026-05-20T14:45:56+00:00

I’m using the Python email module to parse emails. I need to be able

  • 0

I’m using the Python email module to parse emails.

I need to be able to tell if an email is a “Delivery Status Notification”, find out what the status is, and extract information on the email that failed, eg. the Subject.

The object I get after parsing with .parsestr(email) is like this:

{'Content-Transfer-Encoding': 'quoted-printable',
 'Content-Type': 'text/plain; charset=ISO-8859-1',
 'Date': 'Mon, 14 Mar 2011 11:26:24 +0000',
 'Delivered-To': 'sender@gmail.com',
 'From': 'Mail Delivery Subsystem <mailer-daemon@googlemail.com>',
 'MIME-Version': '1.0',
 'Message-ID': '<000e08jf90sd9f00e6f943f@google.com>',
 'Received': 'by 10.142.13.8 with SMTP id 8cs63078wfm;\r\n        Mon, 14 Mar 2011 04:26:24 -0700 (PDT)',
 'Return-Path': '<>',
 'Subject': 'Delivery Status Notification (Failure)',
 'To': 'sender@gmail.com',
 'X-Failed-Recipients': 'recipient@gmail.com'}

Firstly, how do I tell that this is a DSN without using a regexp on the subject?

Secondly, how do I access the body of the email, and information such as the error that was returned by the mail server?

edit: worked out I need to use .get_payload() to get the contents of the message.

The email docs say:

The Parser class has no differences in its public interface. It does
have some additional smarts to
recognize message/delivery-status type
messages
, which it represents as a
Message instance containing separate
Message subparts for each header block
in the delivery status notification


Update:

Basically, I need to be able to reliable detect that an email is a DSN, and then also to extract the original message so I can parse that with email.Parser() and get information about it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:45:57+00:00Added an answer on May 20, 2026 at 2:45 pm

    The docs you cited says that the message is multi-part if it is DSN:

    import email
    
    msg = email.message_from_string(emailstr)
    
    if (msg.is_multipart() and len(msg.get_payload()) > 1 and 
        msg.get_payload(1).get_content_type() == 'message/delivery-status'):
        # email is DSN
        print(msg.get_payload(0).get_payload()) # human-readable section
        
        for dsn in msg.get_payload(1).get_payload():
            print('action: %s' % dsn['action']) # e.g., "failed", "delivered"
            
        if len(msg.get_payload()) > 2:
            print(msg.get_payload(2)) # original message
    

    Format of a Delivery Status Notification (from rfc 3464):

    A DSN is a MIME message with a top-level content-type of
    multipart/report (defined in [REPORT]).  When a multipart/report
    content is used to transmit a DSN:
    
    (a) The report-type parameter of the multipart/report content is
        "delivery-status".
    
    (b) The first component of the multipart/report contains a human-
        readable explanation of the DSN, as described in [REPORT].
    
    (c) The second component of the multipart/report is of content-type
        message/delivery-status, described in section 2.1 of this
        document.
    
    (d) If the original message or a portion of the message is to be
        returned to the sender, it appears as the third component of the
        multipart/report.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.