Skip to content

Instantly share code, notes, and snippets.

@fsans
Last active February 28, 2024 10:45
Show Gist options
  • Save fsans/c3be8c78d36d9b9e4474ea5c5cf3c201 to your computer and use it in GitHub Desktop.
Save fsans/c3be8c78d36d9b9e4474ea5c5cf3c201 to your computer and use it in GitHub Desktop.
AppleScript for sending a webhook with JSon payload from a mail rule action

WebHook mail action AppleScript

This script takes the body and relevant data from any email and sends a webhook using CURL to a given http destination, with the payload as JSon.

Use it as the action of an email rule, saving it in the scripts com.apple.mail folder:

/Volumes/HD/Library/Application Scripts/com.apple.mail

Modify according to your webhook's destination url on the curlCommand parameter definition.

-- notify mail content to webhook POST
-- by air.fsans@gmail.com the 23.FEB.2024
-- 
use scripting additions
use framework "Foundation"

-- classes, constants, and enums used
property NSJSONWritingPrettyPrinted : a reference to 1
property NSJSONSerialization : a reference to current application's NSJSONSerialization

using terms from application "Mail"
	
	on perform mail action with messages theMessages for rule theRule
		
		tell application "Mail"
			
			set msgs to selection
			
			if length of msgs is not 0 then
				
				repeat with msg in msgs
					
					set msgUID to (id of msg) as integer
					set msgId to (message id of msg) as rich text
					set msgContent to (content of msg) as rich text
					set msgSubject to (subject of msg) as rich text
					set msgSender to (sender of msg) as rich text
					set msgDateSent to (date sent of msg) as rich text
					set msgDateReceived to (date received) of msg as rich text
					
					-- added starting and ending flags to allow easy regex parsing
					set msgContent to "##START##" & msgContent & "##END##"
					
					set msgData to {|subject|:msgSubject, |sender|:msgSender, |content|:msgContent, sent:msgDateSent, received:msgDateReceived, uid:msgUID, |id|:msgId}
					
					set theJSONData to (NSJSONSerialization's dataWithJSONObject:msgData options:NSJSONWritingPrettyPrinted |error|:(missing value))
					
					try
						set payload to (current application's NSString's alloc()'s initWithData:theJSONData encoding:(current application's NSUTF8StringEncoding)) as rich text
					on error e
						display dialog "error converting jsondata to string: " & e
					end try

					set curlCommand to "curl " & ¬
						quoted form of "https://your_host/index.php" & ¬
						" --header 'Content-Type: application/json'" & ¬
						" --data-raw " & quoted form of payload
					
					try
						do shell script curlCommand
					on error e
						display dialog "error sending webhook: " & e
					end try
					
				end repeat
				
			end if -- msgs > 0
		end tell
	end perform mail action with messages
	
end using terms from

-- end

and you'll receive some like this....

{
  "sender" : "ServiceBot <noreply@my.service.com>",
  "content" : "##START##...your raw email body content (html or text)...##END##",
  "uid" : 74236,
  "id" : "439718166.223.1706026615753@f015294az-m-p03",
  "sent" : "Tuesday, 23 January 2024 at 17:16:55",
  "received" : "Tuesday, 23 January 2024 at 17:16:56",
  "subject" : "Hi There, this is your data"
}

Thanks to the great NSJSONSerialization lib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment