首先,我回顾了以下问题并将其纳入我的问题:HTML Format in sp_send_dbmail
我正在尝试动态构建一封电子邮件,以便在需要时发送给不同的人。看来我的字符串构建只是查找并发送电子邮件。问题是电子邮件只显示为看起来像HTML的文本。这是我的SQL:
HTML_CTE ( EmailRecipients, EmailSubject, EmailBody )
AS ( SELECT Strings.Email AS EmailRecipients ,
'NOTIFICATION: You have exceeded the alloted meal amount' AS EmailSubject ,
N'<HTML>
<HEAD>
<STYLE>
TD {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:11pt;}
</STYLE>
</HEAD>
<BODY>
<H3>' + Strings.PersonName + N',</H3>
<br/>
<p> On the following dates you spent the indicated amounts:</p>
<br/>
<table cellpadding=0 cellspacing=0 border=0>
<TR>
<TD>Date</TD>
<TD></TD>
<TD>Amount</TD>
</TR>'
+ Strings.Exceptions +
N'</table>
<p>You will see $' + CAST(Strings.TotalOver AS VARCHAR(MAX)) + N' deducted from your paycheck. </p>
</BODY>
</HTML>' AS EmailBody
FROM AllDataStrings_CTE Strings
)
SELECT @ExecSQL = ( SELECT N'EXEC msdb.dbo.sp_send_dbmail'
+ N' @profile_name = ''GrillExceptions'''
+ N' ,@recipients = '''
+ HTML_CTE.EmailRecipients + N''''
+ N' ,@subject = ''' + HTML_CTE.EmailSubject
+ N'''' + N' ,@body = ''' + HTML_CTE.EmailBody
+ N'''' + N' ,@body_format = ''HTML''; '
FROM HTML_CTE
FOR
XML PATH('')
);
SELECT @ExecSQL;
EXEC sys.sp_executesql @ExecSQL;
上面有几个CTE构建了表并获得了正确的数据。从最终SELECT输出的字符串是:
EXEC msdb.dbo.sp_send_dbmail @profile_name = 'GrillExceptions',
@recipients = '*******@******.com',
@subject = 'NOTIFICATION: You have exceeded the alloted meal amount',
@body = '<HTML>
 <HEAD>
 <STYLE>
 TD {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:11pt;}
 </STYLE>
 </HEAD>
 <BODY>
 <H3>James Shinevar,</H3>
 <br/>
 <p> On the following dates you spent the indicated amounts:</p>
 <br/>
 <table cellpadding=0 cellspacing=0 border=0>
 <TR>
 <TD>Date</TD>
 <TD></TD>
 <TD>Amount</TD>
 </TR><TR><TD>2016-11-22</TD><TD>&#x20;</TD><TD>8.50</TD></TR></table>
 <p>You will see $0.50 deducted from your paycheck. </p>
 </BODY>
 </HTML>',
@body_format = 'HTML';
这导致以下内容在电子邮件正文中发送:
<HTML> <HEAD> <STYLE> TD {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:11pt;} </STYLE> </HEAD> <BODY> <H3>***** *****,</H3> <br/> <p> On the following dates you spent the indicated amounts:</p> <br/> <table cellpadding=0 cellspacing=0 border=0> <TR> <TD>Date</TD> <TD></TD> <TD>Amount</TD> </TR><TR><TD>2016-11-22</TD><TD> </TD><TD>8.50</TD></TR></table> <p>You will see $0.50 deducted from your paycheck. </p> </BODY> </HTML>
我已检查并确保我收到其他HTML格式的电子邮件。这个问题不是我的电子邮件客户端设置为纯文本。任何想法或帮助将不胜感激。
答案 0 :(得分:3)
在您的示例中,@body
参数已经是HTML编码的,因此这就是您在电子邮件中获取HTML的原因。您使用FOR XML PATH
是罪魁祸首,因为它正在执行您不需要的编码。找到一种更好的方法来构建@ExecSQL
- 我认为你过度复杂了。
EXEC msdb.dbo.sp_send_dbmail @profile_name = 'GrillExceptions',
@recipients = '*******@******.com',
@subject = 'NOTIFICATION: You have exceeded the alloted meal amount',
@body = '<HTML>
 <HEAD>
 <STYLE>
 TD {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:11pt;}
 </STYLE>
 </HEAD>
 <BODY>
 <H3>James Shinevar,</H3>
 <br/>
 <p> On the following dates you spent the indicated amounts:</p>
 <br/>
 <table cellpadding=0 cellspacing=0 border=0>
 <TR>
 <TD>Date</TD>
 <TD></TD>
 <TD>Amount</TD>
 </TR><TR><TD>2016-11-22</TD><TD>&#x20;</TD><TD>8.50</TD></TR></table>
 <p>You will see $0.50 deducted from your paycheck. </p>
 </BODY>
 </HTML>',
@body_format = 'HTML';
答案 1 :(得分:0)
您应该将解决方案重写为基于集合和基于xml的解决方案,而不是动态sql。
您的问题可能在@body中 - 您将整个html发送到html正文:
y = bar AND x != foo
您应该使用div或table标签。