def get_email_content(self, email):

        if email.is_multipart():
            self.log('Extracting email contents from multipart message')

            magic_type = 'multipart/alternative'
            payload_types = dict((p.get_content_type(), i) for i, p in enumerate(email.get_payload()))
            if magic_type in payload_types.keys():
                self.log('Found magic content type: %s' % magic_type)
                index = payload_types[magic_type]
                payload = email.get_payload()[index].get_payload()
            else:
                payload = email.get_payload()

            for pl in payload:
                if pl.get_filename() is not None:
                    # it's an attached file
                    continue

                if pl.get_content_type() in ACCEPTABLE_TYPES:
                    return pl.get_payload()
        else:
            return email.get_payload()

        return None
Attempts to extract an email's content
dataset/ETHPy150Open codekoala/django-articles/articles/management/commands/check_for_articles_from_email.py/Command.get_email_content