user123456
user123456

Reputation: 63

send all drafts in apple's Mail

I'm new to scripting and am trying to write a simple script for mail. which I am going to use to send all emails in my draft folder at an ical event. however I am running into an error im not sure how to debug. here is my script

tell application "Mail"
set draftMessages to every message in drafts mailbox
repeat with theMessage in draftMessages
    set theSender to (sender of theMessage)
    set theSubject to (subject of theMessage)
    set theContent to (content of theMessage)
    set theRecipients to (to recipients of theMessage)
    set theAddress to (address of theRecipients)

    set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, sender:theSender, address:theAddress, visible:true}

    end repeat
end tell

and here is the events log with sensitive info replaced as blah.

tell application "Mail"
get every message of drafts mailbox
    --> {message id 23241 of mailbox "Drafts" of account "BLAH", message id 23236 of mailbox "Drafts" of account "BLAH"}
get sender of message id 23241 of mailbox "Drafts" of account "BLAH"
    --> "Blah Blah <[email protected]>"
get subject of message id 23241 of mailbox "Drafts" of account "BLAH"
    --> "test 2"
get content of message id 23241 of mailbox "Drafts" of account "BLAH"
    --> "test 2"
get every to recipient of message id 23241 of mailbox "Drafts" of account "BLAH"
    --> {to recipient 1 of message id 23241 of mailbox "Drafts" of account "BLAH"}
Result:
error "Can’t get address of {to recipient 1 of message id 23241 of mailbox \"Drafts\" of
account \"BLAH\" of application \"Mail\"}." number -1728 from «class radd» of {«class 
trcp» 1 of «class mssg» id 23241 of «class mbxp» "Drafts" of «class mact» "BLAH"}

Upvotes: 2

Views: 1157

Answers (2)

Mark
Mark

Reputation: 2435

Are you sure you want to make a new message? The message already exists in your drafts folder and there is no reason to make a new one. This should be sufficient to do what you want:

tell application "Mail"
set draftMessages to every message in drafts mailbox
    repeat with theMessage in draftMessages
        send theMessage
    end repeat
end tell

If this isn't what you want, then you need to rewrite your question.

Upvotes: 2

adayzdone
adayzdone

Reputation: 11238

Try moving the new message outside of the tell block...

tell application "Mail"
    activate
    set draftMessages to every message in drafts mailbox
    repeat with theMessage in draftMessages
        tell theMessage
            set theSender to (sender of theMessage)
            set theSubject to (subject of theMessage)
            set theContent to (content of theMessage)
        end tell
        set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, sender:theSender, visible:true}
    end repeat
end tell

Upvotes: 0

Related Questions