How do I determine if an email is Base64 encoded?

江枫思渺然 提交于 2020-01-02 05:15:10

问题


I am having difficulty determining if the body of a text email message is base64 encoded. if it is then use this line of code; making use of jython 2.2.1

dirty=base64.decodestring(dirty)

else continue as normal.

This is the code I have atm. What line of code will allow me to extract this from the email:

"Content-Transfer-Encoding: base64"

import email, email.Message
import base64

def _get_email_body(self):
    try:
        parts=self._email.get_payload()
        check=parts[0].get_content_type()
        if check=="text/plain":
            part=parts[0].get_payload()
            enc = part[0]['Content-Transfer-Encoding']
            if enc == "base64":
                dirty=base64.decodestring(dirty)
        elif check=="multipart/alternative":
            part=parts[0].get_payload()
            enc = part[0]['Content-Transfer-Encoding']
            if part[0].get_content_type()=="text/plain":
                dirty=part[0].get_payload()
                if enc == "base64":
                    dirty=base64.decodestring(dirty)
            else:
                return "cannot obtain the body of the email"
        else:
            return "cannot obtain the body of the email"
        return dirty
    except:
        raise

OKAY this code works now! thanks all


回答1:


Try:

enc = msg['Content-Transfer-Encoding']

It's a header so you won't be able to get it looking at the body. You should be able to get at the same place you find out the Subject.




回答2:


It is a header but you have to get the payload first from the message.

It'll be:

header = msg.get_payload()[0]

header['Content-Transfer-Encoding']

I'm using Python 3



来源:https://stackoverflow.com/questions/324980/how-do-i-determine-if-an-email-is-base64-encoded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!