如何确定电子邮件是否经过 Base64 编码?

发布于 2024-07-09 16:30:58 字数 1188 浏览 5 评论 0原文

我很难确定文本电子邮件的正文是否采用 Base64 编码。 如果是,则使用这行代码; 使用 jython 2.2.1

dirty=base64.decodebytes(dirty)

,否则继续正常。

这是我atm 上的代码。 哪一行代码可以让我从电子邮件中提取此内容:

“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.decodebytes(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.decodebytes(dirty)
            else:
                return "cannot obtain the body of the email"
        else:
            return "cannot obtain the body of the email"
        return dirty
    except:
        raise

好的,此代码现在可以使用了! 谢谢大家

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.decodebytes(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.decodebytes(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.decodebytes(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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

大姐,你呐 2024-07-16 16:30:58

尝试:

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

它是一个标题,因此您无法通过正文来获取它。 您应该能够到达找到主题的同一位置。

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.

烂柯人 2024-07-16 16:30:58

它是一个标头,但您必须首先从消息中获取有效负载。

它将是:

header = msg.get_payload()[0]
header['Content-Transfer-Encoding']

我正在使用 Python 3

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文