is there a way to add some spacing to cfmail type=text emails in Coldfusion when using variables only?

699 Views Asked by At

I'm a little in awe on how my first Cfmails are looking.

Problem is, I'm using variables for both text and content and I would still like to have some sort of spacing.

For example, if I have:

<cfprocessingdirective suppresswhitespace="No">
    <cfmail
        TO="#Local.User.email#"
        FROM="..."
        SERVER="..."
        USERNAME="..."
        PASSWORD="..."
        WRAPTEXT="80"
        SUBJECT="#tx_automailer_register_subject# - #Local.User.Comp#">

#tx_automailer_default_hello#

#tx_automailer_register_info#

#tx_automailer_register_iln#:   #Local.User.iln#
#tx_firma#:                     #Local.User.firma#
#tx_ansprechpartner#:           #Local.User.ansprechpartner#
#tx_adresse#:                   #Local.User.adresse#
#tx_plz#:                       #Local.User.plz#
#tx_ort#:                       #Local.User.ort#
...

The only place this looks nice is my cfc :-) In the mail itself everything is going bazooka.

Question:
Is there a way to space this? I have also tried to space according to length of variables, but this also does not really do any good and I'm not really keen on doing math for this...

Thanks for help!

3

There are 3 best solutions below

1
On BEST ANSWER

If you're set on plaintext email and are confident that the recipient will be using a fixed-width font, you can use lJustify() to align your text and pad with spaces.

Left justifies characters in a string of a specified length.

#lJustify(tx_automailer_register_iln & ":",32)# #lJustify(Local.User.iln,25)#
#lJustify(tx_firma & ":",32)# #lJustify(Local.User.firma,25)#
#lJustify(tx_ansprechpartner & ":",32)# #lJustify(Local.User.ansprechpartner,25)#
#lJustify(tx_adresse & ":",32)# #lJustify(Local.User.adresse,25)#
#lJustify(tx_plz & ":",32)# #lJustify(Local.User.plz,25)#
#lJustify(tx_ort & ":",32)# #lJustify(Local.User.ort,25)#
1
On

The only option may be to post process the content. Build up the pretty content in a cfsavecontent, then run through cleanup function.

<cfprocessingdirective suppresswhitespace="No">
<cfsavecontent variable="message">
    #tx_automailer_default_hello#

    #tx_automailer_register_info#

    #tx_automailer_register_iln#:   #Local.User.iln#
    #tx_firma#:                     #Local.User.firma#
    #tx_ansprechpartner#:           #Local.User.ansprechpartner#
    #tx_adresse#:                   #Local.User.adresse#
    #tx_plz#:                       #Local.User.plz#
    #tx_ort#:                       #Local.User.ort#
</cfsavecontent>
<cfmail
    TO="#Local.User.email#"
    FROM="..."
    SERVER="..."
    USERNAME="..."
    PASSWORD="..."
    WRAPTEXT="80"
    SUBJECT="#tx_automailer_register_subject# - #Local.User.Comp#"
>#cleanupTextMessage(message)#</cfmail>

<cffunction name="cleanupTextMessage" output="false">
    <cfargument name="content" />
    <!--- remove whitespace at beginning of each line --->
    <cfset arguments.content = reReplace(arguments.content, "^\s+", "", "all") />
    <!--- replace any multiple whitespace characters with one space --->
    <cfset arguments.content = reReplace(arguments.content, "\s+", " ", "all") />
    <cfreturn arguments.content />
</cffunction>

You might actually be able to nest the cfsavecontent inside cfmail, or create a custom tag that does savecontent and function actions.

Note: I was answering under the assumption the question was "how to make code look good without affecting the resulting text message". If you were trying to do something different with the resulting text output let me know.

1
On

You can use HTML To do it by adding the TYPE="html" to your cfmail attributes. Then put in a "pre" tag if you want that sysprint type look. as in

<pre>
#tx_automailer_default_hello#

#tx_automailer_register_info# 
....

</pre>

Or you could add a table as in:

<table
<tr>
<td>#tx_automailer_default_hello#</td>
</tr>
<tr><td>

#tx_automailer_register_info#
</td>

If you want to stick with plain text you need to make sure you have tabs/spaces counted correctly and that none of your lines is longer than 80 chars (or they will wrap..without a beat too).