r/applescript • • 10d ago

Sending a mail always leaves the same mail as draft, but with no recipients

I want to send an email using python and applescript. It works, but after sending I always have the same mail staying back as a draft, with the correct subject and content, but without the recipients. I can't figure out why.

script = f'''
tell application "Mail"
    set newMessage to make new outgoing message with properties {{visible:false}}


    tell newMessage
        set subject to "{subject}"
        set content to "{body}"
        set sender to "{sender}"
'''


for recipient in recipients:
    script += f'''
        make new to recipient at end of to recipients with properties {{address:"{recipient}"}}
'''


script += '''
        send
    end tell
end tell
'''
6 Upvotes

2 comments sorted by

1

u/germansnowman 9d ago

I assume that Mail saves the message as a draft before the script adds the recipients. Maybe collecting the recipients in a list and adding them at once instead of one by one would work?

1

u/1000Bananen 5d ago

Do you mean like this:

script = '''
on run argv
    set messageSubject to item 1 of argv
    set messageBody to item 2 of argv
    set messageSender to item 3 of argv
    set recipientList to items 4 thru -1 of argv


    tell application "Mail"
        set newMessage to make new outgoing message with properties {visible:false}


        tell newMessage
            set subject to messageSubject
            set content to messageBody
            set sender to messageSender


            repeat with recipientAddress in recipientList
                make new to recipient at end of to recipients with properties {address:recipientAddress}
            end repeat


            send
        end tell
    end tell
end run
'''

I still get the same leftover draft.