Tuesday, November 10, 2009

ColdFusion 9 Mail in cfscript

I ran into an issue today trying to send an email using script syntax. My code was pretty simple:


var email = new Mail();
email.setTo("joe@example.com");
email.setFrom("joe@example.com");
email.setSubject("Test Email");
email.setType("html");
email.send(body="Hello, world");


However, when I tried running the code, I got the following error:

Could not find the ColdFusion component or interface Mail.

While it's pretty obvious now what the problem is now, at first I was pretty confused. I figured since all the tags were converted to handle script syntax, everything should just work. I checked the ColdFusion 9 documentation and everything looked fine. After about 10 minutes I finally figured it out.

When installing ColdFusion, one of the first things I do is clean up ColdFusion Administrator by removing the default datasources and custom tag paths. However, when Adobe added script support for a couple tags (ftp, http, mail, pdf, query, storedproc), they chose to implement the functions as objects using CFCs. When I deleted the default custom tag path, ColdFusion was no longer able to find the Mail.cfc, since it was relying on the custom tag path.

If you go to cf_root\servers\cfusion\cfusion-ear\cfusion-war\WEB-INF\cfusion\CustomTags\com\adobe\coldfusion\, you should see all the tags implemented as CFCs. Kinda interesting.

On a random note, although it was said that <cfsavecontent /> would not be implemented in script syntax, apparently it was. You can see it in action if you view the examples on the documentation for using mail in script.


savecontent variable="mailBody"{
WriteOutput("This message was sent by...");
}

8 comments:

  1. While this is news to me, it IS documented. Page 107 has a table of tags/script counterparts and this is listed there.

    ReplyDelete
  2. I'm surprised that the mail cfc doesn't get extended from the component cfc that gets called with every cfc...maybe its just cfcomponent

    ReplyDelete
  3. I think these new cfscript "features" are pretty weak. Making your own components was a trivial task:
    <cffunction name="cfinclude" returntype="any">
    <cfargument name="loc" required="true" />
    <cfinclude template="#arguments.loc#" />
    </cffunction>

    OR use a collection

    <cffunction name="cffile" returntype="any">
    <cfargument name="col" required="true" type="struct" />
    <cffile attributecollection="#arguments.col#" />
    </cffunction>

    ReplyDelete
  4. Thank you, you saved my day with this find. My dev machine worked great, but on the production box I had to add a custom tag path of C:\ColdFusion9\CustomTags\com\adobe\coldfusion.

    ReplyDelete
  5. ugh...just ran into the same issue, nice post. I did work around by making a helper cfc with a mail method in tag-script.

    ReplyDelete
  6. Thanks, this helped

    ReplyDelete
  7. You know that creating your own functions are basically UDF's. Good way to work around certain coding errors.

    ReplyDelete

Note: Only a member of this blog may post a comment.