Quantcast
Channel: Brent Ozar Unlimited®
Viewing all articles
Browse latest Browse all 3153

How to Send Email with SQL Server’s sp_send_dbmail

$
0
0

So you’re writing T-SQL code and you’ve decided that you want to send emails to customers, employees, or the general public. Perhaps you need to send order updates or low stock notifications.

Stop right there. You don’t really wanna do that with SQL Server. Let’s talk about why.

It’s not easy to troubleshoot. SQL Server’s database mail doesn’t have the kind of robust troubleshooting tools necessary to understand why specific emails are delayed or non-deliverable. This is a really important task for customer-facing emails: your customers are gonna say, “I’m not getting your emails,” and your manager is gonna ask why, and you’re not going to have good answers.

Email deliverability is hard. These days, anti-spam and antivirus vendors are doing all kinds of tricks to suss out which emails are sent by humans versus which ones are automated. It’s very easy for your SQL Server’s IP address to end up on blacklists, and depending on how the blacklist is managed, your outgoing emails can also affect the deliverability of your company’s human emails, too. You don’t want your company’s email admins to say, “Well, folks, looks like our emails are all getting marked as spam because one of our developers messed up our sending reputation.”

There are legal requirements on commercial emails. You need to comply with things like the CAN-SPAM Act, giving recipients a clear unsubscribe link that actually works. If users don’t see that, they’ll simply mark your email as spam – and boom, your deliverability rates get shot. Malicious users (like me) simply block any email that doesn’t comply with the CAN-SPAM Act. I don’t give a damn about your company’s email deliverability problems – if you don’t care enough to comply with the law, I’m blocking all further emails from your company.

SQL Server doesn’t make any of that easy. Instead, build your emails in app code like C# or Java, and use transactional email providers like Sendgrid or Postmark to make sure the emails are legal and they get delivered to the right inboxes. You’re cursing me right now because I just made your job a little bit harder, but trust me, you’re going to thank me a year from now.


Viewing all articles
Browse latest Browse all 3153

Trending Articles