skip to Main Content

The SQL below is to send internal emails using sp_send_dbmail. The email is received but the internal styling is not used.

  • I confirmed that I do external receive emails with HTML. They display correctly.
  • Per the code below, body_format is set to HTML

How do I get the email to format correctly and use the internal stylesheet?

declare @body nvarchar (max)
declare @subj nvarchar(300)
declare @clientname nvarchar(100)='myClient'
declare @DataCurrentAsOf nvarchar(100)='01/01/01'
SET @body ='
<!DOCTYPE html>
<html lang="en">
    <style>
        body {
            background-color: lightgrey;
        }
    </style>

    <body>
        Please do not reply to this email.<br><br>
    </body>
</html>
' 

--SET @body = @body + @xml +'</table></body></html>'
Select @body
Set @subj='Daily Counts'
------------------------------------------------------------------------------

Execute msdb.dbo.sp_send_dbmail
    @profile_name='myProfile', 
    @recipients = '[email protected]',
    @body_format = 'HTML',
    @body=@body,
    @subject=@subj

2

Answers


  1. Email clients have a lot of different levels of support to some CSS and markup features, including declared styles (in head or even in body)

    In general, most email clients preferably support inline styles well. So you can probably get a better result adding the style to the body tag:

    <body style="background-color: lightgrey;">
        Please do not reply to this email.<br><br>
    </body>
    

    You can see examples of what is most commonly supported in email clients in this list (please check mobile ones it’s a must because many of them lack support for many common styling features present on desktop clients)

    https://templates.mailchimp.com/resources/email-client-css-support/

    Login or Signup to reply.
  2. The short answer is, that the <style> tag is only supported by ~70%. As such you should not use the head-style but always use inline-style.

    <body style="background-color: lightgrey;">
      Please do not reply to this email.<br><br>
    </body>
    

    Website to check HTML and CSS Support within emails

    Now the longer answer is to answer your comment about what the reason for that is.

    We have to look into the history of HTML which was normed by W3C. The last normed version was HTML4, also a reason why email supports HTML4 more reliably than HTML5.

    HTML5 was never normed or specified!

    Where does HTML5 come from?

    HTML4 needed extensions and Mozilla and Opera proposed that HTM4 should be extended in a backward-compatible way along with other things such as error handling, and open specification processes. W3C held a workshop for that proposal in June 2004 and denied that.
    As consequence, a new open group was formed under the name WHATWG. WHATWG made specification recommendations that is known now as HTML5 and are implemented by the major browser vendors. However, one major party was missing: Microsoft which rather stuck to W3C as WHATWG missing a patent policy.

    That is the main reason why IE died and Microsoft services such as Outlook still don’t work well with HTML and newer CSS use cases that are recommended by WHATWG.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search