Learn more about SQL Server tools

   
   















































SQL Server Alerts with Text Messaging from SQL Server Database Mail

MSSQLTips author John Grover By:   |   Read Comments (18)   |   Related Tips: More > Database Mail
Problem

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.

Solution

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.

Example

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!

Provider

Gateway Domain

Alltel PhoneNumber@message.alltel.com
AT&T PhoneNumber@txt.att.net
Sprint PhoneNumber@messaging.sprintpcs.com
T-Mobile PhoneNumber@tmomail.net
US Cellular PhoneNumber@email.uscc.net
Verizon PhoneNumber@vtext.com
Virgin Mobile PhoneNumber@vmobl.com

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 1235550000@vtext.com 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.

Send Test E-Mail

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 = "1235550000@vtext.com",
    @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 1235550000@vtext.com, 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.

New On-Call Operator

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.

click over to the Notifications Tab, select Alerts
Next Steps
  • 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 Update: 4/4/2013


About the author
MSSQLTips author John Grover
John Grover is a 25-year IT veteran with experience in SQL Server, Oracle, clustering and virtualization.

View all my tips
Related Resources


print tip Print  
Become a paid author





join MSSQLTips for free SQL Server tips     



Learn more about SQL Server tools
Post a comment or let the author know this tip helped you.

       All comments are reviewed, so stay on subject or we may delete your comment.

*Name   *Email Notify for updates



       Note: your email address is not published. Required fields are marked with an asterisk (*)


Get free SQL tips:

*Enter Code refresh code     



Wednesday, May 08, 2013 - 11:08:30 AM - John Grover Read The Tip

Massoud-

According to this page there are some international carriers that do... http://www.emailtextmessages.com/, but I would suggest you check with your specific carrier to find out for sure.


Wednesday, May 08, 2013 - 9:56:43 AM - Massoud Read The Tip

will this be work in India with Indian telecom providers ?

Pls suggest..


Wednesday, May 08, 2013 - 9:31:03 AM - John Grover Read The Tip

Sachin-

The code supplied is for SQL Server 2005 and above using Database Mail. SQL Server 200 uses SQL Mail. This KB article may be helpful for you

http://support.microsoft.com/kb/312839

 


Wednesday, May 08, 2013 - 3:49:19 AM - Sachin Read The Tip

I have SQL server 2000, when I run the below query

 

 

USE [msdb]
GO
EXEC msdb.dbo.sp_send_dbmail 
    @recipients = "1235550000@vtext.com",
    @subject = "Test SMS",
    @body = "This is only a test"
GO
 
following message comes
 
Server: Msg 2812, Level 16, State 62, Line 1 Could not find stored procedure 'msdb.dbo.sp_send_dbmail'. 
 

 

 


Tuesday, May 07, 2013 - 5:26:13 PM - Anand Read The Tip

Good one Thanks for sharing..............


Tuesday, May 07, 2013 - 10:09:14 AM - Barbara Novak Read The Tip

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.


Tuesday, May 07, 2013 - 9:04:53 AM - Richard M Read The Tip

Nice tip. Thanks John!

 

  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 - 2:50:40 AM - Lakki Read The Tip

Awesome Tip.


Friday, May 03, 2013 - 10:37:57 AM - John Grover Read The Tip

Barbara-

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 ...

http://www.mssqltips.com/sqlservertip/1736/sql-server-database-mail-and-sql-agent-mail-setup-by-using-a-script/

Armed with that information and the server and account information from your email admin you should be good to go!


Wednesday, May 01, 2013 - 6:59:59 PM - Barbara Novak Read The Tip

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. 

 

 


Monday, April 22, 2013 - 9:07:39 AM - Seth Delconte Read The Tip

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.


Thursday, April 11, 2013 - 4:38:27 PM - Dawn Read The Tip

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 08, 2013 - 11:00:42 AM - John Read The Tip

Sapen-

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.


Sunday, April 07, 2013 - 11:27:38 PM - Ravi Read The Tip

Good one. Thanks john.. i am gonna implement for all my servers.

 


Thursday, April 04, 2013 - 2:21:29 PM - Rajesh Read The Tip

Very nice. I am start using new SMS alerts for all my production Server.

Thanks.


Thursday, April 04, 2013 - 9:51:49 AM - Sapen Read The Tip

Thanks for the Good tip. Its a bit expensive. However we can configure emails in our phones and have the alerts configured and send email to us instead of text. Just a thought.


Thursday, April 04, 2013 - 9:28:00 AM - Ranga Read The Tip

Nice....


Thursday, April 04, 2013 - 9:11:30 AM - Seth Delconte Read The Tip

Nice tip - thanks John.




 
Sponsor Information