Email from Velocity

Published on September 1, 2010 by in Development, Plugins, Viewtools

2

The more applications I build in dotCMS, the more I find myself trying to accomplish them with just Velocity coding. This saves me from having to create a plugin and make sure it is maintained through my systems. However, lately I have needed to write a lot of Form Handling with dotCMS. Not your basic, send an form to an email kind of thing either. The stuff I have had to work on is more on piping the form entry through some logic and then starting some kind of workflow. I have been fairly successful writing these forms up and handling them using the #getSQLResults macro. The one piece that I have not been able to do with just velocity is send a notification or confirmation email.

The scenario is that we have a form, this form needs to be processed in some manner and then inserted/updated into a database table, and then an email needs sent to a specific person that there is a new or updated entry. This is a very common scenario. Currently to do this in dotCMS without Java Coding of some sort would be a challenge, if not impossible. DotCMS does have some of it’s own form handlers, but I find that in many cases I need to perform some logic with the data before passing it on. The new Form Handler in dotCMS 1.9 has a lot of promise, but unless you plan on paying for an Enterprise License, I am going to consider that option out. That leaves me with building my own custom form handler.

Staying with the idea that I want a purely Velocity Form Handler; I can perform my logic, and use the SQL macro to insert or update my database (Note: you cannot insert or update to the dotCMS tables, you will need to have your own tables or an external database). The only thing I cannot do currently is send an email. Unfortunately the answer is that I have to bring this functionality to Velocity in the form a ViewTool. It’s going to take some Java, but my hope is that for anyone else looking to do similar things will be able to deploy my simple plugin and get moving.

The code for sending an email with Java can be found easily enough from Google. It is a fairly simple matter to take this code and turn it into a usable viewtool. Here is the plugin in all it’s glory:

[ Download the zip here ]

To use this plugin simply download the zip file and extract it to your plugins directory. You will need to edit the conf\plugin.properties file and change your mail stmp server setting. Stop your dotCMS instance. Then in a command prompt, in the base directory for your dotCMS install execute the following command:

ant clean-plugins build-plugins deploy-plugins

Once that is done, you can restart your dotCMS instance and you are ready to send emails from velocity. Here is a quick code example to show you how.

#set($to   = "cfalzone@edinboro.edu")
#set($from = "test@edinboro.edu")
#set($subj = "Testing Mailer")
#set($body = "This is a test")
#set($html = "<h1>This is a <em>Test</em></h1>")

#set($err = $mailer.sendEmail($to, $from, "$subj (TEXT)", $body, false))
#if($UtilMethods.isSet($err))
  <p> TEXT Error:  $err </p>
#else
  <p> TEXT sent ok </p>
#end

#set($err = $mailer.sendEmail($to, $from, "$subj (HTML)", $html, true))
#if($UtilMethods.isSet($err))
  <p> HTML Error:  $err </p>
#else
  <p> HTML sent ok </p>
#end

As you can see the simpleMailer plugin will allow you to send either Plain Text or simple HTML messages. I have not implemented file attachments. One thing to note is, due to the way dotCMS works, when you hit a page in the editor, it loads it once for Edit Mode, once for Preview Mode, and once for Live Mode. This means if you put this code in a page you will get the emails 3 times each.

I hope it helps someone out there trying to do the same thing as I am. I think this is pretty essential tooling for dotCMS, so hopefully I can convince the folks at dotCMS to include this viewtool with the standard set in the future. There are of course many improvements you could make to this. You could add the ability to add attachments. You could add the ability to use a dotCMS page or template as a wrapper for your email.


Photo Credit: AttributionNo Derivative Works Some rights reserved by soapbeard


2 Responses to “Email from Velocity”

  1. Stephen Bell says:

    If you didn’t want to get the email three times, maybe you could just wrap your code in:

    #if($EDIT_MODE==true)
    …..
    #end

    Then it would only fire from the one view.

Trackbacks/Pingbacks

  1. Tweets that mention » Email from Velocity » Learn dotCMS -- Topsy.com - September 1, 2010

    […] This post was mentioned on Twitter by Michael Fienen, dotCMS and Jason Tesser, Christopher Falzone. Christopher Falzone said: Just whipped up a new #dotcms plugin that allows you to send emails from velocity code: http://bit.ly/brNOoX […]

Leave a Reply