How to send an email to multiple recipients ?
Stefan Bogdanescu
Founder & Senior Architect
Mastering Multi-Recipient Emails in SQL: Solving the Stored Procedure Syntax Error
As senior developers, we often rely on stored procedures to handle complex business logic, and sending emails is a very common requirement. When trying to send an email to multiple people—some recipients, some copies—the syntax for handling these lists can quickly become confusing. The issue you are encountering with sp_send_dbmail likely stems from how the system expects recipient lists to be formatted within the parameters.
This post will analyze the syntax error you faced when attempting to use @copy_recipients and provide a robust, developer-focused solution for sending emails to multiple parties using SQL Server's Database Mail functionality.
Understanding the Limitation: Why the Syntax Failed
The core issue lies in how you are passing the recipient lists to the stored procedure. Functions like sp_send_dbmail expect the @recipients and @copy_recipients parameters to contain a single string that lists all emails, usually separated by commas.
When you tried to use syntax like @recipients = @Mail1, @copy_recipients = @Mail2;@Mail3, SQL interprets this as an invalid command structure because it doesn't recognize the comma-separated list directly within the parameter assignment in that manner. The system needs a single, cohesive string input for each recipient field.
The Developer Solution: Concatenating Recipient Lists
The correct approach is to first concatenate all the intended recipients (the primary recipients and the CC recipients) into a single, properly formatted comma-separated string before passing it to the mail function. This ensures that the Mail subsystem receives a single, valid list of addresses.
Here is the corrected pattern for handling multiple recipients:
Step 1: Prepare the Recipient Lists in Your Stored Procedure
Instead of trying to pass separate variables for @recipients and @copy_recipients, you should build one comprehensive string containing everyone who needs to receive the email.
CREATE PROCEDURE dbo.SendMultiRecipientEmail
@Body NVARCHAR(MAX),
@Subject NVARCHAR(255),
@RecipientsList NVARCHAR(MAX) -- This will hold all emails combined
AS
BEGIN
-- Example: Assume @RecipientsList is formatted as "email1@example.com, email2@example.com"
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'mail',
@recipients = @RecipientsList, -- Pass the combined list here
@body = @Body,
@subject = @Subject;
END
Step 2: Executing the Procedure Correctly
In your calling code, you must construct the final string before execution. This is where robust data handling becomes critical, much like structuring models in modern frameworks like Laravel, where input validation and preparation are key to successful output generation.
-- Example of how you would call the procedure:
DECLARE @Mail1 NVARCHAR(255) = 'user_a@domain.com';
DECLARE @Mail2 NVARCHAR(255) = 'user_b@domain.com';
-- Combine recipients using a comma separator
DECLARE @AllRecipients NVARCHAR(MAX);
SET @AllRecipients = @Mail1 + ', ' + @Mail2;
DECLARE @Body NVARCHAR(MAX) = 'This is the content of the email.';
DECLARE @Subject NVARCHAR(255) = 'Important Update';
EXEC dbo.SendMultiRecipientEmail
@Body = @Body,
@Subject = @Subject,
@RecipientsList = @AllRecipients;
By combining the recipient variables into a single string (@Mail1 + ', ' + @Mail2), you satisfy the requirement of sp_send_dbmail, resolving the syntax error and ensuring all intended recipients are successfully notified.
Best Practices for Email Delivery
When dealing with data flow, especially in backend systems, structuring your input correctly prevents downstream errors. Always validate that the concatenated string only contains valid email addresses before attempting to send mail. If you were building a larger application integrating services, focusing on clean data pipelines—ensuring inputs are structured and validated—is paramount, something modern frameworks like Laravel emphasize heavily when managing requests and responses.
Conclusion
The syntax error was due to passing disparate variables instead of a single, formatted string required by the Database Mail function. By implementing a step where you concatenate all @recipients and @copy_recipients into a single comma-separated string before execution, you ensure that your stored procedure operates correctly and reliably sends emails to multiple targets. Always prioritize data preparation to achieve stable and predictable results in your backend logic.
Note: Blog content is currently available in English.