Last active
October 27, 2024 22:08
-
-
Save BrettBukowski/5347463 to your computer and use it in GitHub Desktop.
Filter emails in Mail.app with regular expressions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Mail.app can't run a "Contains" filter with a regex, | |
-- so you can't filter on HTML content. Until now. | |
using terms from application "Mail" | |
on perform mail action with messages theMessages for rule theRule | |
try | |
repeat with theMessage in theMessages | |
-- Getting the content as string converts all HTML tags to '?' and just leaves text content | |
set theBody to quoted form of (theMessage's content as string) | |
-- It's awkwardly hard to get sed to work w/ mult. lines, so collapse newlines | |
set theCommandString to "echo " & theBody & " | tr '\\n' ' ' | sed \"s/brett Author/*MATCHED*(&)/\"" as string | |
set sedResult to do shell script theCommandString | |
if sedResult contains "*MATCHED*" then | |
tell application "Mail" | |
move the theMessage to mailbox "My special mailbox" | |
end tell | |
end if | |
end repeat | |
on error errorText number errorNumber | |
tell application "Mail" to display alert ("Error: " & errorNumber) message errorText | |
return | |
end try | |
end perform mail action with messages | |
end using terms from |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I managed to update and re-write Brett Bukowski's original AppleScript above to get it working on Mac OS High Sierra. Brett's original script no longer works on newer Mac OS, because I believe Mail no longer assigns message IDs to incoming messages that have not yet been saved in a mailbox, and without an ID, you cannot reference and access an email message from within AppleScript.
My updated AppleScript below gets around this issue by allowing incoming email messages to be first saved in the INBOX, when the messages will be assigned an ID. Once saved in the INBOX, my script then checks too see if these messages are spam. To check for spam, my script searches the raw source of the email (which includes the email HTML code) for strings that are specified by the user (you) in the AppleScript (user specified by setting searchStringOne, searchStringTwo, etc within the script). This string search can be done by a standard string match search of the email's raw source, or by a regex match search on the raw source (uncomment the regex code in my AppleScript below if you want to use a regex).
To get my AppleScript to work in Apple Mail, as with Brett's script, save my AppleScript code given below in ~/Library/Application Scripts/com.apple.mail/ with a filename of say "Regex Spam Filter.scpt". Then in Apple Mail > Preferences > Rules, add a new rule which is applied for "Every Message", with this rule running the AppleScript "Regex Spam Filter.scpt" (instead of using "Every Message", you can also set the rule to run just on the email accounts that you want to check for spam using my AppleScript). It does not matter where this rule is placed in Mail's list of rules, because when a new email message comes in, the AppleScript Rule will always run after all the other rules are executed, even if it is placed as the first rule.
Then set up a new mailbox in Apple Mail called "Regex Trash". This can be done in the menu: Mailbox > New Mailbox... This mailbox is where the spam email messages found by this AppleScript will be moved to.
In my AppleScript below you will need to change: [email protected], [email protected] to your own email accounts that you want to check for spam, and you will need to change: searchStringOne, searchStringTwo, etc to text strings which will identify the spam emails.
Very often in spam emails, you may find some distinctive HTML code which you can use to identify the spam. For example, sometimes the URLs spammers use have a distinctive feature or string of text that you can use to identify the email as spam. Because this AppleScript checks the email raw source, you can target any HTML code in the email. This is something you cannot do with regular Apple Mail rules, because these only check the email message text content, and do not allow you to search the message raw source.
Here is my AppleScript code: