Coldfusion 9 read .htm file and loop over data? - coldfusion

I have to loop over .htm files and search for specific fields in each file. Once field is detected I need to pull the data for that field. Here is example of the data:
Name: John, Miller
and code in htm file looks like this:
<tr>
<td><u>Name</u></td>
</tr>
<tr>
<td>John, Mille</td>
</tr>
I tried to use cffile read:
<cffile action="read" file="\files\someFile.htm" variable="myData">
<cfoutput>#myData#</cfoutput>
Code above outputted .htm file on the screen. Is there a way to loop over the data in .htm file?
I have tried this with the Coldfusion 9:
<cfset myFile = "\files\someFile.htm">
<cfloop file="#myFile#" index="i" item="line">
<cfoutput>
#i#:#line#
</cfoutput>
</cfloop>
I got this error:
Attribute validation error for tag CFLOOP.
It has an invalid attribute combination: file,index,item. Possible combinations are:
Required attributes: 'file,index'. Optional attributes: 'charset,from,to'.
Required attributes: 'index,list'. Optional attributes: 'delimiters'.
Required attributes: 'group'. Optional attributes: 'endrow,groupcasesensitive,startrow'.
Required attributes: 'group,query'. Optional attributes: 'endrow,groupcasesensitive,startrow'.
Required attributes: 'query'. Optional attributes: 'endrow,startrow'.
Required attributes: None. Optional attributes: None.
Required attributes: 'array,index'. Optional attributes: None.
Required attributes: 'characters,file,index'. Optional attributes: 'charset'.
Required attributes: 'collection,item'. Optional attributes: None.
Required attributes: 'condition'. Optional attributes: None.
Required attributes: 'from,index,to'. Optional attributes: 'step'.
If anyone knows how to loop over the data in htm file please let me know. Thank you.

I think the problem is CF9 not supporting the ITEM attribute. ITEM is fairly new.
No troubles. You can parse this thing the old fashioned way. Load the file as a list, using the line breaks as the delimiter.
<cfloop index="line" list="#myFile#" delimiters="#chr(10)##chr(13)#">
<cfoutput>
#line#
</cfoutput>
</cfloop>

Related

Control MIME structure of emails produced with cfmail

On this question I asked about what the structure of the email should be. This question is about how to use cfmail (and cfmailpart, cfmailparam, etc.) to produce the correct structure.
The desired structure is:
multipart/mixed
multipart/alternative
text/plain
text/html
image/jpeg
application/pdf
The code I've got currently:
<cfmail from='"No Reply" <noreply#example.com>' subject="Test 123" to="my_outlook_address#example.com,my_gmail_address#gmail.com">
<!--- Some code to get a query of attachment content here... --->
<cfloop query="qAttachments">
<!---
Some code to get the attachment file data here and put it in a variable named `myfile`...
myfile structure:
{
fileName: <string>,
fileContent: <base64 encoded file content>,
mimeType: <image/jpeg for the one, application/pdf for the other>
}
--->
<cfmailparam disposition="attachment" contentID="#myfile.fileName#" content="#myfile.fileContent#" file="#myfile.fileName#" type="#myfile.mimeType#" />
</cfloop>
<cfmailpart type="plain">
My plain text
</cfmailpart>
<cfmailpart type="html">
<strong>My fancypants text</strong>
</cfmailpart>
</cfmail>
However, this produces this structure:
multipart/mixed
multipart/alternative
text/plain
multipart/related
text/html
image/jpeg
application/pdf
I've tried code like this:
<cfmail from='"No Reply" <noreply#example.com>' subject="Test 123" to="my_outlook_address#example.com,my_gmail_address#gmail.com">
<!--- Some code to get a query of attachment content here... --->
<cfloop query="qAttachments">
<cfmailparam disposition="attachment" contentID="#myfile.fileName#" content="#myfile.fileContent#" file="#myfile.fileName#" type="#myfile.mimeType#" />
</cfloop>
<cfmailpart type="multipart/alternative">
<cfmailpart type="plain">
My plain text
</cfmailpart>
<cfmailpart type="html">
<strong>My fancypants text</strong>
</cfmailpart>
</cfmailpart>
</cfmail>
But then it just goes to the undelivered email list in cfadmin.
With both versions of the code I tried for values of the type attribute on the cfmail tag itself:
plain
html
multipart/mixed
multipart/alternative
to no avail.
How do I achieve the desired MIME structure in ColdFusion?
My approach I landed on may not be ideal, but it works in all 3 mail clients I was targeting. What I ended up doing was this:
for any image attachments, I would include a contentID attribute on the cfmailparam tag and include an <img src="cid:..."> with the contentID value
for all other attachments, I omit the contentID attribute on the cfmailparam tag
This has the end result that all images are presented inline in the message body, and all other files are displayed as regular file attachments.
Based on the discussion by who I assume is a developer on the CF team here https://tracker.adobe.com/#/view/CF-4166939 I'm under the impression that the MIME header structure is controlled by ColdFusion and isn't directly manageable by ColdFusion developers. Unfortunate, but at least I have something of a workaround. Hopefully this will help someone.

Populating text input in Coldfusion

I'm trying to do the following (amongst many other attempts), and have validated that #myVar# holds data and is current in the scope, but nothing works. Any ideas how to populate a text input in Coldfusion? Many thanks in advance!
<cfoutput>#myVar#</cfoutput>
<script>document.getElementById("text1").value = #myVar# </script>
The easiest way is with the value attribute of an input tag.
<form>
<input name "fred" value="flinstone">
etc
If you want to populate it dynamically, use cfoutput.
<cfset myVar = "flinstone">
<cfoutput>
<form>
<input name "fred" value="#myVar#">
etc
or is that not the question?
Your closing </cfoutput> is too early, where you're trying to output the value inside your javascript should also be wrapped in the cfoutput.
<cfoutput>
#myVar#
<script>document.getElementById("text1").value = "#myVar#";</script>
</cfoutput>
Is your value a string or an integer? If its a string you will need wrap the value in quotes.
Additionally you will want to use the jsStringFormat() function to ensure any quotes are escaped.
<script>document.getElementById("text1").value = "#jsStringFormat(myVar)#"; </script>

cffile creating blank lines when storing a table

I've got a cfsavecontent tag that saves a table. Later I use cffile to write the saved content to a file. When I look at that file, I see that there many blank lines inserted after <td> tags in the table; and few blank lines inserted after </tr> tags. (Although it doesn't do that where the code says <tr><td> </td></tr> all on one line.)
Presently I have a file which contains two of those tables. The tables are generated in a loop, and the output file is created with cffile append. This file has 915 lines in it of which maybe 30 are non-blank. All my subsequent code works correctly, but this is just test data. In the real world I could have 1000 or more tables, and I am concerned about the file size.
The code:
<cfset head1 = 'from = "moxware" '>
<cfset head2 = 'to = "#hrep.PersonEmail#" '>
<cfset head3 = 'replyto = "#replyto#" '>
<cfset head4 = 'subject = "#subject#" '>
<cfset head5 = 'type = "html" '>
<cfsavecontent variable = "abc">
<cfoutput>
#head1#
#head2#
#head3#
#head4#
#head5# >
#xyz#
</cfoutput>
</cfsavecontent>
<cffile action = "append"
file = "/var/www/reports/moxrep/#reportout#.cfm"
output = "<cfmail"
mode = "777" >
<cffile action = "append"
file = "/var/www/reports/moxrep/#reportout#.cfm"
output = "#abc#"
mode = "777">
<cffile action = "append"
file = "/var/www/reports/moxrep/#reportout#.cfm"
output = "</cfmail>"
mode = "777" >
Re the xyz, I am reading it in from a file:
<cffile action = "read"
file = "/var/www/reports/moxrep/#reportname#.cfm"
variable = "xyz">
and the file looks like this:
<link rel="stylesheet" href="sample.css">
<link rel="stylesheet" type = "text/css" href ="betty.css"/>
<p style="margin-left:40px"><span style="font-size:14px"><span style="font- family:georgia,serif">Dear Customer,</span></span></p>
We were so pleased that you have signed up for one of our programs. Apparently you live in the city of {{1. Additionally we observe that your were referred to us by {{2. Below please find a listing of what you signed up for.</span></span></p>
<p style="margin-left:40px"><span style="font-size:14px"><span style="font- family:georgia,serif">{{r</span></span></p>
<p style="margin-left:40px"><span style="font-size:14px"><span style="font-family:georgia,serif">Sincerely Yours,</span></span></p>
<p style="margin-left:40px"><span style="font-size:14px"><span style="font-family:georgia,serif">John Jones<br />
President<br />
XYZ Corporation</span></span></p>
The file was created by a code generator, not me, so it's a bit cumbersome. Later in the code I replace everything starting with {{ ; in particular {{r gets replaced with a table, and that is where the additional space is coming from.
The append itself is not inserting any extra lines.
Does anyone know what is causing these extra blank lines in the file; and how to get rid of them?
Betty, typically you need to do this carefully if you want to avoid whitespace. In particular the use of cfoutput with a query will generate lines. So this code:
<table>
<cfoutput query="myquery">
<tr><td>#col1#</td><td>#col2#</td></tr>
</cfoutput>
</table>
will produce extra lines... but if you do this:
<cfsetting enablecfoutputonly="yes">
<cfoutput><table></cfoutput>
<cfloop query="myquery"><cfoutput><tr><td>#col1#</td><td>#col2#</td></tr></cfoutput></cfloop>
<cfoutput></table></cfoutput>
You would carefully control exactly what is allowed to be appended to the buffer. enableoutputonly does exactly what it says... it does not allow anything to "go to the buffer" unless it is enclosed in cfoutputs.
Hope this helps. As cameron says you should paste code for questions like this. That's where the answer will typically reside.
(you might also need to experiment with the "addnewline" attribute of cffile - depending on whether your problem is a line at the END of your file).
To answer your question regarding adding cfsetting... in your case you are writing CF code to a file that is then executed later (which by the way is not a great idea usually :). So in your first Append statement:
<cffile action = "append"
file = "/var/www/reports/moxrep/#reportout#.cfm"
output = "<cfmail"
mode = "777" >
Change the "output" to be:
<cffile action = "append"
file = "/var/www/reports/moxrep/#reportout#.cfm"
output = "<cfsetting enablecfoutputonly=""yes""/> <cfmail"
mode = "777" >
But Betty - you will still need to remove the line breaks from your cfsavecontent (if that's where your whitespace is coming from) because they actually ARE inside of a cfoutput. Also, your code that creates the table you are inserting might be at fault - and it is not listed here.
Finally, since this is cfmail take a look at this post regarding line breaks that may or may not have some bearing - but at least gives you one more piece of information :)
http://www.coldfusionmuse.com/index.cfm/2006/4/12/cfmail.linebreak
You may consider using cfprocessingdirective around your cfsavecontent. There is a setting in CF administrator that universally either compresses or retains unnecessary whitespace, "Enable Whitespace Management" - http://help.adobe.com/en_US/ColdFusion/9.0/Admin/WSc3ff6d0ea77859461172e0811cbf3638e6-7ffc.html . Using the suppressWhiteSpace attribute of cfprocessingdirective, you can override this setting for a particular page or part of a page. So in your case:
<cfprocessingdirective suppressWhiteSpace="true">
<cfsavecontent variable="myvar">....
...
...
</cfsavecontent>
</cfprocessingdirective>
may help. Likewise, to ensure the retention of whitespace when building text emails, you'd use suppressWhiteSpace="false".
Cheers,

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

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!
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.
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).
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)#

How to determine if a full name has a space in it?

I have a field that a user can input first and last name to fill out my form. Sometimes, users put on their first name and that results in empty fields in my database. PLEASE keep in mind that I cannot change this method completely because this form is part of a bigger project and it is being used by other websites of my company.
This is the part of the code that i need the validation around it. I already have a validation that ensures that the filed is not empty but I need on more to ensure that the field has two items in it separated by space.
<input name="fullname" class="fullname" type="text" value="#fullname#" maxlength="150"/>
<cfif fullname eq '' and check2 eq 'check2'>
<br /><span style="color:red">*you must enter your full name</span></cfif>
The check2 eq 'check2' is checking if the form was submitted already to ensure a user submitting their data twice.
I thought of using regular expressions to do that but unfortunately I am not very familiar with how to use regx in CF9 and the documentation online through me off a bit.
I was also thinking to use "Find" or "FindOneOF", any thoughts on that?
Also, I am trying to avoid using JQ,JS etc, so please try to keep your suggestions based on CF code IF possible.
Any help or different suggestions on how to tackle this issue will be very appreciated.
No regex is needed for this. A slightly simpler solution:
<cfset form.fullname = "Dave " />
<cfif listLen(form.fullname," ") GT 1> <!--- space-delimited list, no need for trimming or anything --->
<!--- name has more than one 'piece' -- is good --->
<cfelse>
<!--- name has only one 'piece' -- bad --->
</cfif>
You could do something like this for server side validation:
<cfscript>
TheString = "ronger ddd";
TheString = trim(TheString); // get rid of beginning and ending spaces
SpaceAt = reFind(" ", TheString); // find the index of a space
// no space found -- one word
if (SpaceAt == 0) {
FullNameHasSpace = false;
// at least one space was found -- more than one word
} else {
FullNameHasSpace = true;
}
</cfscript>
<cfoutput>
<input type="input" value="#TheString#">
<cfif FullNameHasSpace eq true>
<p>found space at position #SpaceAt#</p>
<p>Your data is good.</p>
<cfelse>
<p>Did not find a space.</p>
<p>Your data is bad.</p>
</cfif>
</cfoutput>