Deleting Emails from Mail on a Schedule

I have an automation where, when I receive a receipt for my online grocery order, Mail filters the email into a particular folder, and with a combination of Automator and Hazel the pdf receipt is removed from the email and added to a folder where I keep electronic receipts for processing later.

The automation is incomplete because I cannot work out how to then delete the email that contained the receipt pdf without manually doing it. I run this automation on a schedule each Friday around 5pm.

Can anybody help?

Thanks, Brian

Iā€™ve been looking for a way to do something similar: I get a lot of emails to one account from merchants, most of which a rent receipts but marketing offers of some kind.

I usually unsubscribe, but some I want to keep around, just in case. But they all have limited lives (3-5 days usually). So they go into a smart folder names for the appropriate lengthā€¦ and there they sit until I clear out the folders, because I can figure out a way to automatically delete an email X days after receipt.

It seems like such a basic need, but I donā€™t think even Sanebox does that (though I could be wrong ā€“ I prefer not to use services that require me to send email to someone elseā€™s server).

Hi Brian

You could use an AppleScript to move the messages in a specific mailbox to the deleted messages mailbox.

The script can be modified for tf2 so that only messages older than n days are selected using ā€œdate received < tā€, where t is current date - (days * n).

1 Like

Thank you very much for doing this for me. :slight_smile:

I will give it a go :grinning:

Sorry, Iā€™ve looked online but canā€™t find the Apple Script you mentioned. Do you have a link?

Thanks, Brian

Hi Brian

You can delete multiple messages (including selecting by date received) like this:

tell application "Mail"
	set targetMailbox to mailbox "Receipts"
	set mailBin to mailbox "Deleted Messages"
	set selectMessages to messages of targetMailbox whose date received < ((current date) - (days * 365))
	repeat with m in selectMessages
		move m to mailBin
	end repeat
end tell

As your automation will have already selected a single message, youā€™d just a single line of AppleScript to move it to your Deleted Messages mailbox.

Hi,

This discussion prompted me to create a workflow to process images coming in from my security cameras.

I expanded orpyā€™s example to move the Mail message to a Mailbox, copy the Attachment to a Finder Folder and cull Messages older than 7 days.

It is easily adaptable for other purposes, I have for example made a version to capture all PDFs received to a central folder.

Code can be found here -

Cheers

Iain

1 Like

Thank you very much for doing this for me. :slight_smile:

I have set a test so that the message I am testing with should move in 2 days to the Deleted file.

May I let you know how I get on?

Thanks, Brian

Hi Iain

Thank you very much for doing this for me. :slight_smile:

Sadly, I am not a programmer and although I can edit very simple code, I wasnā€™t sure what to do with the code you created!

Thanks so much though for responding to me. I am trying to create more ā€˜end to endā€™ automations and this posting was about that. I had managed to create an automation that got me 90% there but couldnā€™t do the final part.

All good wishes,

Brian

Hi Brian

Yes, let me know how you get on.

I couldnā€™t wait two days to see if a script works :slight_smile:

If I were you, Iā€™d create a test mailbox and drag a test message in to it so you can test your script.

If you run into problems you can always select the test message and run a single line, like:

tell application "Mail" to get selection

--> {message id 252052 of mailbox "Suppliers/AcmeCo" of application "Mail"}

So you can make sure that the script is targeting the correct messages/mailbox/account etc.

Adding more test messages with a range of dates to the test mailbox will then allow you to test if selectMessages date selection is working properly.

Just comment out the repeat part of the script while youā€™re testing so that the messages stay where they are.

Once youā€™re satisfied that the list of messages returned by the script is exactly as you want you can then let the repeat run with confidence that it isnā€™t going to do anything unexpected.

Good luck.

Hi,

I updated the code to give a description and purpose of the configurations you need to set. The rest of the code you donā€™t need to touch.

To test the delete function just grab some Junk mail older then the number of days you set in the configuration and place them into the folder you are moving the mail to, then run the script.

I am running the Ventura Beta and I am finding the mail rules are not always being triggered. An option I will try is to create a repeating Calendar to trigger the Applescript.

If you are still stuck drop me a line and maybe we can jump on a Zoom and I can help you out.

Iain

Hi Iain - yes, I am stuck!

If we could have a chat, I would be most welcome.

I am in the UK.

Thanks so much, Brian

Thank you very much for this script. I look forward to trying it out. One thing: Once I have detached an attachment from an eMail, I have no further need for that message. You have your purge set to 7 days, I am unsure how to set the purge to, say, immediately following detachment or less than 1 minute old. What syntax would I use to effect that change to line 5 of your script?

Hi,

Change set deleteAge to 7 to set deleteAge to 0, that should work.

Iain

1 Like