CFbubu
asked on
CF Function help
Hi,
I am trying to get a function to work but somehow I keep getting an error telling me that 'a variable is undefined'. I would appreciate it if someone could point out what I am doing wrong.
I have placed this function below inside a cfc file:
<cffunction name="textMessage" access="public" returntype="string" hint="Converts an html email message into a nicely formatted with line breaks plain text message">
<cfargument name="string" required="true" type="string">
<cfscript>
var pattern = "<br>";
var CRLF = chr(13) & chr(10);
var message = ReplaceNoCase(arguments.st ring, pattern, CRLF , "ALL");
pattern = "<[^>]*>";
</cfscript>
<cfreturn REReplaceNoCase(message, pattern, "" , "ALL")>
</cffunction>
I than write the code below inside 'somepage.cfm' to call the function above in the cfc file & use it.
<cfsavecontent variable="mailmessage">
<cfoutput>
<html>
<head>
</head>
<body>
<h4>Hello!</h4>
<p>This is an interesting test</p>
</body>
</html>
</cfoutput>
</cfsavecontent>
<cfinvoke component="#APPLICATION.si tepath#" method="textMessage" >
<cfinvokeargument name="string" value="#mailmessage#">
</cfinvoke>
<cfmail
subject="This is a test title"
from="someone@js.com"
to="something@js.com"
type = "HTML"
>
<cfmailpart type="text/plain" charset="utf-8">#textMessa ge(mailmes sage)#</cf mailpart>
<cfmailpart type="text/html" charset="utf-8">#mailmessa ge#</cfmai lpart>
</cfmail>
When I run somepage.cfm, I get the error: Variable TEXTMESSAGE is undefined
Please let me know what I am doing wrong...thanks so very much!
I am trying to get a function to work but somehow I keep getting an error telling me that 'a variable is undefined'. I would appreciate it if someone could point out what I am doing wrong.
I have placed this function below inside a cfc file:
<cffunction name="textMessage" access="public" returntype="string" hint="Converts an html email message into a nicely formatted with line breaks plain text message">
<cfargument name="string" required="true" type="string">
<cfscript>
var pattern = "<br>";
var CRLF = chr(13) & chr(10);
var message = ReplaceNoCase(arguments.st
pattern = "<[^>]*>";
</cfscript>
<cfreturn REReplaceNoCase(message, pattern, "" , "ALL")>
</cffunction>
I than write the code below inside 'somepage.cfm' to call the function above in the cfc file & use it.
<cfsavecontent variable="mailmessage">
<cfoutput>
<html>
<head>
</head>
<body>
<h4>Hello!</h4>
<p>This is an interesting test</p>
</body>
</html>
</cfoutput>
</cfsavecontent>
<cfinvoke component="#APPLICATION.si
<cfinvokeargument name="string" value="#mailmessage#">
</cfinvoke>
<cfmail
subject="This is a test title"
from="someone@js.com"
to="something@js.com"
type = "HTML"
>
<cfmailpart type="text/plain" charset="utf-8">#textMessa
<cfmailpart type="text/html" charset="utf-8">#mailmessa
</cfmail>
When I run somepage.cfm, I get the error: Variable TEXTMESSAGE is undefined
Please let me know what I am doing wrong...thanks so very much!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi _agx_,
Thanks for your response :)
I did write the function in a seperate cfc file, but I did try to access the 'variable' by using the cfinvoke before I used the variable inside the cfmail tag......
I used the invoke below:
<cfinvoke component="#APPLICATION.si tepath#" method="textMessage" >
<cfinvokeargument name="string" value="#mailmessage#">
</cfinvoke>
Was that the wrong way?
Thanks for your clarification!
Thanks for your response :)
I did write the function in a seperate cfc file, but I did try to access the 'variable' by using the cfinvoke before I used the variable inside the cfmail tag......
I used the invoke below:
<cfinvoke component="#APPLICATION.si
<cfinvokeargument name="string" value="#mailmessage#">
</cfinvoke>
Was that the wrong way?
Thanks for your clarification!
EDIT
but I did try to access the 'variable' by using the cfinvoke before I used the variable inside the cfmail tag...... I used the invoke below:
Using cfinvoke is like calling any CF function that returns a value. You have to use the "returnVariable" attribute to capture the result, or it's lost and it'll seem like nothing happened.
The other problem was you can't do this:
#textMessage(milmessage)#
There's no function or variable in your CFM page named textmessage. That's why CF threw an error. When you call the function with CFINVOKE you must capture the result in a variable and use the new variable in your mail tag instead:
<cfinvoke component="path.to.YourCom ponentName "
method="textMessage"
returnVariable="theNewVari able">
<cfinvokeargument name="string" value="#mailmessage#">
</cfinvoke>
....
<cfmailpart type="text/plain" charset="utf-8">#theNewVariable#</cfmailpart>
but I did try to access the 'variable' by using the cfinvoke before I used the variable inside the cfmail tag...... I used the invoke below:
Using cfinvoke is like calling any CF function that returns a value. You have to use the "returnVariable" attribute to capture the result, or it's lost and it'll seem like nothing happened.
The other problem was you can't do this:
#textMessage(milmessage)#
There's no function or variable in your CFM page named textmessage. That's why CF threw an error. When you call the function with CFINVOKE you must capture the result in a variable and use the new variable in your mail tag instead:
<cfinvoke component="path.to.YourCom
method="textMessage"
returnVariable="theNewVari
<cfinvokeargument name="string" value="#mailmessage#">
</cfinvoke>
....
<cfmailpart type="text/plain" charset="utf-8">#theNewVariable#</cfmailpart>
ASKER
Hi _agx_,
AGAIN you are brilliant!
Yes....I added changed the cfinvoke to add the returnvariable:
<cfinvoke component="#APPLICATION.si tepath#" method="textMessage" returnvariable="message" >
<cfinvokeargument name="string" value="#mailmessage#">
</cfinvoke>
And I changed the code below to:
<cfmailpart type="text/plain" charset="utf-8">#message#< /cfmailpar t>
instead of:
<cfmailpart type="text/plain" charset="utf-8">#textMessa ge(mailmes sage)#</cf mailpart>
Thank you for always being so quick with your insights!
Regards.
AGAIN you are brilliant!
Yes....I added changed the cfinvoke to add the returnvariable:
<cfinvoke component="#APPLICATION.si
<cfinvokeargument name="string" value="#mailmessage#">
</cfinvoke>
And I changed the code below to:
<cfmailpart type="text/plain" charset="utf-8">#message#<
instead of:
<cfmailpart type="text/plain" charset="utf-8">#textMessa
Thank you for always being so quick with your insights!
Regards.
ASKER
Again... much thanks!
Welcome :)
<cfmailpart type="text/plain" charset="utf-8">#textMessage(milmessage)#</cfmailpart>
By the way, the reason that doesn't work is because the function exists in a separate cfc. So you can't call it directly. You must access it through the component, either using cfinvoke or createObject.