Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed Images in emails created using SQL Server Database Mail

I'm working on an email solution in SQL Server ONLY that will use Database Mail to send out HTML formatted emails. The catch is that the images in the HTML need to be embedded in the outgoing email. This wouldn't be a problem if I were using a .net app to generate & send the emails but, unfortunately, all I have is SQL Server.

Is it possible for SQL Server to embed images on its own?

like image 971
NakedBrunch Avatar asked Aug 30 '08 14:08

NakedBrunch


3 Answers

You have two possibilities:

  1. (easy) Host the images somewhere, and reference them in the <img src="...">.
  2. (difficult) Encode them in Base64 and build a multipart MIME message with known content IDs, so they can be referenced in the message body via cid: URIs.

Each possibility has its downsides:

  1. Remote images may not be loaded on the modern e-mail clients for privacy.
  2. Probably rises spam score.

When the receiving clients are in your control (e.g. same organization), you might be equally fine with either way.

like image 65
Tomalak Avatar answered Oct 17 '22 20:10

Tomalak


Yes, what you need to do is include the images as attachments and then they can be referenced within the HTML.

Use the @file_attachment parameter of sp_send_dbmail

like image 24
Josef Avatar answered Oct 17 '22 20:10

Josef


You could try to encode the image as base64 and reference it directly in an img tag within the email ( <img src="data:image/png;base64[your encoded image here...] ) but i think most email clients correlate this technique with spam. I think you're better off referencing hosted images or simply attaching it to the email.

like image 1
nathan_jr Avatar answered Oct 17 '22 22:10

nathan_jr