6 Replies Latest reply on Dec 4, 2013 8:02 AM by dtwineham

    Cfmail is inserting a blank space in front of my email body

    dtwineham

      When I send a text formatted email using CFMail, Coldfusion 10, I am seeing an extra blank space at the beginning of the message body.  When I capture the .cfmail file from my spool directory, it looks like this in part:

       

      bcc:  dale@realgo.com

      replyto:  dale@realgo.com

      subject:  2 listings

      X-Mailer:  ColdFusion 10 Application Server

      List-Unsubscribe:  <http://overlord.dev.realgo.com/go/B3ANTR4IHSUB>

      X-Receiver:  dale@realgo.com,dale@realgo.com

      MIME-Version:  1.0

      Content-Transfer-Encoding:  7bit

      body:   $Prospective Buyer,

      body: 

      body:  RealGo, Inc.

      body: 

      body: 

      body:  For a detailed view of these listings including all photos, visit this link

      body: 

      body:  Residential-Detached Listings

      body: 

      body:  SRC   MLS #  PRICE $       BD   BA   SQFT  LOCALE                   STAT ADDRESS

      body:  IRES  722659 LP900,000     3    3    1948  Boulder                  A    593 Lee Hill Dr

      body:  ------------------------Total Residential-Detached Listings: 1

       

       

      Note that there are 2 spaces between "body:" and the text for that line, but on the first line there are 3 spaces.

       

      The "$" is on that line as a check to make sure my message body didn't itself have the extra space, it was generated by

      <cfset EmailBody = "$#EmailBody#">

       

      My cfmail is coded as follows:

      <cfmail

          to = "#Attributes.To#"

          from = "#Attributes.From#"

          cc = "#Attributes.CC#"

          bcc = "#Attributes.BCC#"

          replyTo = "#Attributes.Reply_To#"

          subject = "#Attributes.Subject#"

          type = "text"

          charset="us-ascii">

          <cfif "#Attributes.EmailUnsubscribeLink#" NEQ "">

              <cfmailparam name="List-Unsubscribe" value ="<#Attributes.EmailUnsubscribeLink#>">

          </cfif>

          <cfmailparam name="X-Receiver" value="#xReceivers#">

          <cfmailparam name="MIME-Version" value="1.0">

          <cfmailparam name="Content-Transfer-Encoding" value="7bit">

          #EmailBody#

      </cfmail>

       

      I have tried this with all the whitespace collapsed, to no effect.  Any ideas on what might be causing this extra space to appear?

       

      Thanks,

      dtwineham

       

        • 1. Re: Cfmail is inserting a blank space in front of my email body
          Steve Sommers Level 4

          If your code is exactly as shown above, the issue is your code/tag formatting. Notice there is at least one space before the #EmailBody#. I'm kind of surprised you don't also have an extra CR/LF as well. For kicks, try removing the CR/LF and all whitespace from the end of the last tag and the emailBody:

           

              <cfmailparam name="Content-Transfer-Encoding" value="7bit">#EmailBody#

           

          Dealing with whitspace is a royal pain.

          • 2. Re: Cfmail is inserting a blank space in front of my email body
            dtwineham Level 1

            Steve, thanks for the quick response.

             

            I have tried that, even putting that whole code segment on a single line with no spaces outside of a tag. .  It turns out that if you put a literal string in for the message body, it does include any leading white space.  But if all that ColdFusion encounters is a variable, you can have any number of spaces there and only the text in your variable comes out.  (Except for my phantom 1 leading space).

            • 3. Re: Cfmail is inserting a blank space in front of my email body
              Steve Sommers Level 4

              While ugly, I'm certain the following will work:

               

              <cfmail

              to = "#Attributes.To#"

              from = "#Attributes.From#"

              cc = "#Attributes.CC#"

              bcc = "#Attributes.BCC#"

              replyTo = "#Attributes.Reply_To#"

              subject = "#Attributes.Subject#"

              type = "text"

              charset="us-ascii"><cfif "#Attributes.EmailUnsubscribeLink#" NEQ ""><cfmailparam name="List-Unsubscribe" value ="<#Attributes.EmailUnsubscribeLink#>"></cfif><cfmailparam name="X-Receiver" value="#xReceivers#"><cfmailparam name="MIME-Version" value="1.0"><cfmailparam name="Content-Transfer-Encoding" value="7bit">#Trim(EmailBody)#</cfmail>

               

              I've had problems in the past where the cfif tag leaves whitespace behind when the conditional logic evaluates as false. Placing the entire tag through the close tag seems to correct it.

               

              Also notice that I wrapped EmailBody with a Trim().

              • 4. Re: Cfmail is inserting a blank space in front of my email body
                dtwineham Level 1

                Awesome, that works.  I vaguely recall seeing that <cfif> quirk at some time years ago it was so far back that it didn't even occur to me this time. I did try something a little different and put the message body in before any of the <cfmailparam> tags and that also does the trick, even though it doesn't look quite right.  At least it will be a little easier to maintain.  Either way, it is gonna need a heck of a comment.

                 

                Didn't need the trim, I was already doing that when I set the value of the variable for the content.

                 

                Thanks again.

                • 5. Re: Cfmail is inserting a blank space in front of my email body
                  duncancumming Level 3

                  This article might help you:

                  http://duncan99.wordpress.com/2009/01/15/cfmail-whitespace-trick/

                   

                  In short this might strip out that leading whitespace for you:

                  <cfmail

                      to = "#Attributes.To#"

                      from = "#Attributes.From#"

                      cc = "#Attributes.CC#"

                      bcc = "#Attributes.BCC#"

                      replyTo = "#Attributes.Reply_To#"

                      subject = "#Attributes.Subject#"

                      type = "text"

                      charset="us-ascii">

                      <cfif "#Attributes.EmailUnsubscribeLink#" NEQ "">

                          <cfmailparam name="List-Unsubscribe" value ="<#Attributes.EmailUnsubscribeLink#>">

                      </cfif>

                      <cfmailparam name="X-Receiver" value="#xReceivers#">

                      <cfmailparam name="MIME-Version" value="1.0">

                      <cfmailparam name="Content-Transfer-Encoding" value="7bit">

                      #Chr(0)##EmailBody##Chr(13)#

                  </cfmail>

                  • 6. Re: Cfmail is inserting a blank space in front of my email body
                    dtwineham Level 1

                    Thanks, Duncan.

                     

                    I had seen and tried that trick but no luck.  It appears that if the body of the email is a string literal or even just starts with one (or from your example, includes one between variables), it preserves any whitespace you might have put in for code formatting.  But if the only thing in the body is a variable name, only the value of that variable is inserted, none of the preceeding whitespace. So

                         #Chr(0)#Hello World#Chr(13)#

                    would be useful for eliminating the leading whitespace, but

                         #Chr(0)##variableName##Chr(13)#

                    doesn't have any effect because the leading whitespace was not included in the first place.