Filtering email by display name

I want to filter incoming email based on the display name of the sender, not the email address. For instance, if mail comes from

Junk Mail Sender<abc@xyz.com>

I want to send it to a certain folder, regardless of what address appears (in place of abc@xyz.com) between the angle brackets. Making a rule in which the "From" field is "Junk Mail Sender" doesn't work, because (apparently) the "From" field must match the address, not the display name. Making a group in Contacts whose members include a company with name "Junk Mail Sender" and then setting up a rule using "Sender is member of group" also doesn't work. This raises the question of what qualifies a sender to be a member of a group. Apparently some sort of match with email addresses is required, not just the name. Or have I not been clever enough putting "Junk Mail Sender" into the contact card? If I could make this method work, it would be wonderfully efficient, since I could continually add to the group display names that proliferate in spam with ever-changing email addresses.


My reason for wanting this facility is to cut down on the number of junk mail messages I have to peruse in order to catch things that my server mistakenly classifies as junk. By diverting things I absolutely know to be junk to another folder, I won't have so many things in the Junk folder. In Mail->Preferences->Junk Mail Behaviors, I have selected "Move it to the Junk mailbox" under "When junk mail arrives", and I have unchecked "Filter junk mail before applying my rules". Have I misunderstood the combined effect of those two choices? That is, are the "Junk Mail Sender" messages I want to catch being sent to the Junk folder before my "Sender is member of group" rule can be applied?


Thanks for any help you can suggest!

MacBook Air 13", macOS 10.15

Posted on Apr 12, 2020 9:16 AM

Reply
Question marked as Top-ranking reply

Posted on Apr 14, 2020 2:03 PM

