SQL Server Alerts with Text Messaging from SQL Server Database Mail
I need to get alerts from my SQL Server databases 24/7, but I don't always carry my laptop to check email and when I do, I may not be near an internet connection. I always have my cell phone with me, though. Can I get my SQL Servers to send me a text message when there is an important alert without setting up my own SMS gateway or cellular modem? Check out this tip to learn more.
Most cellular providers who offer texting (SMS or Short Message Service) also provide a free email to SMS gateway that costs the sender nothing and the receiver pays for messages based on their text plan. In this tip I'll show how to configure an operator that will send an email to one of these gateways.
I learned some time ago that I could send an email to my cell phone via email using a free service provided by my cellular provider. It didn't seem all that useful at the time unless I had a friend who did not have texting and wanted to chat. When I started developing an on-call rotation for my DBA team it quickly became apparent that for off-hours alerts we needed a way to reach team members when they were not online. Even those of us who have smart phones found it easier to attach a custom notification to an incoming text message than to an email.
We setup two operators on each server, one for non-critical alerts and one for alerts that need immediate attention. The non-critical alerts only go to email and can sit in our inbox until someone checks. The critical alerts use an operator setup to send email to the SMS gateway of our cellular provider so they can be received by the on-call DBA even when they are offline.
I'm going to make the assumption that you have already setup Database Mail on your server, if not there are instructions in the Database Mail category on MSSQLTips.com that will walk you through the process.
This is a table listing the largest cellular providers here in the US and the domain name of the SMS gateway that will convert an email to a text message. It is a free service from most providers, but a few may charge a fee or require sign-up. Keep in mind that even though sending the message is free, the recipient is liable for charges based on their contract, if you have a run-away alert it can get very expensive! Check with your provider before you rack up a ton of text charges - you have been warned!
|US Cellular||[email protected]|
|Virgin Mobile||[email protected]|
If your provider is not in this list or you are outside the US, then search Wikipedia for "SMS Gateway" for a more extensive list or contact your provider directly for information.
I am a Verizon customer and my cell number is ... (123)555-0000. No, not really but for this tip we'll pretend it is. To ensure Database Mail can reach the gateway let's send a text email/text message. Open SQL Server Management Studio and connect to an instance with Database Mail configured. In Object Explorer expand the instance, then the Management tab. Right-click on Database Mail and select "Send a test E-mail". Given the phone number above and the fact that I use Verizon I'll enter [email protected] in the To: field and click Send Test E-Mail, use your cell number and the gateway domain name from the table above to find your own cell phones email address. If you've got the right address and Database Mail is set up correctly you should get a text message in just a minute or two.
You can also enter the appropriate email in the script below to send a quick test. Be sure to double check the number so you aren't spamming some unsuspecting stranger!
USE [msdb] GO EXEC msdb.dbo.sp_send_dbmail @recipients = "[email protected]", @subject = "Test SMS", @body = "This is only a test" GO
For this to be useful for alerts we will need to configure an operator. In the Object Explorer pane of SQL Server Management Studio expand the instance, then expand the SQL Server Agent listing, right-click the "Operators" option and select "New Operator...". For the name of the operator enter "New On-Call Operator" and in the Email name field I'll enter [email protected], you should enter your cell number and the appropriate gateway from the table above or from Wikipedia. Our on-call rotation is 24/7 so I'm not going to configure a duty schedule, but get creative as needed for your own organization.
I have a some alerts already set up, so I'll click over to the Notifications Tab, select Alerts and check each of the alerts this new operator will get texted for and press the "OK" button.
- That's really all there is to it. The only difference from a standard email alert is that you need to find the gateway for your cellular provider. If you want more information on Database Mail, Alerts, and monitoring check out these tip categories on MSSQLTips.com.
Last Updated: 2013-04-04
About the author
View all my tips