Mail rule for outgoing messages?


(Alberto González Ramírez) #1

Does anybody know how to apply a “mail rule” for outgoing messages?
Is it posible in Mail.app?

Thanks in advance.


#2

Alberto,

You can run a mail rule and then move the message to where you want after the run is done. That’s pretty simple. Also it’s pretty easy to attach an AppleScript to a mail rule. I usually do all my rule stuff in an AppleScript.

I put some same samples of using an AppleScript which can basically do just about anything. If this kind of stuff is of help let me know. If I had more details I could probably suggest a way to do the specific task.

Bill

using terms from application "Mail"
	on ProcessSingleMailMessage(MailSubject, TheSender, SenderName, SenderAddress, MailboxName, SentDate, ReceivedDate, ListOfRecipients, TheMessage)
		if ((class of ListOfRecipients) as string) ≠ "list" then
			set TheResult to my HandleErrorSaveAndDisplay("\"ListOfRecipients,\" inside \"ProcessSingleMailMessage\" is not of class list")
			if (not (Successful of TheResult)) then return false
			return true
		end if
		
		try
			set NumOfRecipients to count of ListOfRecipients
			if NumOfRecipients < 1 then
				set TheResult to my HandleErrorSaveAndDisplay("\"ListOfRecipients\" has an invalid number (count = " & NumOfRecipients & ") of items.")
				if (not (Successful of TheResult)) then return false
				return true
			end if
		on error errMsg number errNum from offendingObject partial result resultList to expectedType
			my HandleErrorSaveAndDisplay("Error " & (errNum as string) & " occured while trying to get at item out of ListOfRecipients." & return & errMsg)
			return true
		end try
		
		if (DebuggingMode) then display dialog "0" buttons {"Cancel", "OK"} default button "OK" with title "Debugguing mode"
		
		set FoundMyEmail to false
		repeat with ARecipient in ListOfRecipients
			if (MyEmailAddresses contains ARecipient) then
				set FoundMyEmail to true
				exit repeat
			end if
		end repeat
		
		if (not FoundMyEmail) then
			my HandleErrorSaveAndDisplay("An error occured in ProcessSingleMailMessage.  None of the emails recipients was one of my emails.")
			set TheResult to MoveMessage(TheMessage, "Trash")
			if (not (Successful of TheResult)) then return false
			return true
		end if
		
		if (DebuggingMode) then display dialog "1" buttons {"Cancel", "OK"} default button "OK" with title "Debugguing mode"

The “if (DebuggingMode)” is something where I can turn a property on and off to display a tracking of what the script is doing.

Here’s a sample of how I create “my kind” of rule

			set AddressesToKeep to {¬
				{MailAddress:"pattkopp@cox.net", MailboxLocation:"People/Patt Kopp"}, ¬
				{MailAddress:"patt_kopp@me.com", MailboxLocation:"People/Patt Kopp"}, ¬
				{MailAddress:"9496807538@vtext.com", MailboxLocation:"People/Patt Kopp"}, ¬
				{MailAddress:"pkopp@occ.cccd.edu", MailboxLocation:"People/Patt Kopp"}, ¬
				{MailAddress:"richard.parker@gmail.com", MailboxLocation:"People/Richard Parker"}, ¬
				{MailAddress:"richard@electrophobia.com", MailboxLocation:"People/Richard Parker"}, ¬
				{MailAddress:"rlp@mac.com", MailboxLocation:"People/Richard Parker"}, {MailAddress:"pest193765@cox.net", MailboxLocation:"Trash"}, ¬
				{MailAddress:"mnwd@onlinebiller.com", MailboxLocation:"Creditors/Moulton Niguel Water District/Billing statemnent"}, ¬
				{MailAddress:"customerservice@socalgas.com", MailboxLocation:"Creditors/Southern California Gas Company/Billing statemnent"}, ¬
				{MailAddress:"do_not_reply@itunes.com", MailboxLocation:""}, ¬
				{MailAddress:"donotreply@mylifevantage.com", MailboxLocation:"Creditors/Protandim"}, ¬
				{MailAddress:"distributorsupport@mylifevantage.com", MailboxLocation:"Creditors/Protandim"}, ¬
				{MailAddress:"newsletter@kurzweilai.net", MailboxLocation:"Mailing lists/Kurzweil"}, ¬
				{MailAddress:"DiscoverMagazine@maillist.kalmbachmail.com", MailboxLocation:"Mailing lists/Discover Magazine"}}

Here is a sample of how I use “my kind” of rule

		try
				repeat with TheRecord in AddressesToKeep
					if (TheSender contains (MailAddress of TheRecord)) then
						set TheResult to MoveMessage(TheMessage, MailboxLocation of TheRecord)
						if (not (Successful of TheResult)) then return false
						return true
					end if
				end repeat
			on error errMsg number errNum from offendingObject partial result resultList to expectedType
				my HandleErrorSaveAndDisplay("Error " & (errNum as string) & " occured while processing \"AddressesToKeep\" emails." & return & errMsg)
				return false
			end try
			
			if (DebuggingMode) then display dialog "3" buttons {"Cancel", "OK"} default button "OK" with title "Debugguing mode"
			
			-- Move any emails with a specified domain 
			try
				repeat with TheRecord in DomainsToKeep
					if (TheSender contains (Domain of TheRecord)) then
						set TheResult to MoveMessage(TheMessage, MailboxLocation of TheRecord)
						if (not (Successful of TheResult)) then return false
						return true
					end if
				end repeat
			on error errMsg number errNum from offendingObject partial result resultList to expectedType
				my HandleErrorSaveAndDisplay("Error " & (errNum as string) & " occured while processing \"DomainsToKeep\" emails." & return & errMsg)
				return false
			end try

(Alberto González Ramírez) #3

@BillKopp thanks very, very much for your reply.

I think I didn’t explain correctly.
I was pointing to outgoing messages.
With Mail Rules, as you say, we can attach an AppleScript to get the work done with the incoming messages.
But, what if I wanted to apply an AppleScript to outgoing messages?

For example.

How could I create a script that ask me if I really want to send the email to a specific recipient after having pressed the send button?

Or, how could I archive every message I send “dynamically”? I mean, I press the send button and an AppleScript moves the sent message to a specific mailbox depending on the recipient.

Just two examples.


#4

I have been working with Apple Mail for a long, long time and I have never seen anything that successfully scripted out-going mail. I know of a huge number of people who have tried a lot and complained about it not working. Scripting incoming mail is no problem.

What I was talking about does use Mail rules. One of the mail rules is use AppleScripts. Mail rules without AppleScripts can do a lot of simple things but when the complexity gets high enough people switch to AppleScript assisted mail rules. Mail rules work on properties of emails, as well as delete, mark and flag emails. They also produce visible notification of things like bound doc, play sound, set color and send notifications. And lastly they can execute AppleScripts.

If you do want do want to script Mail rules for out-going mails you can try searching google on “apple mail rules out going email.” What I do instead is use the “Apple Rules” menu item in the “Message” menu to perform an action that uses AppleScript to do what I want Mail to do.

But if you want to use straight mail rules without AppleScript then what you need to check into is using rules on out-going email. This is a commonly known problem and that is the name they use when talking about it, “out going messages” or “out going mail.”

I hope this helps. I suspect that Mail does not apply rules to out going emails, but I could be wrong.

Bill


(Alberto González Ramírez) #5

Thanks Bill.

Very clear explanation.

Thanks again, @BillKopp.