User Tools

Site Tools


tutorials:email:index

Sending Email Alerts from XTension

UPDATE: Sending email is now a built in function of the XTension dictionary. It’s no longer necessary to script Mail.app to be able to send an email. With the send email verb it’s now possible to send both simple text messages as well as sending fully HTML formatted messages with inline attachments of video stream snapshots or XTdb graph and gauge displays if you wish.

Sending an email is probably one of the most reliable ways to get a notification of alarm status or violation, system status, power status, just daily temperature updates, or whatever. It's also very easy to setup. Email standards and the cable and phone companies that run our servers have gotten on the ball lately about ssh connections and password protection to try (some might say in vain) to reduce the spam coming from their networks, so keeping something internal to XTension up to date and continually testing with all the different services just isn't a good use of time and effort. Luckily OSX has a terrific mail app in mail.app that is already on every machine, can easily be setup with your outgoing mail service and can easily be scripted from XTension to send emails in response to whatever actions or conditions you set.

First we're going to create a global script that contains all the handlers necessary to send emails. Then once that is setup we're going to create groups or just individual scripts to send them.

Create a global script to hold your script

My global script is called just “email” but you can name yours whatever makes sense to you. Remember that you're going to be using the name in every call to send an email so dont make it too long winded. Into the script paste this script handler:

on DoEmail(TheSubject, TheBody, TheName, theAddress)
 
	tell application "Mail" -- this sends the event to the recipients
		set newMessage to make new outgoing message with properties {subject:TheSubject, content:TheBody & return & return}
		tell newMessage
			set visible to true
			-- set sender to theSender
			set sender to TheName
			make new to recipient at end of to recipients with properties {name:TheName, address:theAddress}
			send newMessage
		end tell
	end tell
end DoEmail

After saving that script you can send an email from the ON script of any motion sensor or other unit by doing something like:

tell xGlobalScript "email" to DoEmail( "ALARM: motion", "There was motion in the kitchen", "your name", "your@email.com")

The problem with this is that now your email address and specific info is all over the place. What if you want to add a second email address to also send to your iPhone or just want to change the email address or subject? You have to go back and edit dozens or more scripts. Much better to create another handler in the email script for each specific class of email you want to send. So into the same email script above we're going to add some more code.

on SendAlert(TheSubject, TheMessage)
	DoEmail(TheSubject, TheMessage, "XTension", "my@email")
	DoEmail(TheSubject, TheMessage, "XTension", "my@iphone")
end SendAlert

That code will send 2 emails to my regular email and to my iPhone account. so from the ON script of a perimeter door while the alarm is on you might call that something like:

tell xGlobalScript "email" to SendAlert( "ALARM: Kitchen Door", "Kitchen door was opened at " & (current date as text))

and if you scatter that around a dozen door and window sensors you can change the email addresses by editing only 1 script. But you've still got to manually put that into each units script. Much better to create a group and run the same script for all members of the group. Then you can drag and drop units into and out of the group to add ones without writing any more scripts.

Create a group called something like “Units for perimeter alarms” and drag into it all the units you want to send a specific message. The group ON script can have a special handler in it called “GroupMemberChanged( TheUnit, TheValue)” that will get called whenever any units in the group change their value so in this group you can send an email for everything… (unfinished)

sending email without Mail.app

It is possible to send email from the command line as well if you do not wish to script to Mail.app for whatever reason. Jeff Wooding has written up an excellent summary here.

/home/e805485/machomeautomation.com/data/pages/tutorials/email/index.txt · Last modified: 2023/02/13 14:52 by 127.0.0.1