Expand Sending Email with Applescript

applescriptcsvgmailmail.app

I read with interest the article regarding sending an email based upon parsing through a csv file to pick up the name and email address. This looks like most of what I need to accomplish my objective with the exception that I would like for the script to run once daily and send the email one day before a date stored in the csv file. This is to remind someone that they need to perform a a certain task the next day. Emails will be sent using Apple Mail as the MTA through my gmail account.

The csv will contain the following:

firstname, lastname, action date

The script would need to pick up the first name and date from the csv file. I have coded the email text into the script as shown in the previous example.

Pseudocode:

  1. Script runs just after midnight every day parsing the csv to determine who should receive the email on what date.
  2. For the selected names, create and send an email the day prior to the required action.
  3. As a bonus, the script could resend the reminder on the due date as well as any new reminders for the following day.

I suppose I could manually subtract one day in my csv to make the coding easier.

I am just getting started with Applescript, and do not know how to modify the existing script to accomplish this objective.

Best Answer

Try something like this:

set csv to "name@example.com,7/14
name2@example.com,7/15"
--set csv to read "/Users/username/Documents/file.csv" as «class utf8»

set text item delimiters to ","
set y to year of (current date)
repeat with l in paragraphs of csv
    set d to (date (text item 2 of l & "/" & y))
    if date string of (current date) is date string of (d - 1 * days) then
        tell application "Mail"
            tell (make new outgoing message)
                set subject to "subject"
                set content to "content"
                make new to recipient at end of to recipients with properties {address:text item 1 of l}
                send
            end tell
        end tell
    end if
end repeat

You can run the script every day after midnight (if the computer is awake) by running EDITOR=nano crontab -e and adding a line like 1 0 * * * osascript ~/Scripts/some\ script.scpt.

The recognized date formats depend on the settings selected in System Preferences. I don't know how to get an email address for a first name and last name.