Sterling Archer
Sterling Archer

Reputation: 22445

ColdFusion If Statements Causing Spacing in <cfmail>

I'm sending an email out via the <cfmail> in ColdFusion, and I've encountered an issue where these line breaks appear in my email, despite my code not having that many line breaks to do that.

My co-worker told me that multi-line if statements can cause this, so I made them one-lined statements, but the line-breaking persists in my emails!

<cfmail ... >
    <strong><u>Event Details: </u></strong><br />
    <strong>What:</strong><cfif itemsforForm.normalTicket EQ true>Brunch</cfif><br />
    <strong>When:</strong> Sunday, August 17th, 2014<br />
    <cfif(itemsForForm.sponsor GT 0)>11:00am-2:00pm<br /></cfif> <!--- these cause the line breaks --->
    <cfif itemsforForm.normalTicket EQ true>11:30am-1:00pm<br /></cfif>
    <cfif itemsForForm.vipTicket EQ true>11:30am-2:00pm</cfif>
</cfmail>

For every <cfif> in my email, a linebreak appears in the output. Why is this happening?

Upvotes: 1

Views: 366

Answers (2)

Adam Cameron
Adam Cameron

Reputation: 29870

Look more closely at your code:

<cfmail ... >
    <strong><u>Event Details: </u></strong><br />
    <strong>What:</strong><cfif itemsforForm.normalTicket EQ true>Brunch</cfif><br />
    <strong>When:</strong> Sunday, August 17th, 2014<br />
    <cfif(itemsForForm.sponsor GT 0)>11:00am-2:00pm<br /></cfif> <!--- these cause the line breaks --->
    <cfif itemsforForm.normalTicket EQ true>11:30am-1:00pm<br /></cfif>
    <cfif itemsForForm.vipTicket EQ true>11:30am-2:00pm</cfif>
</cfmail>

At the end of each of those lines of code is a line break. That's why they're not one continuous line of code. And if your if conditions are false, you will not get the contents of the <cfif> block, but you'll still get the line break after the </cfif>

To work around this, enable cfoutput-only mode (with <cfsettings>), and then surround just the stuff you want in the mail message with <cfoutput> tags.

Or use CFScript rather than tags. It does not emit anything you don't tell it do.

Upvotes: 7

Sterling Archer
Sterling Archer

Reputation: 22445

The issue here is that in the text editor, the code is indented. It seems that ColdFusion and <cfmail> take those indents into consideration when sending an email. So by un-indenting everything inside the cfmail tag, you get rid of any of those tabbed spaces making their way into your code and injecting linebreaks into seemingly nothing.

Upvotes: 0

Related Questions