Wednesday, December 9, 2015

Send SMS From SQL Server Using SMS API


                Declare @MobileNo varchar(12)='Mobile Number', @smstext as varchar(300)='Message'  
                Declare @iReq int,@hr int   
                Declare @sUrl as varchar(500)   
                DECLARE @errorSource VARCHAR(8000)  
                DECLARE @errorDescription VARCHAR(8000)   
                EXEC @hr = sp_OACreate 'Microsoft.XMLHTTP', @iReq OUT                  
                if @hr <> 0   
                  Raiserror('sp_OACreate Microsoft.XMLHTTP FAILED!', 16, 1)   
                set @sUrl='http://sapteleservices.com/SMS_API/sendsms.php?username=YourUserName&password=YourPassword&mobile=#MobNo#&sendername=YourCode&message=#Msg#&routetype=1'  
                set @sUrl=REPLACE(@sUrl,'#MobNo#',@MobileNo)   
                set @sUrl=REPLACE(@sUrl,'#Msg#',@smstext)   
                EXEC @hr = sp_OAMethod @iReq, 'Open', NULL, 'GET', @sUrl, true   
                if @hr <> 0   
                     Raiserror('sp_OAMethod Open FAILED!', 16, 1)   
                EXEC @hr = sp_OAMethod @iReq, 'send'   
                if @hr <> 0   
                  Begin   
                    EXEC sp_OAGetErrorInfo @iReq, @errorSource OUTPUT, @errorDescription OUTPUT  
                    Raiserror('sp_OAMethod Send FAILED!', 16, 1)   
                  end   
                else   
                Begin  
                  EXEC @hr = sp_OAGetProperty @iReq,'responseText'  
                  /*SMS LOG IF ANY*/  
                end  

*IF THERE ANY ERROR PLEASE EXECUTE THIS FIRST THEN TRY AGAIN

 exec sp_configure 'show advanced options', 1  
 go  
 reconfigure  
 go  
 exec sp_configure 'Ole Automation Procedures', 1 -- Enable  
 -- exec sp_configure 'Ole Automation Procedures', 0 -- Disable  
 go  
 reconfigure  
 go  
 exec sp_configure 'show advanced options', 0  
 go  
 reconfigure  
 go