How to determine if a mail fetch by imap base64 encoded?

≯℡__Kan透↙ 提交于 2021-02-10 18:34:12

问题


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     

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

How can I process that mails?

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.

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

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

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?


回答1:


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.

EDIT: 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


来源:https://stackoverflow.com/questions/9108358/how-to-determine-if-a-mail-fetch-by-imap-base64-encoded

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