i was looking to automate email with outlook, i needed to click a button and open a pre configurated email body, subject and attachment in outlook, this way i could edit or make changes.
Im using Mac OS.
Found this Automate Outlook on Mac with Python and this seems to be what i need but im getting errors
Can someone help?
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 = ['[email protected]', '[email protected]']
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()
Getting this error:
raise CommandError(self, (args, kargs), e, self.AS_appdata) from e appscript.reference.CommandError: Command failed: OSERROR: -1743 MESSAGE: The user has declined permission. COMMAND: app('/Applications/Microsoft Outlook.app').make(new=k.outgoing_message, with_properties={k.subject: 'This is an important email!', k.content: 'Just kidding its not.'})
Are you using the 'new outlook' version of Outlook for Mac? If so, Applescript is not supported.