Send an email in Apple Automator conditional on whether an email has been received

Discussion in 'Mac Programming' started by pinguthepenguin, Feb 23, 2015.

  1. pinguthepenguin macrumors newbie

    Joined:
    Jan 26, 2008
    #1
    I would like to send an automated email every week conditional on whether a specific email has been received that week. Is this possible with the Automator tool, and if so, how can it be achieved please?
     
  2. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
  3. pinguthepenguin thread starter macrumors newbie

    Joined:
    Jan 26, 2008
    #3
    Thanks for the reply, however rules are not appropriate here as the trigger is not receiving an email.
     
  4. MasConejos macrumors regular

    MasConejos

    Joined:
    Jun 25, 2007
    Location:
    Houston, TX
    #4
    You could still potentially use a mail rule in combination with automator, if you do it as a dead-man switch. The idea is that you have a timer that counts down. When the timer reaches zero, an event happens (your follow-up email). To prevent this event, you reset the timer to its starting value frequently (whenever you DO receive your specific email).

    I don't really use Automator or mail rules much so you'll need to adapt this idea to fit what the programs can do for you.
    1. Every time you DO receive the email, use automator/mail rule to write the time or copy the email to a folder.
    2. Automator checks the folder for most recent time or the date of files and if the date is larger than one week, send the follow-up email
    3. After sending the new email, optionally write a placeholder file or time to your target folder so that you don't get spammed with reminder emails every time the script runs.

    Automator will need to run its script periodically for this. You can also use the built-in chron system to schedule automator (or a perl/ruby/python/applescript) to do the task.
     
  5. pinguthepenguin thread starter macrumors newbie

    Joined:
    Jan 26, 2008
    #5
    I really appreciate the reply MasConejos. I should have been more specific with my original question, apologies, however it is not the trigger mechanism that is causing me difficulties.

    This is my scenario. At the end of every week I am sent email summaries from my team. Invariably they are late or forgotten. I would like to save myself time in chasing them by having an automator application I can run that will query who has sent me an update this week, and then automatically email those who have forgotton.

    I have a smart mail box in mail that collects the relevant emails from the past week. What I can't do is interrogate this folder to see who is missing.

    I hope that clarifies things, I was trying to keep my original question as generic as possible so it could benefit others, but in the process I did not provide sufficient detail, sorry.
     
  6. MasConejos macrumors regular

    MasConejos

    Joined:
    Jun 25, 2007
    Location:
    Houston, TX
    #6
    I am fairly certain this could be accomplished with applescript, but I'm not at a mac so I can't experiment right now. I'll try to remember to look into it this this evening, but no promises
     
  7. pinguthepenguin thread starter macrumors newbie

    Joined:
    Jan 26, 2008
  8. kryten2, Feb 27, 2015
    Last edited: Feb 27, 2015

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #8
    Unfortunately a smart mail box is not accessible by AppleScript or Automator.
    You can try this script example by selecting all messages in your smart mail box and run it from the ScriptEditor.


    Code:
    -- Fill the list with the names and addresses of your team!!!
    -- These are just examples.
    set teamNamesList to {"John Doe <john.doe@hotmail.com>", "Jane Doe <jane.doe@hotmail.com>"}
    
    tell application "Mail"
    	set selectedMessages to selection
    	if (count of selectedMessages) is equal to 0 then
    		display alert "No Messages Selected" message "Select the messages you want to process before running this script."
    	else
    		set senderList to {}
    		repeat with i from 1 to number of items in selectedMessages
    			set thisMessage to item i of selectedMessages
    			set senderItem to sender of thisMessage
    			if senderItem is not in senderList then
    				set end of senderList to senderItem
    			end if
    		end repeat
    	end if
    end tell
    
    repeat with j from 1 to number of items in teamNamesList
    	set this_item to item j of teamNamesList
    	if this_item is not in senderList then
    		sendReminderMessage(this_item)
    	end if
    end repeat
    
    on sendReminderMessage(fullySpecifiedEmailAddress)
    	tell application "Mail"
    		set theMsg to (make new outgoing message with properties {subject:"Reminder", content:"I haven't received your ...", visible:true})
    		tell theMsg
    			make new to recipient with properties {name:extract name from fullySpecifiedEmailAddress, address:extract address from fullySpecifiedEmailAddress}
    		end tell
    		--send theMsg -- Uncomment if you want to send the message
    	end tell
    end sendReminderMessage
    
     
  9. pinguthepenguin thread starter macrumors newbie

    Joined:
    Jan 26, 2008
  10. pinguthepenguin thread starter macrumors newbie

    Joined:
    Jan 26, 2008
    #10
    Thanks kryten2, I am almost there. Can I bypass the smart mail box restriction, and add that logic to the script?

    I would need to check messages in the past five days that have "summary" or "update" in the subject.
     
  11. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #11
    You can use a filter. Here's a simple example :


    Code:
    -- Fill the list with the names and addresses of your team!!!
    -- These are just examples.
    set teamNamesList to {"John Doe <john.doe@hotmail.com>", "Jane Doe <jane.doe@hotmail.com>"}
    
    set daysToCheck to 5
    set dateReference to (current date) - (daysToCheck * days)
    
    tell application "Mail"
    	-- Change mailbox and account to your mailbox and account
    	set senderList to get sender of every message of mailbox "INBOX" of account "FOO" whose (subject contains "update" or subject contains "summary") and date received ≥ dateReference
    end tell
    
    repeat with j from 1 to number of items in teamNamesList
    	set this_item to item j of teamNamesList
    	if this_item is not in senderList then
    		sendReminderMessage(this_item)
    	end if
    end repeat
    
    on sendReminderMessage(fullySpecifiedEmailAddress)
    	tell application "Mail"
    		set theMsg to (make new outgoing message with properties {subject:"Reminder", content:"I haven't received your ...", visible:true})
    		tell theMsg
    			make new to recipient with properties {name:extract name from fullySpecifiedEmailAddress, address:extract address from fullySpecifiedEmailAddress}
    		end tell
    		--send theMsg -- Uncomment if you want to send the message
    	end tell
    end sendReminderMessage
     
  12. pinguthepenguin thread starter macrumors newbie

    Joined:
    Jan 26, 2008
    #12
    It's all working perfectly! thanks very much indeed for all your help kryten2
     
  13. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #13
    Great. Glad to hear it worked. When you enable the Show Script menu in menu bar option from the ScriptEditor Preferences window, you can save it in your Mail Scripts Folder so it's available from the Script Menu in Mail.
     

    Attached Files:

  14. pinguthepenguin thread starter macrumors newbie

    Joined:
    Jan 26, 2008
    #14
    Neat, thanks! it's all up and running now, and saving me time, thanks.
     

Share This Page