Help with a simple email script for OSX

applescriptemail

I would be so happy if someone could assist me in creating an Applescript to send some emails. I have seen a few examples but I am not confident that I can adapt them to my own needs.

I need to send the same email (subject & body), one at a time, to a list of addresses on a .csv file. I am using Apple Mail 5.3 and OSX 10.7.5

Many thanks in advance to anyone willing to help out.

Here's what I have so far.

    tell application "Mail"

    tell (make new outgoing message)
        set theAddress to "xxxx"
        make new to recipient at beginning of to recipients with properties {address:theAddress}
        set subject to "xxxx"
        set content to "xxxx"
        send
    end tell
end tell

I need to set theAddress to cell A1 on my excel sheet. How do I point it there? Once the email has sent I need to point it to cell A2… and so on.

Best Answer

The simplest and quickest way to do this in other languages would just be to drop each of your columns from excel into a list (array) and then have the whole thing in a loop and it will create the email fill the information and then send the email then loop back to the beginning and start again with values from each of the arrays at [1] and so on from there through your list. Not the most professional or efficient way but for your purposes that would work. If you can implement that then go ahead. If not I'll script it when I get back to my desktop.

----edit----

I edited script first written on this post: https://stackoverflow.com/questions/15386794/applescript-for-sending-emails-from-a-list-in-excel

to achieve this script. I code in java mostly so this might not be perfect so maybe somebody can come along and tweak it. OP should go through and enter his own values for the things written in all caps. Also the excel sheet needs to be in the form that the OP from the links excel sheet is in

set {firstName, eAddress} to getData()

repeat with i from 1 to count firstName
tell application "Mail"
    activate
    set mymail to make new outgoing message at the beginning of outgoing messages with properties {subject:"INSERT SUBJECT HERE"}
    tell mymail
        make new to recipient at beginning of to recipients with properties {address:item i of eAddress}
--The next line will start each email with Hi firstName and then carriage return to the text you fill in below

        set content to "Hi " & item i of firstName & "

INSERT BODY OF EMAIL HERE"
    end tell
    --show message window (otherwise it's hidden)
    set visible of mymail to true
    --bring Mail to front
    activate
    send mymail
end tell
end repeat


on getData()
set colA to {}
set colB to {}
tell application "Microsoft Excel"

    activate
    tell active sheet
        set lastRow to first row index of (get end (last cell of column 1) direction toward the top)

        repeat with i from 3 to lastRow
            set end of colA to (value of range ("A" & i))
            set end of colB to (value of range ("B" & i))
        end repeat
    end tell
end tell

return {colA, colB}
end getData    
Related Question