|
1 | 1 | --- |
2 | | -title: DB Mail and Email Alerts with SQL Agent on Linux |
3 | | -description: Learn how to use DB Mail and how to set up Email Alerts with SQL Server Agent (mssql-server-agent) on Linux. |
| 2 | +title: Database Mail and email alerts with SQL Server Agent on Linux |
| 3 | +description: Learn how to use Database Mail and how to set up Email Alerts with SQL Server Agent (mssql-server-agent) on Linux. |
4 | 4 | author: rwestMSFT |
5 | 5 | ms.author: randolphwest |
6 | | -ms.date: 11/16/2023 |
| 6 | +ms.date: 11/30/2023 |
7 | 7 | ms.service: sql |
8 | 8 | ms.subservice: linux |
9 | 9 | ms.topic: conceptual |
10 | 10 | ms.custom: |
11 | 11 | - linux-related-content |
12 | 12 | --- |
13 | | -# DB Mail and Email Alerts with SQL Agent on Linux |
| 13 | +# Database Mail and email alerts with SQL Server Agent on Linux |
14 | 14 |
|
15 | 15 | [!INCLUDE [SQL Server - Linux](../includes/applies-to-version/sql-linux.md)] |
16 | 16 |
|
17 | | -The following steps show you how to set up DB Mail and use it with SQL Server Agent (**mssql-server-agent**) on Linux. |
| 17 | +This article shows how to set up Database Mail and use it with SQL Server Agent (**mssql-server-agent**) on Linux. |
18 | 18 |
|
19 | | -## 1. Enable DB Mail |
| 19 | +## 1. Enable Database Mail |
20 | 20 |
|
21 | 21 | ```sql |
22 | | -USE master |
| 22 | +USE master; |
23 | 23 | GO |
24 | | -sp_configure 'show advanced options',1 |
| 24 | + |
| 25 | +sp_configure 'show advanced options', 1; |
25 | 26 | GO |
| 27 | + |
26 | 28 | RECONFIGURE WITH OVERRIDE |
27 | 29 | GO |
28 | | -sp_configure 'Database Mail XPs', 1 |
| 30 | + |
| 31 | +sp_configure 'Database Mail XPs', 1; |
29 | 32 | GO |
30 | | -RECONFIGURE |
| 33 | + |
| 34 | +RECONFIGURE; |
31 | 35 | GO |
32 | 36 | ``` |
33 | 37 |
|
34 | 38 | ## 2. Create a new account |
35 | 39 |
|
36 | 40 | ```sql |
37 | | -EXECUTE msdb.dbo.sysmail_add_account_sp |
38 | | -@account_name = 'SQLAlerts', |
39 | | -@description = 'Account for Automated DBA Notifications', |
40 | | -@email_address = 'sqlagenttest@gmail.com', |
41 | | -@replyto_address = 'sqlagenttest@gmail.com', |
42 | | -@display_name = 'SQL Agent', |
43 | | -@mailserver_name = 'smtp.gmail.com', |
44 | | -@port = 587, |
45 | | -@enable_ssl = 1, |
46 | | -@username = 'sqlagenttest@gmail.com', |
47 | | -@password = '<password>' |
| 41 | +EXECUTE msdb.dbo.sysmail_add_account_sp @account_name = 'SQLAlerts', |
| 42 | + @description = 'Account for Automated DBA Notifications', |
| 43 | + @email_address = 'sqlagenttest@example.com', |
| 44 | + @replyto_address = 'sqlagenttest@example.com', |
| 45 | + @display_name = 'SQL Agent', |
| 46 | + @mailserver_name = 'smtp.example.com', |
| 47 | + @port = 587, |
| 48 | + @enable_ssl = 1, |
| 49 | + @username = 'sqlagenttest@example.com', |
| 50 | + @password = '<password>'; |
48 | 51 | GO |
49 | 52 | ``` |
50 | 53 |
|
51 | 54 | ## 3. Create a default profile |
52 | 55 |
|
53 | 56 | ```sql |
54 | 57 | EXECUTE msdb.dbo.sysmail_add_profile_sp |
55 | | -@profile_name = 'default', |
56 | | -@description = 'Profile for sending Automated DBA Notifications' |
| 58 | + @profile_name = 'default', |
| 59 | + @description = 'Profile for sending Automated DBA Notifications'; |
57 | 60 | GO |
58 | 61 | ``` |
59 | 62 |
|
60 | 63 | ## 4. Add the Database Mail account to a Database Mail profile |
61 | 64 |
|
62 | 65 | ```sql |
63 | 66 | EXECUTE msdb.dbo.sysmail_add_principalprofile_sp |
64 | | -@profile_name = 'default', |
65 | | -@principal_name = 'public', |
66 | | -@is_default = 1 ; |
67 | | - ``` |
| 67 | + @profile_name = 'default', |
| 68 | + @principal_name = 'public', |
| 69 | + @is_default = 1; |
| 70 | +GO |
| 71 | +``` |
68 | 72 |
|
69 | 73 | ## 5. Add account to profile |
70 | 74 |
|
71 | 75 | ```sql |
72 | 76 | EXECUTE msdb.dbo.sysmail_add_profileaccount_sp |
73 | | -@profile_name = 'default', |
74 | | -@account_name = 'SQLAlerts', |
75 | | -@sequence_number = 1; |
76 | | - ``` |
| 77 | + @profile_name = 'default', |
| 78 | + @account_name = 'SQLAlerts', |
| 79 | + @sequence_number = 1; |
| 80 | +GO |
| 81 | +``` |
77 | 82 |
|
78 | 83 | ## 6. Send test email |
79 | 84 |
|
80 | | -> [!NOTE] |
81 | | -> You might have to go to your email client and enable the "allow less secure clients to send mail." Not all clients recognize DB Mail as an email daemon. |
| 85 | +You might have to go to your email client and enable the **allow less secure clients to send mail** option. Not all clients recognize Database Mail as an email daemon. |
82 | 86 |
|
83 | | -``` |
| 87 | +```sql |
84 | 88 | EXECUTE msdb.dbo.sp_send_dbmail |
85 | | -@profile_name = 'default', |
86 | | -@recipients = 'recipient-email@gmail.com', |
87 | | -@Subject = 'Testing DBMail', |
88 | | -@Body = 'This message is a test for DBMail' |
| 89 | + @profile_name = 'default', |
| 90 | + @recipients = 'recipient-email@example.com', |
| 91 | + @subject = 'Testing DBMail', |
| 92 | + @body = 'This message is a test for DBMail' |
89 | 93 | GO |
90 | 94 | ``` |
91 | 95 |
|
92 | | -## 7. Set DB Mail Profile using mssql-conf or environment variable |
| 96 | +## 7. Set Database Mail profile using mssql-conf or environment variable |
93 | 97 |
|
94 | | -You can use the mssql-conf utility or environment variables to register your DB Mail profile. In this case, let's call our profile default. |
| 98 | +You can use the **mssql-conf** utility, or environment variables, to register your Database Mail profile. In this case, let's call our profile `default`. |
95 | 99 |
|
96 | | -```bash |
97 | | -# via mssql-conf |
98 | | -sudo /opt/mssql/bin/mssql-conf set sqlagent.databasemailprofile default |
99 | | -# via environment variable |
100 | | -MSSQL_AGENT_EMAIL_PROFILE=default |
101 | | -``` |
| 100 | +- Set via **mssql-conf**: |
| 101 | + |
| 102 | + ```bash |
| 103 | + sudo /opt/mssql/bin/mssql-conf set sqlagent.databasemailprofile default |
| 104 | + ``` |
102 | 105 |
|
103 | | -## 8. Set up an operator for SQLAgent job notifications |
| 106 | +- Set via environment variable: |
| 107 | + |
| 108 | + ```bash |
| 109 | + MSSQL_AGENT_EMAIL_PROFILE=default |
| 110 | + ``` |
| 111 | + |
| 112 | +## 8. Set up an operator for SQL Server Agent job notifications |
104 | 113 |
|
105 | 114 | ```sql |
106 | 115 | EXEC msdb.dbo.sp_add_operator |
107 | | -@name=N'JobAdmins', |
108 | | -@enabled=1, |
109 | | -@email_address=N'recipient-email@gmail.com', |
110 | | -@category_name=N'[Uncategorized]' |
| 116 | + @name = N'JobAdmins', |
| 117 | + @enabled = 1, |
| 118 | + @email_address = N'recipient-email@example.com', |
| 119 | + @category_name = N'[Uncategorized]'; |
111 | 120 | GO |
112 | 121 | ``` |
113 | 122 |
|
114 | 123 | ## 9. Send email when 'Agent Test Job' succeeds |
115 | 124 |
|
116 | | -``` |
| 125 | +```sql |
117 | 126 | EXEC msdb.dbo.sp_update_job |
118 | | -@job_name='Agent Test Job', |
119 | | -@notify_level_email=1, |
120 | | -@notify_email_operator_name=N'JobAdmins' |
| 127 | + @job_name = 'Agent Test Job', |
| 128 | + @notify_level_email = 1, |
| 129 | + @notify_email_operator_name = N'JobAdmins' |
121 | 130 | GO |
122 | 131 | ``` |
123 | 132 |
|
|
0 commit comments