CFML developers have it easy when it comes to sending email. Set up your mail server in the administrator and use the
<cfmail> tag and you’re all set. Sending email with Grails requires a bit more work, but not much. In this post we’ll take a look at one way to send email from your Grails application.
Grails does not ship with a method to send email ‘out of the box.’ Luckily the Java world has solved the problem of sending email long ago. In fact, ColdFusion itself uses JavaMail behind the scenes to send your messages when you use the
<cfmail> tag. Wouldn’t it be sweet if we could also use JavaMail in our Grails application?
With the Grails mail plugin we can do just that. Let’s take a look at how easy it is to install the plugin and send email from our application.
To install the plugin, run the following command:
grails install-plugin mail
Once installed the mail plugin can now be injected anywhere you need it in your Grails app. In fact, the plugin automatically injects the
sendMail method into all of your controllers to make things even easier. There are a number of config settings for the mail plugin. These settings are added to your
conf/Config.groovy file. By default, the plugin assumes that your mail server is running on port 25 of localhost, but you can configure the plugin to use any mail server (including Gmail) to send mail. In this example I’ve added a simple config to the development environment. Note that I’ve added an
overrideAddress for this environment so that no actual emails are sent while I’m testing the app in development. I’ve also included the host and port (even though they are the same as the defaults) to illustrate how those values are set should you need to change them.
A full exploration of the plugin is a bit beyond the scope of this post. Take a look at the plugin documentation for further information on configuration including how to send attachments.
Now that we’ve got everything set up, let’s take a look at a simple example using “real world” requirements. Systems often have users, and when new users are created we often send them an email to let them know that an account has been created for them. It should certainly go without saying, but please note that in a “real world” application you would not store passwords in plain text. Ever. This is a contrived example. You have been warned.
To get started, set up a simple domain class for our User.
Now scaffold out a controller and views . You can do this via
grails generate-all or by using the appropriate method within your chosen IDE’s graphical interface. Within the generated controller locate the
save() method that was created for you. This method is called whenever the form within the
views/user/create.gsp view is posted. Within the save method we’ll first verify that the userInstance is truly a new instance and if it is a new instance we’ll send the user an email to let them know their account has been created.
Note that if I was not running a mail server and tried to submit the user form at this point I would receive a big fat runtime exception since the plugin attempts to connect to localhost:25 to send the message. For the purpose of this demo I have set up a simple mail server using Apache James. Now, when I submit the new user form the message is properly sent, but since I used an invalid override address the message gets stuck in James’ outgoing folder. If we navigate to that folder and open the ‘FileStreamStore’ file with our favorite text editor we can take a look at the contents of the message which will look something like this:
And that’s all it takes to get started sending email from your Grails application.