Send SMS & Email Using Backend
Kailash...
206
Points
23
Posts
|
Rahul M...
4916
Points
27
Posts
|
Hi Kailash, There is no in-build method to sending SMS but you can use external xmlhttp api in store procedure. For email sending there is a method 'msdb.dbo.sp_send_dbmail' that you can use to send email. For example DECLARE @tableHTML NVARCHAR(MAX) ;
SET @tableHTML =
N'<H1>Work Order Report</H1>' +
N'<table border="1">' +
N'<tr><th>Work Order ID</th><th>Product ID</th>' +
N'<th>Name</th><th>Order Qty</th><th>Due Date</th>' +
N'<th>Expected Revenue</th></tr>' +
CAST ( ( SELECT td = wo.WorkOrderID, '',
td = p.ProductID, '',
td = p.Name, '',
td = wo.OrderQty, '',
td = wo.DueDate, '',
td = (p.ListPrice - p.StandardCost) * wo.OrderQty
FROM AdventureWorks.Production.WorkOrder as wo
JOIN AdventureWorks.Production.Product AS p
ON wo.ProductID = p.ProductID
WHERE DueDate > '2004-04-30'
AND DATEDIFF(dd, '2004-04-30', DueDate) < 2
ORDER BY DueDate ASC,
(p.ListPrice - p.StandardCost) * wo.OrderQty DESC
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</table>' ;
EXEC msdb.dbo.sp_send_dbmail @recipients='yourfriend@niceone-Works.com',
@subject = 'Nice work order list',
@body = @tableHTML,
@body_format = 'HTML' ;
For more information for sending email follow:https://msdn.microsoft.com/en-in/library/ms190307.aspx For SMS, I think you need to use third party api in procedure and execute this procedure in trigger. Trigger: ALTER TRIGGER [dbo].[mytrigger]
ON [dbo].[smslog]
AFTER INSERT
AS
Declare @MobileNo int
Declare @smstext as varchar(300)
Begin
set @MobileNo = ( select mobile from inserted)
set @smstext = (select smstext from inserted)
set @sResponse = 'test'
Exec pr_SendSmsSQL @MobileNo, @smstext, @sResponse
END
Procedure: create procedure [dbo].[pr_SendSmsSQL]
@MobileNo varchar(12),
@smstext as varchar(300),
@sResponse varchar(1000) OUT
as
BEGIN
Declare @iReq int,@hr int
Declare @sUrl as varchar(500)
DECLARE @errorSource VARCHAR(8000)
DECLARE @errorDescription VARCHAR(8000)
-- Create Object for XMLHTTP
EXEC @hr = sp_OACreate 'Microsoft.XMLHTTP', @iReq OUT
print @hr
if @hr <> 0
Raiserror('sp_OACreate Microsoft.XMLHTTP FAILED!', 16, 1)
set @sUrl='https://api.someapi.com/http/sendmsg?user=niceone&password=xyz&api_id=3360313&to=#MobNo#&text=#Msg#'
set @sUrl=REPLACE(@sUrl,'#MobNo#',@MobileNo)
set @sUrl=REPLACE(@sUrl,'#Msg#',@smstext)
print @sUrl
-- sms code start
EXEC @hr = sp_OAMethod @iReq, 'Open', NULL, 'GET', @sUrl, true
print @hr
if @hr <> 0
Raiserror('sp_OAMethod Open FAILED!', 16, 1)
EXEC @hr = sp_OAMethod @iReq, 'send'
select @iReq
print @hr
if @hr <> 0
Begin
EXEC sp_OAGetErrorInfo @iReq, @errorSource OUTPUT, @errorDescription OUTPUT
SELECT [Error Source] = @errorSource, [Description] = @errorDescription
Raiserror('sp_OAMethod Send FAILED!', 16, 1)
end
else
Begin
EXEC @hr = sp_OAGetProperty @iReq,'responseText', @sResponse OUT
print @hr
insert into send_log (Id, mobile, sendtext, response, created, createddate)
values(0, @MobileNo, @smstext, @sResponse, 'System', GETDATE())
end
end
Posted On:
13-Jan-2017 00:20
|
Rahul M...
4916
Points
27
Posts
|
Hi Kailash, You need to replace 'responseText' in
with respected response property.
Posted On:
17-Apr-2017 23:56
|
Kailash...
206
Points
23
Posts
|
Posted On:
18-Apr-2017 00:32
|
Kailash...
206
Points
23
Posts
|
Posted On:
18-Apr-2017 00:40
|
Kailash...
206
Points
23
Posts
|
Posted On:
18-Apr-2017 00:43
|
Rahul M...
4916
Points
27
Posts
|
Hi, Try different variable '@txtResponse' like following:
Posted On:
18-Apr-2017 02:33
|
Kailash...
206
Points
23
Posts
|
Posted On:
18-Apr-2017 04:37
|
Rahul M...
4916
Points
27
Posts
|
Hi Guys, I got success to test it on my local SQL Server by modifying configuration as
And I modified the store procedure to see the how we are not getting response.
And Now I found following error message as:
I don't know why this is happening although the sms message is sent successfully.
Posted On:
19-Apr-2017 04:52
|