0

Need to convert an email address from Contacts into a recipient in Mail.

tell application "Contacts"
    set sendTo to every person's email 1
end tell

tell application "Mail"
    set mesg to make new outgoing message
    set recipient of mesg to sendTo -- Need to convert here.
    set subject of mesg to "A title"
    set content of mesg to "A body"
    send mesg
end tell
alexbuzzbee
  • 165
  • 10

1 Answers1

1

You've got several problems in your code...

  1. When you get "every person's email 1" you get a list of references, not a list of email addresses. To get the actual email address from a reference you get its "value".

  2. sendTo will be a list, so you have to loop over the list and add each address one at a time in mail.

  3. there's different kinds of recipients. You need to use a "to recipient".

Try this:

tell application "Contacts"
    set sendToList to value of every person's email 1
end tell

set emailSender to "me@me.com"
set theSubject to "The subject of the mail"
set theContent to "message body"
tell application "Mail"
    set mesg to make new outgoing message with properties {sender:emailSender, subject:theSubject, content:theContent, visible:true}
    tell mesg
        repeat with i from 1 to count of sendToList
            set thisEmail to item i of sendToList
            if thisEmail is not missing value then
                make new to recipient at end of to recipients with properties {address:thisEmail}
            end if
        end repeat
        --send
    end tell
end tell
regulus6633
  • 18,848
  • 5
  • 41
  • 49