说明

eml文件中的发件人From、收件人To、抄送人Cc有时包含中文,用foxmail查看时显示乱码。
本程序将发件人、收件人、抄送人修改为base64,utf8格式的,以便用foxmail查看时,正常显示。
经过实测,修改后eml文件用foxmail可以正常显示发件人、收件人、抄送人,导入Foxmail也正常。

Python代码如下:

# -*- coding: utf-8 -*-import base64
import os
import re# 根据isdebug,决定日志的输出量
isdebug = True
isdebug = Falsedef tobase64names(start_str, line):"""    按照关键字修改文件的某一行内容:param start_str: 关键字,在每行开头出现:param line: 文件的一行内容:return: rtn 修改后的内容"""rtn = 'null'base64_names = ''if line.startswith(start_str):str_names = line.replace(start_str, '')str_names = str_names.replace('\n', '')if str_names.find(',') >= 0:if isdebug:print("%s 多个姓名:%s" % (start_str, str_names))names = re.split(r'(?:,)', str_names)base64_names = ''for s in names:if (not s.startswith('=?utf-8?B?')) and s != ',' and len(s) > 0:if isdebug:print('姓名列表:[%s]' % s)bytesString = s.encode(encoding="utf-8")base64_names += '=?utf-8?B?' + base64.b64encode(bytesString).decode() + ','if isdebug:print('base64后的姓名:[%s]' % base64_names)else:s = str_namesif (not s.startswith('=?utf-8?B?')) and len(s) > 0:if isdebug:print("单个姓名:[%s]" % s)bytesString = s.encode(encoding="utf-8")base64_names = '=?utf-8?B?' + base64.b64encode(bytesString).decode()if isdebug:print('base64后的姓名:[%s]' % base64_names)rtn = start_str + base64_names + '\n'if isdebug:print(rtn)return rtndef alter_eml(sourceeemlfile, destemlfile):"""    修改单个eml文件函数:param sourceeemlfile: 原始eml文件:param destemlfile: 修改后的eml文件:return: None"""with open(sourceeemlfile, "r", encoding="GB18030") as f1, open("%s" % destemlfile, "w", encoding="GB18030") as f2:for line in f1:# 处理发件人,逐个修改为base64,utf8格式的str_rtn = tobase64names('From:', line)if str_rtn != 'null':line = str_rtn# 处理收件人,逐个修改为base64,utf8格式的str_rtn = tobase64names('To:', line)if str_rtn != 'null':line = str_rtn# 处理抄送人,逐个修改为base64,utf8格式的str_rtn = tobase64names('Cc:', line)if str_rtn != 'null':line = str_rtnf2.write(line)def main():"""eml文件中的发件人From、收件人To、抄送人Cc有时包含中文,用foxmail查看时显示乱码。本程序将发件人、收件人、抄送人修改为base64,utf8格式的,以便用foxmail查看时,正常显示。"""# 待修改eml文件所在路径sourcedir = 'D:\\temp\\'# 修改后文件所在路径destdir = 'D:\\temp1\\'# 循环检索sourcedir目录下所有的eml文件,逐一修改后,放在destdir下for root, dirs, files in os.walk(sourcedir):for file in files:# 只处理文件后缀名为“.eml”的文件if file.endswith('.eml'):try:eml = os.path.join(root, file)neweml = os.path.join(root, file).replace(sourcedir, destdir)# 根据原邮件所在的子目录,在新目录下创建同名子目录if not os.path.isdir(neweml.replace(file, '')):os.makedirs(neweml.replace(file, ''), exist_ok=True)except Exception as e:if isdebug:print(neweml)print(e)finally:alter_eml(eml, neweml)print(neweml)if isdebug:print(eml)# 此处参考网友的
# def main():
#     temp_str = 'base64编码、解码测试'
#
#     # 编码
#     bytesString = temp_str.encode(encoding="utf-8")
#     encode_str = base64.b64encode(bytesString).decode()
#     print("编码后:", encode_str)
#
#     # 解码
#     decode_str = base64.b64decode(encode_str).decode()
#     print('解码后:', decode_str)
#if __name__ == '__main__':main()

