I have a stored procedure which sent emails to few recipients. In this I want to send to two differet recipients using #copy_recipients. But I get a syntax error. How to make this work?
stored procedure code
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'mail',
#recipients = #Mail1,
#copy_recipients = #Mail2;#Mail3,
#body =#body ,
#subject =#subject
You need to add ; (semicolon) between e-mail addresses using string concatenation:
DECLARE #copy_to varchar(max)= #Mail2+';'+#Mail3
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'mail',
#recipients = #Mail1,
#copy_recipients = #copy_to,
#body =#body ,
#subject =#subject
You can read MSDN article here
[ #recipients= ] 'recipients'
Is a semicolon-delimited list of e-mail
addresses to send the message to. The recipients list is of type
varchar(max). Although this parameter is optional, at least one of
#recipients, #copy_recipients, or #blind_copy_recipients must be
specified, or sp_send_dbmail returns an error.
[ #copy_recipients= ] 'copy_recipients'
Is a semicolon-delimited list
of e-mail addresses to carbon copy the message to. The copy recipients
list is of type varchar(max). Although this parameter is optional, at
least one of #recipients, #copy_recipients, or #blind_copy_recipients
must be specified, or sp_send_dbmail returns an error.
Related
This question already has an answer here:
Sending attachment with sp_send_dbmail getting error Failed to initialize sqlcmd library with error number -2147467259
(1 answer)
Closed 1 year ago.
I am trying to send an email containing the results of a stored procedure call inside SQL Server.
declare #querytext nvarchar(100)
set #querytext = 'sp_get_SPresults #item_name = ''Name of Group'''';
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'My Email',
#recipients = 'anaddress#adomain.com',
#body = 'Email',
#subject = 'Suitable Subject' ,
#query = #querytext,
#attach_query_result_as_file = 1,
#query_result_header = 1,
#query_no_truncate = 1;
But when I run this, I get a
Msg 22050, Level 16, State 1, Line 23
Failed to initialize sqlcmd library with error number -2147467259 error.
When I send a test email in SQL Server it sends it fine. Even just setting #querytext as "select * from [Table]" doesn't work. I've seen elsewhere that including #query_result_header=1 can clear this kind of issue, but it doesn't appear to make a difference here.
Any help would be appreciated.
Thanks
In the end, for whatever reason, I tried using just the following parameters:
#profile_name
#recipients
#body
#subject
#body_format
#execute_query_database
This now works. Thanks everyone for your help. :)
I have some custom audit processes that send emails with sp_send_mail. I discovered recently that testing was sending out emails to everybody because the recipient address was hard coded as a variable value. I'd rather use an Operator so I don't have to alter code moving from one environment to the next. I've googled but this doesn't seem to be a thing unless I'm just using the wrong key words.
What is the proper #recipients value to use an Operator or should I be using a different proc all together?
You're definitely on the right track but you just need to retrieve the email address of the operator based on the name of the operator like this:
DECLARE #OperatorName sysname = N'OnCallDBAs';
DECLARE #OperatorEmailAddress nvarchar(100)
= (SELECT email_address
FROM msdb.dbo.sysoperators
WHERE [name] = #OperatorName);
IF #OperatorEmailAddress IS NOT NULL
BEGIN
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'Adventure Works Administrator',
#recipients = #OperatorEmailAddress,
#body = 'The stored procedure finished successfully.',
#subject = 'Automated Success Message';
END;
Hope that helps.
I'm trying to receive bounce back emails to multiple accounts, and I have tried setting up a email box that forwards out, but that isn't working. Is there a way to add multiple email addresses into the #from_address parameter?
EXECUTE msdb.dbo.sp_send_dbmail
#profile_name = #profile_name,
#recipients = 'test#test.com',
#subject = 'test',
#body = 'works',
#body_format = 'HTML',
#from_address = #email
The above code works with an email being bounced back, but is there a way to add multiple emails to the #from_address. CCing doesn't work as it just sends them the email and doesn't include the bounce back
#from_address = 'email1; email2' - doesn't work
An email can only be from a single address, however your #recipients should be able to contain multiple email addresses:
#recipients = 'test#test.com; test2#test.com'
...
#from_address = 'from#test.com'
If you want to have multiple people see a bounce-back email, configure an email, i.e. errors#test.com and allow multiple people to access that email.
I am using sp_send_dbmail to send an email containing entries in table Persons. It throws following error -
Failed to initialize sqlcmd library with error number -2147467259.
My query is below -
declare #q varchar(max)
SET #q='SELECT FirstName,LastName FROM dw_extract.dbo.Persons'
USE msdb
EXEC sp_send_dbmail
#profile_name = 'profile_name',
#recipients = 'abc#xyz.com',
#subject = 'T-SQL Query Result',
#body = 'The result from SELECT is appended below',
#execute_query_database = 'dw_extract',
#query = #q
I did some research and found that few people who faced such problem could get rid of it by explicitly stating column names and database of the table used. I did that but still it continues to throw the error.
I'm using the sp_send_dbmail stored procedure, and I'm trying to include the result from a query in the body string. I can't seem to get this to work. Any ideas would be much appreciated.
use msdb
go
exec sp_send_dbmail
#profile_name = 'WarehouseEmailer',
#recipients = 'someone#example.com',
#subject = 'Database Mail Test 1',
#Body = 'This is a test email from SQL Server. <br> This should be line 2. <br> The
subject\'s first name is: '+ (select top(1) FirstName from Warehouse.dbo.Subject),
#body_format = 'HTML'
Obviously this example just a simple test, but it is exactly the type of data I'd like to pass to the stored procedure to include in the email body. I look forward to your responses and many thanks for any advice!
Dynamic SQL, or concatenated strings, cannot be passed into the procedures parameters like that.
Try this:
use msdb
go
declare #msgbody nvarchar(1000)
set #msgbody = 'This is a test email from SQL Server.' + char(10) + 'This should be line 2. <br> The
subject''s first name is: '+ (select top(1) FirstName from Warehouse.dbo.Subject)
exec sp_send_dbmail
#profile_name = 'WarehouseEmailer',
#recipients = 'someone#example.com',
#subject = 'Database Mail Test 1',
#Body = #msgbody,
#body_format = 'HTML'