如何确定是否通过imap base64提取的邮件编码? [英] How to determine if a mail fetch by imap base64 encoded?

查看:79
本文介绍了如何确定是否通过imap base64提取的邮件编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将整个邮件另存为xx.eml,但是某些邮件正文告诉邮件第一行使用base64进行编码,例如:

I saved the whole message as xx.eml, but some mails body tells that mail is encoding by base64 at the first line, for example:

charset="utf-8" Content-Transfer-Encoding: base64   
charset="gb2312" Content-Transfer-Encoding: base64     

我试图获取 body [0] [1] 的键,但是没有内容传输编码字段(仅内容类型).

I tried to get the keys of body[0][1], but there is no content-transfer-encoding field (only content-type).

我如何处理该邮件?

def saveMail(conn, num):

    typ, body = conn.fetch(num, 'RFC822')

    message = open(emldirPath + '\\' + num + '.eml', 'w+')
    message.write(str(email.message_from_string(body[0][1])))

    print email.message_from_string(body[0][1]).keys()
    #['Received', 'Return-Path', 'Received', 'Received', 'Date', 'From', 'To',
    # 'Subject', 'Message-ID', 'X-mailer', 'Mime-Version', 'X-MIMETrack',
    # 'Content-Type', 'X-Coremail-Antispam']

    message.close()

我发现了问题,这不是解码问题.

I found the problem, it's not decoding problem.

正确的邮件如下:
------ = _ Part_446950_1309705579.1326378953207
内容类型:文本/纯文本;charset = GBK
内容传输编码:base64

right mail as follow:
------=_Part_446950_1309705579.1326378953207
Content-Type: text/plain; charset=GBK
Content-Transfer-Encoding: base64

我的程序下载了什么:
------ = _ Part_446950_1309705579.1326378953207
内容类型:文本/纯文本;
charset ="utf-8"
内容传输编码:base64

what my program download:
------=_Part_446950_1309705579.1326378953207
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: base64

当我的程序保存.eml文件时,它会在'text/plain;'之后更改行;
因此,Outlook Express无法解析邮件如果我将行编辑为"Content-Type:text/html; charset =" utf-8",
可以

when my program save the .eml file, it change line after 'text/plain;'
therefore outlook express can't parse the mail if I edit the line to ""Content-Type: text/html;charset="utf-8"",
it works

现在的问题是:如何编辑我的程序以使其不更改行号?

Now the question is: how to edit my program to not let it change line?

推荐答案

以BASE64格式传输的电子邮件必须设置 Content-Transfer-Encoding .但是,您最有可能处理MIME/Multipart消息(例如,同一消息中的文本/纯文本和HTML),在这种情况下,将为每个部分分别设置传输编码.您可以使用 is_multipart() Content-Type 是否为 multipart/alternative 进行测试.在这种情况下,您可以使用 walk 来遍历不同的部分.

Emails that are transfered as BASE64 must set Content-Transfer-Encoding. However you are most likely dealing with a MIME/Multipart message (e.g. both text/plain and HTML in the same message), in which case the transfer encoding is set separately for each part. You can test with is_multipart() or if Content-Type is multipart/alternative. If that is the case you use walk to iterate over the different parts.

使用带引号的可打印文本和纯文本以及使用BASE64的HTML发送文本/纯文本是很正常的.

It is quite normal to send text/plain using quoted-printable and HTML using BASE64.

Content-Type: multipart/alternative; boundary="=_d6644db1a848db3cb25f2a8973539487"
Subject: multipart sample
From: Foo Bar <foo@example.net>
To: Fred Flintstone <fred@example.net>

--=_d6644db1a848db3cb25f2a8973539487
Content-Transfer-Encoding: base64
Content-Type: text/plain; charset=utf-8

SOME BASE64 HERE
--=_d6644db1a848db3cb25f2a8973539487
Content-Transfer-Encoding: base64
Content-Type: text/html; charset=utf-8

AND SOME OTHER BASE64 HERE

这篇关于如何确定是否通过imap base64提取的邮件编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