Saturday, January 15, 2011

Shifting Gears: From Twitter to Gmail

I posted some articles in the past on how to leverage Twitter for sending and receiving systems alerts using Direct Messaging (DM). That was using Basic Authentication, not the newer (current) OAuth model.  But regardless, the biggest annoyance with that approach was Twitter's downtime and backlog.  Some alerts wouldn't show up for almost a half hour.  Some never showed up.  Even the inverse approach of sending DM tweets to an account monitored by my server suffered the same hinderances.

The better approach?  GMail.  Yes.  GMail.  Using standard SMTP commands with CDO and pretty much ANY SCRIPT language you prefer, you can get and send e-mail, and it is infinitely more reliable and prompt.

Here's a sample chunk of VBscript code to try for yourself…

strSubject = "Test Message"
strMessage = "This is a test message"
sender = "YOUR NAME <your_address@GMAIL.COM>"
recipient = "" ' or put in a SMS address like ""

On Error Resume Next
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")

If err.Number <> 0 Then
wscript.Echo "error: unable to invoke CDO interface"
End If

Set Flds = iConf.Fields

schema = ""

Flds.Item(schema & "sendusing") = 2
Flds.Item(schema & "smtpserver") = ""
Flds.Item(schema & "smtpserverport") = 465
Flds.Item(schema & "smtpauthenticate") = 1
Flds.Item(schema & "sendusername") = "UserName"
Flds.Item(schema & "sendpassword") = "Password"
Flds.Item(schema & "smtpusessl") = 1

wscript.echo "info: sending smtp message..."

With iMsg
.To = recipient
.From = sender
.Subject = strSubject
.HTMLBody = strMessage
.Sender = " "
.Organization = " "
.ReplyTo = " "
Set .Configuration = iConf
SendEmailGmail = .Send
End With

Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing

wscript.echo "info: message was sent"

This is easy enough for outbound work: sending alerts.  For doing the opposite, you'll need to set up a dedicated Gmail account for your server to monitor the Inbox and parse what it finds.  Same basic concept as the Twitter DM approach, but using SMTP (and/or SMS) instead.

In case you don't feel like digging back and reading old blog posts, some of the things you can do on the inbound side are rather interesting (and empowering):

  • Enable or Disable AD (or local) user accounts

  • Unlock user accounts

  • Add or Remove user accounts within AD security Groups

  • Move accounts around

  • Start and Stop services

  • Initiate secondary automation processes (manual kick-off)

And so on.  Since all this does is set your server up to read specially-formatted Inbox messages and take action on them, you can make it do whatever you want.  The world (or at least your data center) is your oyster.

Post a Comment