You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过AppleScript提取Mail.app邮件的MIME格式字符串?

Yes, You Can Grab the Raw MIME Source via AppleScript

Absolutely! This is the perfect workaround—AppleScript can pull the full raw MIME source of any email in Mail.app, which you can then hand off to Python's email library for the heavy lifting of separating replies from quoted original content.

Step 1: Fetch the MIME Source with AppleScript

The key property here is source of a Mail.app email object—it returns the complete MIME-formatted string, including all headers, encoded body content, and attachment data. Here's a quick example to grab the selected email's MIME source and save it to a temporary file (easy to pass to Python):

tell application "Mail"
    -- Target the currently selected email (adjust this to target mailboxes/threads as needed)
    set selectedEmails to selection
    if selectedEmails is not {} then
        set targetEmail to item 1 of selectedEmails
        -- Grab the full raw MIME content
        set mimeSource to source of targetEmail
        
        -- Save to a temporary .eml file (avoids issues with long string arguments)
        set tempFolder to POSIX path of (path to temporary items folder as text)
        set tempFilePath to tempFolder & "email_to_parse.eml"
        write mimeSource to file tempFilePath as «class utf8»
        
        -- Optional: Call your Python script directly from here
        do shell script "/usr/bin/python3 /path/to/your/parse_email.py " & quoted form of tempFilePath
    else
        display dialog "Please select an email first!"
    end if
end tell

Step 2: Parse the MIME Content in Python

Once you have the .eml file, Python's email module can fully parse it to extract and process the message body. Here's a basic example to get you started with separating reply content from quoted text:

import email
from email.parser import BytesParser
from email.policy import default

def parse_email(mime_file_path):
    with open(mime_file_path, "rb") as f:
        msg = BytesParser(policy=default).parse(f)
    
    # Extract the plain text body (adjust for HTML if needed)
    body_content = ""
    if msg.is_multipart():
        for part in msg.walk():
            if part.get_content_type() == "text/plain":
                # Decode quoted-printable or base64 encoded content
                body_content = part.get_content()
                break
    else:
        body_content = msg.get_content()
    
    # Now you can process body_content to split reply vs quoted text
    # For example, split on common quote markers like ">" or "On [date], [sender] wrote:"
    lines = body_content.split("\n")
    reply_lines = []
    quoted_lines = []
    in_quote = False
    
    for line in lines:
        if line.strip().startswith(">") or "wrote:" in line:
            in_quote = True
            quoted_lines.append(line)
        else:
            if in_quote and line.strip() == "":
                # Sometimes quotes are separated by blank lines
                continue
            reply_lines.append(line)
    
    print("Reply Content:\n" + "\n".join(reply_lines))
    print("\nQuoted Original Content:\n" + "\n".join(quoted_lines))

if __name__ == "__main__":
    import sys
    if len(sys.argv) > 1:
        parse_email(sys.argv[1])

Quick Tips for Integration

  • Avoid passing MIME content directly as a shell argument: Long MIME strings can hit character limits or encoding issues—using a temporary .eml file is far more reliable.
  • Handle encoding properly: The email module's default policy automatically handles most MIME encodings, so you don't have to manually decode quoted-printable or base64 content.
  • Batch processing: Modify the AppleScript to loop through all emails in a mailbox instead of just selected ones if you need to process multiple messages.

This combination plays to both tools' strengths: AppleScript's deep integration with Mail.app, and Python's robust email parsing capabilities. It's exactly the solution you need for separating replies from quoted original content.

内容的提问来源于stack exchange,提问作者MattS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:17:00