views:

95

answers:

2

I have a SQL Server 2008 procedure that sends email via sp_send_dbmail.

I'm using the following code:

  set @bodyText = ( select 
                      N'Here is one line of text ' +
                      N'It would be nice to have this on a 2nd line ' +
                      N'Below is some data: ' +
                      N' ' +
                      N' ' +
                      field1 +
                      N' ' +
                      field2 +
                      N' ' +
                      N'This is the last line'
                    from myTable )

    EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'myProfile',
        @recipients = @to,
        @body = @bodyText,
        @body_format = 'TEXT',
        @subject = 'Testing Email' ;

My myProfile is set to use the local smtp server, which results in a .EML file in c:\inetpub\mailroot\queue

When I open one of those .eml files (ug - the only thing that can open them is outlook express, looking at them in anything else just shows the body as a base64 encoded blob.) it looks like it's rendering the result as HTML - so I'm not sure if the problem is in the client, or

I've tried putting \n into the message, but that didn't work. How can I send plain text with line breaks, and verify that the end result looks correct?

BTW, I can't actually send the email to test it with real email clients - corp. network is locked down.

+1  A: 

I've always used CHAR(13)+CHAR(10) to create line breaks (which seems to work mixed in with nvarchar values) in TSQL, so try something like this:

DECLARE @CRLF char(2)
       ,@bodyText nvarchar(max)
       ,@field1  nvarchar(10)
       ,@field2  nvarchar(10)
SELECT @CRLF=CHAR(13)+CHAR(10)
      ,@field1='your data'
      ,@field2='and more'

set @bodyText =
                N'Here is one line of text ' 
                +@CRLF+ N'It would be nice to have this on a 2nd line ' 
                +@CRLF+ N'Below is some data: ' + N' ' + N' ' + ISNULL(@field1,'') + N' ' + ISNULL(@field2 + N' ' ,'')
                +@CRLF+ N'This is the last line' 


PRINT @bodyText

OUTPUT:

Here is one line of text 
It would be nice to have this on a 2nd line 
Below is some data:   your data and more 
This is the last line

this CHAR(13)+CHAR(10) will work with msdb.dbo.sp_send_dbmail, I send formatted e-mails using that all the time.

KM
A: 

You aren't actually inserting any line breaks. Does this work?

set @bodyText = ( SELECT 
N'Here is one line of text 
It would be nice to have this on a 2nd line 
Below is some data: 


' + field1 + N' 

' + field2 + N' 

' + N'This is the last line'
                    FROM myTable )
Martin Smith
Surprisingly, it does! In fact, I can put whole blocks into one pair of quotes.
chris
really makes the code look funky, I used to do it that way. however, I found the `CHAR(13)+CHAR(10)` works better with indenting queries and other code.
KM
Yes it doesn't really work very well with indenting as the `'` needs to go right at the start of the line to avoid injecting a whole load of surplus spaces but it can look a bit more WYSIWYG where that isn't a concern.
Martin Smith