if msg.is_multipart() and len(msg.get_payload()) > 1 and msg.get_payload(1
    ).get_content_type() == 'message/delivery-status':
    print(msg.get_payload(0).get_payload())
for dsn in msg.get_payload(1).get_payload():
    print('action: %s' % dsn['action'])
if len(msg.get_payload()) > 2:
    pass
Detecting if an email is a "Delivery Status Notification" and extract information - Python