AppleScript - 将电子邮件提取为MIME对象

时间:2018-05-17 08:07:26

标签: macos applescript mime apple-mail

我有一个我编写的AppleScript来对Mail.app消息进行一些解析,但似乎我需要比AppleScript提供的更强大的处理(特别是 - 从它引用的原始消息中分离回复的消息)(比如说,使用Python的email包。是否可以将电子邮件作为MIME字符串获取?

1 个答案:

答案 0 :(得分:1)

我不确定这是不是您的意思,但是您可以通过以下方式获取您在Mail.app中所做的选择中的原始邮件文本,而这些选择可以使用MIME工具进行处理。提取所有部分。

<div class="contato">
    <form name="contato" method="POST" netlify>
     
        <div class="header">
           <h1>Contato</h1>
        </div>

        <div class="preencher">
            <p>
                <label>Nome: <input type="text" name="nome"></label>
            </p>
            <p>
                <label>E-mail: <input type="email" name="email"></label>
                </p>
                <p>
                   <label>Empresa/site: <input cols="60" type="email" name="empresa"></label>
                </p>
                <p>
                    <label>Mensagem: <textarea name="mensagem"></textarea></label>
                </p>
                <p>
                    <button type="submit">Send</button>
                </p>
        </div>
    </form>
</div>

然后这是一个Python电子邮件示例脚本,它解压缩消息并将每个MIME部分写入目录中的单独文件

https://docs.python.org/3.4/library/email-examples.html

tell application "Mail"
    set msgs to selection
    if length of msgs is not 0 then
        repeat with msg in msgs

            set messageSource to source of msg

            set textFile to "/Users/harley/Desktop/foo.txt"

            set myFile to open for access textFile with write permission
            write messageSource to myFile
            close access myFile

        end repeat
    end if
end tell

那么如果unpack.py脚本在AppleScript输出上运行......

#!/usr/bin/env python3

"""Unpack a MIME message into a directory of files."""

import os
import sys
import email
import errno
import mimetypes

from argparse import ArgumentParser


def main():
    parser = ArgumentParser(description="""\
Unpack a MIME message into a directory of files.
""")
    parser.add_argument('-d', '--directory', required=True,
                        help="""Unpack the MIME message into the named
                        directory, which will be created if it doesn't already
                        exist.""")
    parser.add_argument('msgfile')
    args = parser.parse_args()

    with open(args.msgfile) as fp:
        msg = email.message_from_file(fp)

    try:
        os.mkdir(args.directory)
    except FileExistsError:
        pass

    counter = 1
    for part in msg.walk():
        # multipart/* are just containers
        if part.get_content_maintype() == 'multipart':
            continue
        # Applications should really sanitize the given filename so that an
        # email message can't be used to overwrite important files
        filename = part.get_filename()
        if not filename:
            ext = mimetypes.guess_extension(part.get_content_type())
            if not ext:
                # Use a generic bag-of-bits extension
                ext = '.bin'
            filename = 'part-%03d%s' % (counter, ext)
        counter += 1
        with open(os.path.join(args.directory, filename), 'wb') as fp:
            fp.write(part.get_payload(decode=True))


if __name__ == '__main__':
    main()

您将获得一个MIME部分分开的目录。当我在引用原始邮件的邮件上运行此邮件时,原始邮件将显示在单独的部分中。