【Python3脚本】导出个人腾讯企业邮箱的全部邮件并保存成eml

由于邮箱容量不足,作者通过Python3编写IMAP脚本,高效地将腾讯企业邮箱的全部邮件导出为eml文件,以解决存储问题。脚本避免了使用Foxmail导出速度慢的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景

这两天收到系统通知邮件,提示我的企业邮箱空间容量不够,再过两天就无法接收邮件。因此,我看了一下邮箱,想着将这些邮件归档存到本地。

我尝试过用Foxmail导出邮件,但是速度太慢了。

突发奇想,让ChatGPT帮我写一个脚本,用Python3来实现IMAP导出全部邮件。

经过几次代码优化迭代,形成了下面可以导出的脚本。

代码实现

import hashlib
import imaplib
import os
import email
from email.header import decode_header
from datetime import datetime
from imapclient import imap_utf7


def get_decoded_header(header: str) -> str:
    try:
        decoded_header = email.header.decode_header(header)
        header_value, encoding = decoded_header[0]

        if isinstance(header_value, bytes):
            try:
                if encoding and encoding.lower() != "unknown-8bit":
                    return header_value.decode(encoding)
                else:
                    raise LookupError("unknown-8bit encoding")
            except (UnicodeDecodeError, LookupError):
                # 尝试使用备选编码进行解码
                alt_encodings = ["gb18030", "gbk", "gb2312", "big5", "utf-8"]
                for alt_encoding in alt_encodings:
                    try:
                        return header_value.decode(alt_encoding)
                    except UnicodeDecodeError:
                        continue
                return header_value.decode("utf-8", errors="replace")

        return header_value
    except Exception as e:
        print(f"Error while decoding header: {
     e<
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄旺鑫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值