Here's a template for an applescript program that will find email messages whose sender has one of a list of display names that you designate, and move them to a folder of your designation. The names you want to filter out are in the list variable displayNames. I am not an expert applescript programmer, and you may be able to make several improvements. For instance, every time I want to add a new display name to the bad list, I have to edit the "set displayNames …" statement in the Script Editor. It would be nicer if the list could be maintained externally as a plain text file. Another improvement would be to find a way to pick out messages whose display name contains one of the names on the bad list, not merely equals it. In the script below, I have put asterisks to disguise my actual account names. Also, I have given only an abbreviated sample of the bad names in my actual list; your list will likely be different. To get Mail's internal name for the junk box in one of your accounts (in case that's where you want to look for the messages), open Mail Preferences, choose the Accounts tab, select the account, and look on the Mailbox Behaviors tab; the name is not necessarily "Junk". If you want to inspect all messages in the inboxes, not just the unread ones, omit the "where read status is false" clauses, but be aware that the script will probably takle a lot longer to run. Once you have the thing working, you can use Automator to assign a key combination that will trigger running it. I use Shift-Command-2, because my target spam folder is called "@WorstSpam" and the @ sign is Shift-2.


Below is the script. I'd be happy to hear your suggestions for improvements. This is a stand-alone script, not meant to be triggered by a Mail rule, but perhaps you will want to figure out how to do that.


global displayNames


set displayNames to {¬

"KetoSlim", ¬

"Oxybreathpro", ¬

"Skin Tag Removal", ¬

"Skin Tags Remover", ¬

"Vision Loss"}



tell application "Mail"

set xferCounter to 0 (* Used in pop-up message at end *)

set targetFolder to mailbox "@WorstSpam" (* Change this to your choice *)

(* Inspect all messages in Junk box *)

set sourceFolderName to "INBOX/spam"

set sourceFolder to mailbox sourceFolderName of account "*****"

set everyMessage to every message of sourceFolder

set listSize to count of everyMessage

set junkCounter to count of everyMessage

repeat with mssg in everyMessage

set theName to extract name from sender of mssg

if displayNames contains theName then

set mailbox of mssg to targetFolder

set xferCounter to xferCounter + 1

end if

end repeat

(* Inspect unread messages in INBOX of gmail account *)

set sourceFolderName to "INBOX"

set sourceFolder to mailbox sourceFolderName of account "*****@gmail.com"

set everyMessage to every message of sourceFolder where read status is false

set gmailInboxCounter to count of everyMessage

repeat with mssg in everyMessage

set theName to extract name from sender of mssg

if displayNames contains theName then

set mailbox of mssg to targetFolder

set xferCounter to xferCounter + 1

end if

end repeat

(*Inspect unread messages in IMBOX of another account *)

set sourceFolderName to "INBOX"

set sourceFolder to mailbox sourceFolderName of account "*****"

set everyMessage to every message of sourceFolder where read status is false

set otherInboxCounter to count of everyMessage

repeat with mssg in everyMessage

set theName to extract name from sender of mssg

if displayNames contains theName then

(* display dialog theName *)

set mailbox of mssg to targetFolder

set xferCounter to xferCounter + 1

end if

end repeat

display dialog "Done filtering the worst spam from " & junkCounter & ¬

" Junk, " & gmailInboxCounter & " gMail Inbox, " & " and " & ¬

otherInboxCounter & " ***** Inbox messages; transferred " & xferCounter ¬

& " messages."

end tell


Similar questions

4 replies
Question marked as Top-ranking reply

Apr 14, 2020 2:03 PM in response to brenden dv

Here's a template for an applescript program that will find email messages whose sender has one of a list of display names that you designate, and move them to a folder of your designation. The names you want to filter out are in the list variable displayNames. I am not an expert applescript programmer, and you may be able to make several improvements. For instance, every time I want to add a new display name to the bad list, I have to edit the "set displayNames …" statement in the Script Editor. It would be nicer if the list could be maintained externally as a plain text file. Another improvement would be to find a way to pick out messages whose display name contains one of the names on the bad list, not merely equals it. In the script below, I have put asterisks to disguise my actual account names. Also, I have given only an abbreviated sample of the bad names in my actual list; your list will likely be different. To get Mail's internal name for the junk box in one of your accounts (in case that's where you want to look for the messages), open Mail Preferences, choose the Accounts tab, select the account, and look on the Mailbox Behaviors tab; the name is not necessarily "Junk". If you want to inspect all messages in the inboxes, not just the unread ones, omit the "where read status is false" clauses, but be aware that the script will probably takle a lot longer to run. Once you have the thing working, you can use Automator to assign a key combination that will trigger running it. I use Shift-Command-2, because my target spam folder is called "@WorstSpam" and the @ sign is Shift-2.


Below is the script. I'd be happy to hear your suggestions for improvements. This is a stand-alone script, not meant to be triggered by a Mail rule, but perhaps you will want to figure out how to do that.


global displayNames


set displayNames to {¬

"KetoSlim", ¬

"Oxybreathpro", ¬

"Skin Tag Removal", ¬

"Skin Tags Remover", ¬

"Vision Loss"}



tell application "Mail"

set xferCounter to 0 (* Used in pop-up message at end *)

set targetFolder to mailbox "@WorstSpam" (* Change this to your choice *)

(* Inspect all messages in Junk box *)

set sourceFolderName to "INBOX/spam"

set sourceFolder to mailbox sourceFolderName of account "*****"

set everyMessage to every message of sourceFolder

set listSize to count of everyMessage

set junkCounter to count of everyMessage

repeat with mssg in everyMessage

set theName to extract name from sender of mssg

if displayNames contains theName then

set mailbox of mssg to targetFolder

set xferCounter to xferCounter + 1

end if

end repeat

(* Inspect unread messages in INBOX of gmail account *)

set sourceFolderName to "INBOX"

set sourceFolder to mailbox sourceFolderName of account "*****@gmail.com"

set everyMessage to every message of sourceFolder where read status is false

set gmailInboxCounter to count of everyMessage

repeat with mssg in everyMessage

set theName to extract name from sender of mssg

if displayNames contains theName then

set mailbox of mssg to targetFolder

set xferCounter to xferCounter + 1

end if

end repeat

(*Inspect unread messages in IMBOX of another account *)

set sourceFolderName to "INBOX"

set sourceFolder to mailbox sourceFolderName of account "*****"

set everyMessage to every message of sourceFolder where read status is false

set otherInboxCounter to count of everyMessage

repeat with mssg in everyMessage

set theName to extract name from sender of mssg

if displayNames contains theName then

(* display dialog theName *)

set mailbox of mssg to targetFolder

set xferCounter to xferCounter + 1

end if

end repeat

display dialog "Done filtering the worst spam from " & junkCounter & ¬

" Junk, " & gmailInboxCounter & " gMail Inbox, " & " and " & ¬

otherInboxCounter & " ***** Inbox messages; transferred " & xferCounter ¬

& " messages."

end tell


Apr 15, 2020 9:36 AM in response to ben_c_mpls

Below is a grown-up version of the script I posted before. It uses subroutines in place of the repetitive code I had before, and it permits specifying just the initial substrings of display names that you know signify junk. Thus, spammers who routinely modify their display names are more likely to continue to be caught by the script despite that tactic. Details that will allow you to adapt the script for your own use are specified in the comments. When I run this script now, it pops up statistics that indicate that there are no bad messages in the inboxes of my accounts, but lots of them in my junk mailbox. This tells me that the spam filter that populates the junk box is doing a good job, and that I can save effort checking the junk box for mail that was wrongly classified as spam by running this script. As I encounter increased numbers of spam messages with the same display name, I edit the script to add it to the list.


Here's the new version (I have replaced my account names with ***** and simplified the list displayNames):


(*

This script will look in the mailboxes specified in the

global variable sourceFolders, examining every message

or every unread message, as you specify. For each message

it will extract the display name of the "From:" header, if

there is one. For instance, if the message is from

"MUST-HAVE ITEM"

the display name is

MUST-HAVE ITEM

The script will compare this name to each item in the

global variable displayNames. If the display name begins with

any one of those items, ignoring case, the message will be moved

to the folder specified in the first line of code after

tell Application "Mail" below. For example, if one of the

items in displayNames is "Skin Tag", then messages

with display names such as "Skin Tag Removal", "Skin tags removed",

and "Skin tags gone forever" will be moved.


Note that it will probably take considerably longer to inspect

all messages in a mailbox, rather than just the unread ones.


At the end of the inspections, the script will pop up statistics,

which will look something like this:


Statistics:

transferred 19 messages from mailbox INBOX/spam of account *****

transferred 0 messages from mailbox INBOX of account *****

transferred 2 messages from mailbox INBOX of account *****

Total transferred: 21

*)


global displayNames, sourceFolders, targetFolder


set displayNames to {¬

"Body Therm", ¬

"KetoSlim", ¬

"Skin Tag"}


(*

Each item in the list sourceFolders is itself a list of three items:

1. The name of the mailbox

2. The name of the account (use "" if none)

3. The boolean true if you want to inspect every message

in the mailbox, false if you want to inspect only

unread messages

To get Mail's internal name for the junk box in one of your accounts

(in case that's where you want to look for the messages), open Mail

Preferences, choose the Accounts tab, select the account, and look on

the Mailbox Behaviors tab; the name is not necessarily "Junk".

*)

set sourceFolders to ¬

{"INBOX/spam", "*****", true}, ¬

{"INBOX", "*****", false}, ¬

{"INBOX", "*****", false}}


tell application "Mail"

set targetFolder to mailbox "@WorstSpam"

set transferTotal to 0

set reportString to "Statistics:"

repeat with sourceInfo in sourceFolders

set transferCount to my handleFolder(sourceInfo)

set reportString to reportString & "

" & "transferred " & transferCount ¬

& " messages from mailbox " & (item 1 of sourceInfo)

if (item 2 of sourceInfo) is not "" then

set reportString to reportString & " of account " & (item 2 of sourceInfo)

end if

set transferTotal to transferTotal + transferCount

end repeat

set reportString to reportString & "

Total transferred: " & transferTotal

display dialog reportString

end tell


on handleFolder(_sourceInfo)

tell application "Mail"

set folderName to item 1 of _sourceInfo

set accountName to item 2 of _sourceInfo

set readStatus to item 3 of _sourceInfo

set transferCount to 0

if accountName is "" then

set sourceFolder to mailbox folderName

else

set sourceFolder to mailbox folderName of account accountName

end if

if readStatus then

set everyMessage to every message of sourceFolder

else

set everyMessage to every message of sourceFolder where read status is false

end if

repeat with mssg in everyMessage

set transferCount to transferCount + (my handleMessage(mssg))

end repeat

return transferCount

end tell

end handleFolder


on handleMessage(_message)

tell application "Mail"

set theName to extract name from sender of _message

repeat with stem in displayNames

set strLength to length of stem

if length of theName ≥ strLength then

set trimmedName to characters 1 thru strLength of theName

if trimmedName as string = stem as string then

set mailbox of _message to targetFolder

return 1

end if

end if

end repeat

return 0

end tell

end handleMessage



[Email Edited by Moderator]

Apr 13, 2020 12:14 PM in response to brenden dv

Thanks, brenden dv for the two links. Unfortunately, neither of them says anything about filtering on display names, which—when they are present—are part of the "From:" header, but apparently not the part that the filters operate on. Thus the "Edit header list" option in Use rules to manage emails you receive in Mail on Mac doesn't help. I see in the same document, however, that it is possible to use AppleScript scripts as rule actions. I'll look into whether AppleScript can pick out the display name, in which case I'll try to roll my own; that's more of a project!

Apr 13, 2020 10:08 AM in response to ben_c_mpls

Hello ben_c_mpls,


Welcome to Apple Support Communities. It sounds like you're having trouble filtering your email. I'd be happy to share information that may help.


Take a look at the information in this article: Use rules to manage emails you receive in Mail on Mac. As the article mentions, rules will be applied in the order that they appear in the list.


This article may also help: If junk mail filters aren’t working in Mail on Mac


Take care.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Filtering email by display name

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.