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!
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 firstname.lastname@example.org 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!
@recipients = "email@example.com",
@subject = "Test SMS",
@body = "This is only a test"
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 firstname.lastname@example.org, 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.
It's only expensive if you don't have unlimited SMS on your phone, which most people with a data plan already have. But if you have the ability to easily alert based on email address then that will certainly work.
What I've found is that the text message content itself is only marginally useful (becasue it's truncated to 140 char) and I go to email to get the full story, receiving the text is a way for me to easily know something is wrong.
What an awesome tip!! We were wondering how to send texts from SQL Server. We are going to use this to send job completion status for jobs that run over the weekend. no more having to check email. thanks!!
Monday, April 22, 2013 - 9:07:39 AM - Seth Delconte
I set this up for my team after reading your tip. I was already monitoring database disk drive space, so it was fairly easy to set up a job that sends us a text when a drive drops to less than 1% free space. Thanks again, John.
Wednesday, May 01, 2013 - 6:59:59 PM - Barbara Novak
Are there any configurations for Exchange server perspective to consider? Using my Exchange account as the Outgoing E-mail address and my User Name for Basic authentication in the SMTP Authentication section for database mail configuration, the record in sysmail_allitems shows "sent" in the sent_status column. I don't get the text message on my phone. Do get the message when sent to my email account. Can send an email from Outlook and recieve the text message.
When I use the database mail profile that sends messages daily to members of the Exchange GAL, the sent_status = failed and the event log message is: "The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2013-05-01T17:49:43). Exception Message: Cannot send mails to mail server. (Mailbox unavailable. The server response was: 5.7.1 Unable to relay)."
The Server name in the database mail configuration is "relay.companyname.com". Directed to use that by my Exchange admin.
I can't speak to your individual eMail setup as every site will be different. Once you have an account and profile that work sending an SMS message is a matter of configuring the sending address. If you've not set up Database Mail yet take a look at Kun Lee's tip ...
Barbara: you need to get with your Exschange Admin to solve it. Even though they provided you with a specific relay "relay.companyname.com" for you to use, it more than likely is NOT configured to allow relaying to outside domains. If you test this solution with a valid e-mail address in your own domain (companyname.com) it works as you already described. They might have to setup a separate relay for you to use (with basic auth/password) that allows for external relays.
Tuesday, May 07, 2013 - 10:09:14 AM - Barbara Novak
Thanks all. The Exchange admin created a contact that I called 'DBA Pager' using my phone account for the email. Now I get alerts via text message. Using a generic account means one change in the event the DBA leaves.