{"id":189,"date":"2025-06-30T18:03:02","date_gmt":"2025-06-30T10:03:02","guid":{"rendered":"https:\/\/www.idcyz.com\/blog\/?p=189"},"modified":"2025-06-30T18:03:02","modified_gmt":"2025-06-30T10:03:02","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e6%8f%90%e5%8f%96eml%e6%96%87%e4%bb%b6%e4%b8%ad%e7%9a%84%e9%99%84%e4%bb%b6%e5%b9%b6%e5%a4%84%e7%90%86%e5%b8%b8%e8%a7%81%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/www.idcyz.com\/blog\/189.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Python\u63d0\u53d6EML\u6587\u4ef6\u4e2d\u7684\u9644\u4ef6\u5e76\u5904\u7406\u5e38\u89c1\u95ee\u9898"},"content":{"rendered":"<p><p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.idcyz.com\/blog\/wp-content\/uploads\/2025\/06\/L1Hr59.jpg\" alt=\"\u5982\u4f55\u4f7f\u7528Python\u63d0\u53d6EML\u6587\u4ef6\u4e2d\u7684\u9644\u4ef6\u5e76\u5904\u7406\u5e38\u89c1\u95ee\u9898\" title=\"\u5982\u4f55\u4f7f\u7528Python\u63d0\u53d6EML\u6587\u4ef6\u4e2d\u7684\u9644\u4ef6\u5e76\u5904\u7406\u5e38\u89c1\u95ee\u9898\"><\/p>\n<p><h3>1. Python \u4eceEML\u4e2d\u63d0\u53d6\u9644\u4ef6<\/h3>\n<\/p>\n<p> \u4f7f\u7528 Python \u63d0\u53d6 EML \u6587\u4ef6\u4e2d\u7684\u9644\u4ef6\u662f\u4e00\u4e2a\u76f8\u5bf9\u7b80\u5355\u7684\u8fc7\u7a0b\uff0c\u53ea\u9700\u4f7f\u7528\u4e00\u4e9b\u6d41\u884c\u7684\u5e93\u3002\u5728\u8fd9\u65b9\u9762\uff0c\u6211\u4eec\u5f3a\u70c8\u63a8\u8350\u4f7f\u7528\u4ee5\u4e0b\u5e93\uff1a<\/p>\n<\/p>\n<p><p>1. <b>email<\/b>\uff1a\u6b64\u5e93\u662f Python \u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u7528\u4e8e\u89e3\u6790\u548c\u5904\u7406\u7535\u5b50\u90ae\u4ef6\u3002\u5b83\u80fd\u591f\u5904\u7406 EML \u6587\u4ef6\u4e2d\u7684\u4e0d\u540c MIME \u7c7b\u578b\uff0c\u8fd8\u80fd\u4ece\u4e2d\u63d0\u53d6\u5404\u7c7b\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>2. <b>imaplib<\/b>\uff1a\u5982\u679c\u4f60\u6253\u7b97\u4ece\u90ae\u7bb1\u4e2d\u4e0b\u8f7d EML \u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u5e93\u6765\u8fde\u63a5\u5230\u90ae\u7bb1\uff0c\u4e0b\u8f7d\u90ae\u4ef6\u3002<\/p>\n<\/p>\n<p><p>3. <b>os<\/b>\uff1a\u7528\u6765\u521b\u5efa\u548c\u5b58\u50a8\u9644\u4ef6\u6587\u4ef6\u7684\u7cfb\u7edf\u64cd\u4f5c\u5e93\u3002<\/p>\n<\/p>\n<p><p>4. <b>re<\/b>\uff1a\u7528\u4e8e\u5904\u7406\u548c\u63d0\u53d6\u6587\u4ef6\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u786e\u4fdd\u6587\u4ef6\u540d\u7684\u5408\u6cd5\u6027\u3002<\/p>\n<\/p>\n<p><p>5. <b>pathlib<\/b>\uff1a\u5e2e\u52a9\u5904\u7406\u6587\u4ef6\u8def\u5f84\uff0c\u53ef\u4ee5\u7528\u6765\u5c06\u9644\u4ef6\u4fdd\u5b58\u5230\u6307\u5b9a\u6587\u4ef6\u5939\u4e2d\u3002<\/p>\n<\/p>\n<p>\n<h3>2. EML \u6587\u4ef6\u7ed3\u6784<\/h3>\n<\/p>\n<p> \u5728\u8fdb\u884c\u9644\u4ef6\u63d0\u53d6\u524d\uff0c\u4e86\u89e3 EML \u6587\u4ef6\u7684\u7ed3\u6784\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002EML \u6587\u4ef6\u901a\u5e38\u4ee5\u6587\u672c\u683c\u5f0f\u5b58\u50a8\uff0c\u4f7f\u7528 MIME \u534f\u8bae\u6765\u63cf\u8ff0\u90ae\u4ef6\u7684\u5404\u4e2a\u90e8\u5206\uff0c\u5305\u62ec\u9644\u4ef6\u3002\u90ae\u4ef6\u7684\u4e3b\u4f53\u53ef\u80fd\u662f\u7eaf\u6587\u672c\u3001HTML\uff0c\u6216\u5176\u5b83\u683c\u5f0f\uff0c\u800c\u9644\u4ef6\u4f1a\u4ee5\u7279\u5b9a\u7684 MIME \u683c\u5f0f\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><p>EML \u6587\u4ef6\u7684\u57fa\u672c\u7ed3\u6784\u4e00\u822c\u5305\u542b\u4ee5\u4e0b\u51e0\u4e2a\u90e8\u5206\uff1a<\/p>\n<\/p>\n<p><p>&#8211; <b>\u5934\u90e8\uff08Header\uff09<\/b>\uff1a\u5305\u542b\u53d1\u4ef6\u4eba\u3001\u6536\u4ef6\u4eba\u3001\u4e3b\u9898\u7b49\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>&#8211; <b>\u4e3b\u4f53\uff08Body\uff09<\/b>\uff1a\u90ae\u4ef6\u7684\u4e3b\u8981\u5185\u5bb9\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><p>&#8211; <b>\u9644\u4ef6\uff08Attachment\uff09<\/b>\uff1a\u4ee5\u7279\u5b9a MIME \u7c7b\u578b\u5206\u522b\u5b58\u50a8\u7684\u6587\u4ef6\uff0c\u901a\u5e38\u9700\u8981\u89e3\u6790\u51fa\u6765\u624d\u80fd\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<p>\n<h3>3. \u63d0\u53d6\u9644\u4ef6\u7684\u57fa\u672c\u6b65\u9aa4<\/h3>\n<\/p>\n<p> \u63d0\u53d6\u9644\u4ef6\u7684\u8fc7\u7a0b\u867d\u7136\u770b\u8d77\u6765\u590d\u6742\uff0c\u4f46\u5b9e\u9645\u4e0a\u5206\u4e3a\u51e0\u4e2a\u7b80\u5355\u7684\u6b65\u9aa4\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u8bfb\u53d6 EML \u6587\u4ef6\uff1b\u7136\u540e\u89e3\u6790\u6587\u4ef6\u7684\u5934\u90e8\u548c\u4e3b\u4f53\uff1b\u63a5\u7740\uff0c\u63d0\u53d6\u5176\u4e2d\u7684\u9644\u4ef6\u90e8\u5206\u5e76\u4fdd\u5b58\u5230\u672c\u5730\u3002<\/p>\n<\/p>\n<p><p>\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a<\/p>\n<\/p>\n<p><p>&#8211; <b>\u8bfb\u53d6 EML \u6587\u4ef6<\/b>\uff1a\u4f7f\u7528 Python \u7684\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u6253\u5f00 EML \u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>&#8211; <b>\u89e3\u6790\u90ae\u4ef6\u5185\u5bb9<\/b>\uff1a\u4f7f\u7528 email \u5e93\u7684\u529f\u80fd\u89e3\u6790\u90ae\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>&#8211; <b>\u63d0\u53d6\u5e76\u4fdd\u5b58\u9644\u4ef6<\/b>\uff1a\u68c0\u67e5 MIME \u7c7b\u578b\u4e3a\u9644\u4ef6\u7684\u90e8\u5206\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u5230\u6307\u5b9a\u8def\u5f84\u3002<\/p>\n<\/p>\n<p>\n<h3>4. \u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p> \u63a5\u4e0b\u6765\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u7528\u4e8e\u5c55\u793a\u5982\u4f55\u4ece EML \u6587\u4ef6\u4e2d\u63d0\u53d6\u9644\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code><\/p>\r\n<p>import os<\/p>\r\n<p>import email<\/p>\r\n<p>from email import policy<\/p>\r\n<p>from email.parser import BytesParser<\/p>\r\n\r\n<p># \u5b9a\u4e49\u4fdd\u5b58\u9644\u4ef6\u7684\u8def\u5f84<\/p>\r\n<p>save_path = '.\/attachments'<\/p>\r\n\r\n<p># \u786e\u4fdd\u4fdd\u5b58\u8def\u5f84\u5b58\u5728<\/p>\r\n<p>os.makedirs(save_path, exist_ok=True)<\/p>\r\n\r\n<p># \u8bfb\u53d6 EML \u6587\u4ef6<\/p>\r\n<p>with open('example.eml', 'rb') as f:<\/p>\r\n<p>    msg = BytesParser(policy=policy.default).parse(f)<\/p>\r\n\r\n<p># \u904d\u5386\u90ae\u4ef6\u5185\u5bb9<\/p>\r\n<p>for part in msg.iter_parts():<\/p>\r\n<p>    if part.get_content_maintype() == 'multipart':<\/p>\r\n<p>        continue<\/p>\r\n<p>    if part.get('Content-Disposition') is not None:<\/p>\r\n<p>        # \u63d0\u53d6\u9644\u4ef6\u540d\u79f0<\/p>\r\n<p>        filename = part.get_filename()<\/p>\r\n<p>        with open(os.path.join(save_path, filename), 'wb') as att_file:<\/p>\r\n<p>            att_file.write(part.get_payload(decode=True))<\/p>\r\n<p>            print(f'\u9644\u4ef6\u5df2\u4fdd\u5b58\uff1a{filename}')<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p>\n<h3>5. \u5982\u4f55\u786e\u4fdd\u9644\u4ef6\u7684\u547d\u540d\u5408\u6cd5\uff1f<\/h3>\n<\/p>\n<p> \u5728\u63d0\u53d6\u9644\u4ef6\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u6587\u4ef6\u547d\u540d\u5408\u6cd5\u662f\u4e2a\u91cd\u8981\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528 re \u5e93\u6765\u8fc7\u6ee4\u6389\u4e0d\u5408\u6cd5\u7684\u5b57\u7b26\uff0c\u4f8b\u5982\u7a7a\u683c\u3001\u7279\u6b8a\u5b57\u7b26\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u5728\u63d0\u53d6\u6587\u4ef6\u540d\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u8fdb\u884c\u6b63\u5219\u5339\u914d\uff1a<\/p>\n<\/p>\n<p><pre><code><\/p>\r\n<p>import re<\/p>\r\n\r\n<p>def sanitize_filename(filename):<\/p>\r\n<p>    # \u8fc7\u6ee4\u6389\u4e0d\u5408\u6cd5\u5b57\u7b26<\/p>\r\n<p>    return re.sub(r'[^a-zA-Z0-9_.-]', '_', filename)<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u63d0\u53d6\u9644\u4ef6\u65f6\uff0c\u5c31\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u786e\u4fdd\u6587\u4ef6\u540d\u7684\u5408\u6cd5\u6027\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code><\/p>\r\n<p>safe_filename = sanitize_filename(filename)<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p>\n<h3>6. \u811a\u672c\u8fd0\u884c\u540e\u6709\u54ea\u4e9b\u5e38\u89c1\u9519\u8bef\uff1f<\/h3>\n<\/p>\n<p> \u8fd0\u884c\u63d0\u53d6\u9644\u4ef6\u7684\u811a\u672c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\uff0c\u4f8b\u5982\u6587\u4ef6\u672a\u627e\u5230\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u53ef\u80fd\u7684\u9519\u8bef\u4fe1\u606f\u53ca\u5176\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<\/p>\n<p><p>&#8211; <b>FileNotFoundError<\/b>\uff1a\u786e\u4fdd\u63d0\u4f9b\u7684 EML \u6587\u4ef6\u8def\u5f84\u6b63\u786e\u3002<\/p>\n<\/p>\n<p><p>&#8211; <b>PermissionError<\/b>\uff1a\u786e\u4fdd\u7a0b\u5e8f\u6709\u6743\u5728\u6307\u793a\u7684\u76ee\u5f55\u4e2d\u521b\u5efa\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>&#8211; <b>AttributeError<\/b>\uff1a\u786e\u4fdd\u90ae\u4ef6\u5185\u5bb9\u6b63\u786e\u8bfb\u53d6\uff0c\u5e76\u5b58\u5728\u672a\u89e3\u6790\u7684\u90e8\u5206\u3002<\/p>\n<\/p>\n<p>\n<h3>7. \u4f7f\u7528IMAP\u4e0b\u8f7dEML\u6587\u4ef6\u7684\u793a\u4f8b<\/h3>\n<\/p>\n<p> \u5982\u679c\u60f3\u4ece\u90ae\u7bb1\u4e2d\u76f4\u63a5\u4e0b\u8f7d EML \u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528 imaplib \u5e93\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4ece Gmail \u4e2d\u4e0b\u8f7d\u90ae\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code><\/p>\r\n<p>import imaplib<\/p>\r\n<p>import email<\/p>\r\n\r\n<p># \u8fde\u63a5\u5230IMAP\u670d\u52a1\u5668<\/p>\r\n<p>with imaplib.IMAP4_SSL('imap.gmail.com') as mail:<\/p>\r\n<p>    mail.login('your_email@gmail.com', 'your_password')<\/p>\r\n<p>    mail.select('inbox')<\/p>\r\n    \r\n<p>    # \u641c\u7d22\u90ae\u4ef6<\/p>\r\n<p>    result, data = mail.search(None, 'ALL')<\/p>\r\n    \r\n<p>    # \u4e0b\u8f7d\u7b2c\u4e00\u5c01\u90ae\u4ef6<\/p>\r\n<p>    mail_id = data[0].split()[0]<\/p>\r\n<p>    result, msg_data = mail.fetch(mail_id, '(RFC822)')<\/p>\r\n    \r\n<p>    msg = email.message_from_bytes(msg_data[0][1])<\/p>\r\n<p>    # \u4fdd\u5b58\u90ae\u4ef6\u4e3aEML\u6587\u4ef6<\/p>\r\n<p>    with open('downloaded_email.eml', 'wb') as f:<\/p>\r\n<p>        f.write(msg_data[0][1])<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p>\n<h3>8. \u4e3a\u4ec0\u4e48\u9009\u62e9\u4f7f\u7528 Python \u5904\u7406EML\u6587\u4ef6\uff1f<\/h3>\n<\/p>\n<p> <b>Python \u5904\u7406 EML \u6587\u4ef6\u7684\u4f18\u52bf\u662f\u4ec0\u4e48\uff1f<\/b> Python \u662f\u4e00\u95e8\u529f\u80fd\u9f50\u5168\u4e14\u6613\u5b66\u7684\u8bed\u8a00\uff0c\u5176\u4e30\u5bcc\u7684\u5e93\u652f\u6301\u8ba9\u5904\u7406\u5404\u79cd\u683c\u5f0f\uff0c\u5305\u62ec EML \u6587\u4ef6\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><p><b>\u5904\u7406\u5927\u89c4\u6a21\u90ae\u4ef6\u662f\u5426\u6709\u6548\uff1f<\/b> \u5f53\u7136\u53ef\u4ee5\uff01 Python \u7684\u811a\u672c\u53ef\u4ee5\u6279\u5904\u7406\u5927\u91cf\u90ae\u4ef6\uff0c\u901a\u8fc7\u7ed3\u5408 IMAP \u548c\u90ae\u4ef6\u89e3\u6790\u5e93\uff0c\u53ef\u4ee5\u9ad8\u6548\u63d0\u53d6\u6570\u636e\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u65e0\u9700\u624b\u52a8\u64cd\u4f5c\uff0c\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p><b>\u662f\u5426\u6709\u5176\u4ed6\u5de5\u5177\u53ef\u7528\u4e8e\u63d0\u53d6 EML \u9644\u4ef6\uff1f<\/b> \u5f53\u7136\uff0cPython \u4ee5\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u5de5\u5177\u53ef\u4ee5\u9009\u62e9\uff0c\u6bd4\u5982\u90ae\u4ef6\u5ba2\u6237\u7aef\u3001\u81ea\u5b9a\u4e49\u7684\u7b2c\u4e09\u65b9\u5e94\u7528\u7b49\uff0c\u4f46 Python \u7684\u7075\u6d3b\u6027\u548c\u5e7f\u6cdb\u7684\u793e\u533a\u652f\u6301\u4f7f\u5176\u6210\u4e3a\u5f88\u597d\u7684\u9009\u62e9\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Python \u4eceEML\u4e2d\u63d0\u53d6\u9644\u4ef6 \u4f7f\u7528 Python \u63d0\u53d6 EML \u6587\u4ef6\u4e2d\u7684\u9644\u4ef6\u662f\u4e00\u4e2a\u76f8\u5bf9\u7b80\u5355\u7684\u8fc7\u7a0b\uff0c\u53ea\u9700\u4f7f\u7528\u4e00\u4e9b\u6d41\u884c\u7684\u5e93\u3002\u5728\u8fd9\u65b9\u9762\uff0c\u6211\u4eec\u5f3a\u70c8\u63a8\u8350\u4f7f\u7528\u4ee5\u4e0b\u5e93\uff1a 1. email\uff1a\u6b64\u5e93\u662f Python \u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u7528\u4e8e\u89e3\u6790\u548c\u5904\u7406\u7535\u5b50\u90ae&#8230;<\/p>\n","protected":false},"author":1,"featured_media":190,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[108,110,109],"topic":[],"class_list":["post-189","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jiushu","tag-eml","tag-110","tag-109"],"_links":{"self":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/comments?post=189"}],"version-history":[{"count":1,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/189\/revisions"}],"predecessor-version":[{"id":192,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/189\/revisions\/192"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/media\/190"}],"wp:attachment":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/media?parent=189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/categories?post=189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/tags?post=189"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/topic?post=189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}