SQL Server Alerts with Text Messaging from SQL Server Database Mail

By:   |   Comments (22)   |   Related: > 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 [email protected]
AT&T [email protected]
Sprint [email protected]
T-Mobile [email protected]
US Cellular [email protected]
Verizon [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.

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 = "[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.

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.


sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



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

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips



Comments For This Article




Monday, September 9, 2019 - 10:17:26 AM - John Grover Back To Top (82324)

Partha,

You will need to check with the carriers themselves. The information is probably on their website or available from their support department.


Monday, September 9, 2019 - 2:19:58 AM - Partha Mandayam Back To Top (82320)

Can we use this method with Indian mobile carriers?

What would be the email addresses to use?


Thursday, September 6, 2018 - 3:07:31 PM - Chris McKenzie Back To Top (77378)

Hi Raju.

I tested the steps in this article a couple of different ways:

  • sending the email with "1" in front of my phone number (ex: [email protected])
    • did not receive a message
  • sending the email without the "1" in front of my phone number (ex: [email protected])
    • message successfully received

My suggestions to you would be:

  • ensure your cell phone service includes text messaging
  • find the SMS Gateway for your cell phone provider (if one is available via your cell phone service provider)
  • try sending your message again, using both the "making a long distance call" format (my first example from above) and the "not making a long distance phone call" format (my second example from above)

As John mentioned to Massoud, if your cell phone service provider does not have an SMS Gateway available, a Google search should help you find free/inexpensive alternatives.

I apologize if my reply sounds/feel condenscending.....I certainly mean no disrespect to you.


Wednesday, June 27, 2018 - 5:07:43 AM - RajuBandaram Back To Top (76434)

Hi dear,

 

       Good Technique I'm Tried for this type of Logic to Send Text to Mobile Number Through DataBase. But It is Not Working, I Just Sent a Test mail From SQLMON to my mobile number. But I didn't get any Message to my device. Please Suggest me to do this.


Wednesday, May 8, 2013 - 11:08:30 AM - John Grover Back To Top (23817)

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 8, 2013 - 9:56:43 AM - Massoud Back To Top (23811)

will this be work in India with Indian telecom providers ?

Pls suggest..


Wednesday, May 8, 2013 - 9:31:03 AM - John Grover Back To Top (23810)

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 8, 2013 - 3:49:19 AM - Sachin Back To Top (23795)

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

 

 

USE [msdb]
GO
EXEC msdb.dbo.sp_send_dbmail 
    @recipients = "[email protected]",
    @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 7, 2013 - 5:26:13 PM - Anand Back To Top (23780)

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


Tuesday, May 7, 2013 - 10:09:14 AM - Barbara Novak Back To Top (23761)

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 7, 2013 - 9:04:53 AM - Richard M Back To Top (23760)

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 7, 2013 - 2:50:40 AM - Lakki Back To Top (23755)

Awesome Tip.


Friday, May 3, 2013 - 10:37:57 AM - John Grover Back To Top (23704)

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 1, 2013 - 6:59:59 PM - Barbara Novak Back To Top (23674)

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 Back To Top (23475)

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 Back To Top (23312)

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 8, 2013 - 11:00:42 AM - John Back To Top (23234)

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 7, 2013 - 11:27:38 PM - Ravi Back To Top (23225)

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

 


Thursday, April 4, 2013 - 2:21:29 PM - Rajesh Back To Top (23169)

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

Thanks.


Thursday, April 4, 2013 - 9:51:49 AM - Sapen Back To Top (23160)

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 4, 2013 - 9:28:00 AM - Ranga Back To Top (23158)

Nice....


Thursday, April 4, 2013 - 9:11:30 AM - Seth Delconte Back To Top (23155)

Nice tip - thanks John.















get free sql tips
agree to terms