.eml文件发件人From、收件人To、抄送人Cc中包含中文,显示乱码的问题解决办法相关推荐

  1. 在一个JS文件中包含中文字符串,通过innerHTML输出后中文乱码?

    在一个JS文件中包含中文字符串,通过innerHTML输出后中文乱码? Posted on 2008-07-13 12:00 尹合磊 阅读(1902) 评论(0)  编辑 收藏 所属分类: ASP.N ...

  2. c++日志文件中文显示乱码的问题及解决办法

    近段时间在做战斗系统,昨天观察日志时发现中文显示乱码,而且奇怪的是有些中文日志是正常的,有些中文日志乱码 就是下面这种表现 Debug - Thu Oct 24 20:42:35 2019 : sys ...

  3. HTML 表单 存为EXCEL文件时 中文显示乱码

    在做宣传品发放系统时,需求要把数据库查询的记录生成表单并转存excel文件. 在转存的EXCEL文件中文显示乱码,表格和其他字符正常,检查后发现是创建EXCEL文件打开模式不对 之前: myfile ...

  4. Python正则表达式工具类文件的封装实例,提供了多个实例,并且在代码中包含中文注释

    Python正则表达式工具类文件的封装实例,提供了多个实例,并且在代码中包含中文注释 import reclass RegexUtils:'''正则表达式工具类'''def __init__(self ...

  5. python存成csv文件时中文乱码_Python在向CSV文件写中文时乱码的处理办法

    前言 python2最大的坑在于中文编码问题,遇到中文报错首先加u,再各种encode.decode. 当list.tuple.dict里面有中文时,打印出来的是Unicode编码,这个是无解的. 对 ...

  6. html文件浏览器乱码怎么解决方法,【学习笔记一】HTML文件在浏览器中文显示乱码的原因及解决办法...

    最近在学习前端html相关知识(备注:前端小白一枚),准备从移动端开发扩展到前端开发,希望做一个大前端,即前端全栈工程师.为什么要转向前端学习开发,在后面的文章中,我会详细阐述这个原因和相关心得. 在 ...

  7. python 生成html文件浏览器_python如何将数据生成html文件+浏览器中文显示乱码问题...

    需求:从msysql数据库查询数据,并生成html文件,后自动发送邮件(html格式),在网上找了许久,终于找到2种解决方法! 一.近来在网上采集数据,想把采集下来的数据整合成html的形式保存.以便 ...

  8. 详解Intellij IDEA中.properties文件中文显示乱码问题的解决

    首先,你可能会见到如下提示: File encoding is disabled because .properties file (see Settings|Editor|File Encoding ...

  9. python 从数据库 提取数据 生成html_python如何将数据生成html文件+浏览器中文显示乱码问题...

    需求:从msysql数据库查询数据,并生成html文件,后自动发送邮件(html格式),在网上找了许久,终于找到2种解决方法! 一.近来在网上采集数据,想把采集下来的数据整合成html的形式保存.以便 ...

最新文章

  1. Personal preference
  2. jqPlot图表插件使用说明(一)
  3. thymeleaf与jsp_PagingAndSortingRepository –如何与Thymeleaf一起使用
  4. md5模式 签名_MD的完整形式是什么?
  5. 组件文件已损坏或android内部模块,android - Android Q更新后,模块化系统组件在托管配置文件中不可用 - 堆栈内存溢出...
  6. P4313 文理分科 网络流
  7. 毕设日志——Linux中相对路径和文件的移动和常用指令
  8. WebAPI2使用Autofac实现IOC属性注入完美解决方案
  9. WPF中实现图片文件转换成Visual对象,Viewport3D对象转换成图片
  10. OriginPro中三维图片旋转
  11. Pytorch框架--知识图谱可视化展示
  12. 苹果是如何让iOS12加速的
  13. busboy中文文件名乱码问题
  14. 自如上线分期支付,租金贷发展受限后充分利用流量变现
  15. Delphi Thread 多线程编程(6)
  16. iastora怎么改成ahci_Win10系统无需重装,硬盘IDE改为AHCI模式的方法
  17. centos7部署openwhisk
  18. IoT物联网——各大厂质量保障实践分享汇总(智能语音视频篇)
  19. 【Python游戏】Python实现一个Q版泡泡堂小游戏 | 附带源码
  20. 全媒体运营 之 受众分析

热门文章

  1. 华北电力大学毕业论文格式修改---EndNote插入参考文献的各种错误修改%J、endnote自动定位参考文献
  2. RV1126----新增sensor--imx214
  3. 自制光立方(制作中)
  4. 被AI包裹的冬天,智慧供热打破城市供热的“不可能三角”
  5. avclient安装_反病毒软件clamAV源代码分析 01 - 安装编译
  6. JavaSE小游戏(美女漫画拼图)
  7. 阿里云出事了,被工信部处罚,它真不冤,但也别慌
  8. 得推校园O2O系统为校园创业提供技术支持
  9. 手机遥控电脑软件(BtControid)
  10. 开学季真无线蓝牙耳机怎么选?品质好的蓝牙耳机推荐