mail rules

I have mail rules setup to add what i deem as junk to be read and deleted amongst other rules I hit apply in addition to syncing mailbox but my rules are not working as designed or setup.

iMac 21.5", macOS 10.13

Posted on Feb 13, 2020 5:38 AM

Reply
Question marked as Top-ranking reply

Posted on Feb 13, 2020 12:28 PM

[Continuation due to 5000 character limit]


From your Dock, select Launchpad : Other : Script Editor.


Copy/paste all of the following code into the Script Editor:


-- return-path.applescript
-- Select a given message item in Apple Mail, and then run this script. It will produce a dialog
-- showing the sender's From and Return-Path addresses. Copy the entire Return-Path
-- string into the right-window of an Apple Mail rule.
-- This script *does not* alter, or remove individual email messages.
-- VikingOSX, 2019-10-23, Apple Support Communities, No warranties of any kind

use framework "Foundation"
use AppleScript version "2.4" -- Yosemite or later
use scripting additions

property NSString : a reference to current application's NSString
property NSRegularExpression : a reference to current application's NSRegularExpression
property NSRegularExpressionCaseInsensitive : a reference to current application's NSRegularExpressionCaseInsensitive

tell application "Mail"
	if not it is running then activate
	
	if not (get selection) is {} then
		set theMsg to item 1 of (get selection)
	else
		return
	end if
	
	tell theMsg
		set theSender to its sender
		set theHDR to its all headers
	end tell
end tell
set rtnpath to my return_address(theHDR) as text
set fmsg to "Sender Address:" & return & theSender & return & return & ¬
	"Return-Path:" & return & rtnpath

display dialog fmsg with title "Current Mail Item Addresses"
return

on return_address(atxt)
	set hstr to NSString's alloc()'s initWithString:atxt
	set pattern to "(?<=Return-Path:|Return-path:)\\s+(<.*?>).*"
	set regex to NSRegularExpression's regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive |error|:0
	set hrange to current application's NSMakeRange(0, hstr's |length|())
	set matches to (regex's firstMatchInString:hstr options:0 range:hrange)
	
	if matches = "" then return "Not Found"
	
	set matchrange to matches's rangeAtIndex:1
	return (hstr's substringWithRange:matchrange) as text
end return_address


In the Script Editor, click the compile (hammer) icon, and the purple text will change colors indicating that it has no compile issues. Open Apple Mail, and select a single message item from a known SPAM source, and then click the Run button in the Script Editor.


You can save this script to your Desktop as either a script, script bundle, or Application through the Script Editor's Save panel. Then you double-click the Desktop item when you have a Mail item selected.

6 replies
Question marked as Top-ranking reply

Feb 13, 2020 12:28 PM in response to VikingOSX

[Continuation due to 5000 character limit]


From your Dock, select Launchpad : Other : Script Editor.


Copy/paste all of the following code into the Script Editor:


-- return-path.applescript
-- Select a given message item in Apple Mail, and then run this script. It will produce a dialog
-- showing the sender's From and Return-Path addresses. Copy the entire Return-Path
-- string into the right-window of an Apple Mail rule.
-- This script *does not* alter, or remove individual email messages.
-- VikingOSX, 2019-10-23, Apple Support Communities, No warranties of any kind

use framework "Foundation"
use AppleScript version "2.4" -- Yosemite or later
use scripting additions

property NSString : a reference to current application's NSString
property NSRegularExpression : a reference to current application's NSRegularExpression
property NSRegularExpressionCaseInsensitive : a reference to current application's NSRegularExpressionCaseInsensitive

tell application "Mail"
	if not it is running then activate
	
	if not (get selection) is {} then
		set theMsg to item 1 of (get selection)
	else
		return
	end if
	
	tell theMsg
		set theSender to its sender
		set theHDR to its all headers
	end tell
end tell
set rtnpath to my return_address(theHDR) as text
set fmsg to "Sender Address:" & return & theSender & return & return & ¬
	"Return-Path:" & return & rtnpath

display dialog fmsg with title "Current Mail Item Addresses"
return

on return_address(atxt)
	set hstr to NSString's alloc()'s initWithString:atxt
	set pattern to "(?<=Return-Path:|Return-path:)\\s+(<.*?>).*"
	set regex to NSRegularExpression's regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive |error|:0
	set hrange to current application's NSMakeRange(0, hstr's |length|())
	set matches to (regex's firstMatchInString:hstr options:0 range:hrange)
	
	if matches = "" then return "Not Found"
	
	set matchrange to matches's rangeAtIndex:1
	return (hstr's substringWithRange:matchrange) as text
end return_address


In the Script Editor, click the compile (hammer) icon, and the purple text will change colors indicating that it has no compile issues. Open Apple Mail, and select a single message item from a known SPAM source, and then click the Run button in the Script Editor.


You can save this script to your Desktop as either a script, script bundle, or Application through the Script Editor's Save panel. Then you double-click the Desktop item when you have a Mail item selected.

Feb 13, 2020 12:27 PM in response to roxanne133

I made a custom mail to handle SPAM from one particular address. One sets their Mail rule to acton on any Return-path that matches that server address. I get about 30 spams a day from several different named sources that share the same server, and one mail rule automatically redirects this junk into my Trash On My Mac folder. The rule that does this is:



Now, Return-Path does not exist by default in the list of selectable From list items, so it has to be added. At the very bottom of the long menu you get when you click that first selector, is a Edit header list… entry.



You select that, and a panel opens that shows your existing From items, and you press the [+] to add Return-Path to the Header items, and it is entered just like this:


Click Ok, then you can select that Header item as shown in my first screenshot.


I have written a short AppleScript that displays a dialog of the selected mail item's sender's email address, and their Return-Path server address which you want to copy and paste into the third field as I have done above. Save the rule. If there are any emails that have this Return-Path server in their header, they should be immediately processed as soon as you save the rule.


An example of the dialog produced by the AppleScript using bogus addresses so the Apple hosts won't delete it due to privacy concerns:



You want to copy the entire (including <> characters) of the Return-Path address shown for your rule match.


See my next post for the AppleScript code to produce the preceding dialog.



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.

mail rules

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