0

我正在寻找使用 Outlook 自动化电子邮件,我需要单击一个按钮并在 Outlook 中打开预先配置的电子邮件正文、主题和附件,这样我就可以编辑或进行更改。

我正在使用 Mac 操作系统。

在 Mac 上使用 Python找到了这个Automate Outlook,这似乎是我需要的,但我遇到了错误

有人可以帮忙吗?

from tkinter.filedialog import askopenfilename
from appscript import app, k
from mactypes import Alias
from pathlib import Path

def create_message_with_attachment():
    subject = 'This is an important email!'
    body = 'Just kidding its not.'
    to_recip = ['test0@hotmail.com', 'test1@gmail.com']

    msg = Message(subject=subject, body=body, to_recip=to_recip)

    # attach file
    file = askopenfilename(initialdir='Downloads/')
    if not file:
        return
    else:
        p = Path(file)
        msg.add_attachment(p)

    msg.show()

class Outlook(object):
    def __init__(self):
        self.client = app('Microsoft Outlook')

class Message(object):
    def __init__(self, parent=None, subject='', body='', to_recip=[], cc_recip=[], show_=True):

        if parent is None: parent = Outlook()
        client = parent.client

        self.msg = client.make(
            new=k.outgoing_message,
            with_properties={k.subject: subject, k.content: body})

        self.add_recipients(emails=to_recip, type_='to')
        self.add_recipients(emails=cc_recip, type_='cc')

        if show_: self.show()

    def show(self):
        self.msg.open()
        self.msg.activate()

    def add_attachment(self, p):
        # p is a Path() obj, could also pass string

        p = Alias(str(p)) # convert string/path obj to POSIX/mactypes path

        attach = self.msg.make(new=k.attachment, with_properties={k.file: p})

    def add_recipients(self, emails, type_='to'):
        if not isinstance(emails, list): emails = [emails]
        for email in emails:
            self.add_recipient(email=email, type_=type_)

    def add_recipient(self, email, type_='to'):
        msg = self.msg

        if type_ == 'to':
            recipient = k.to_recipient
        elif type_ == 'cc':
            recipient = k.cc_recipient

        msg.make(new=recipient, with_properties={k.email_address: {k.address: email}})

create_message_with_attachment()

收到此错误:

raise CommandError(self, (args, kargs), e, self.AS_appdata) from e appscript.reference.CommandError: Command failed: OSERROR: -1743 MESSAGE: The user has denied permission. COMMAND: app('/Applications/Microsoft Outlook.app').make(new=k.outgoing_message, with_properties={k.subject: '这是一封重要的电子邮件!', k.content: '只是在开玩笑。' })

4

0 回答 0