Grails Mail Plugin with DB Driven configuration
We have been coming across a certain use case in a couple of projects that the Email credentials/settings used for sending email should be different based on the from address. This is because the applications act as software services that are used by different organizations, which operate in the same business. And each needed their own customized emails and use their email credentials for sending of mails.
After searching a lot for such a solution, we found that such a use case has arisen for quite a few developers.
Another demand was that a mail service provider like GMail be used to send emails instead of using a mail server like Postfix of Sendmail.
What we came up was a modified version of the grails mail plugin, which works with DB driven mail configurations. It defaults to Config.groovy it it fails to fetch the MailConfig domain object based on the from address, which we usually in the sendMail() method. The plugin makes use of a customMailSender bean, which is actually a copy of the mailSender bean which gets built from Config.groovy, but can be modified in a thread safe environment with values fetched from the DB.
The plugin is still in its infancy, but you can download the plugin and its source code here. You can read the documentation here.
Hope you find this useful
Vivek
Hi Lari,
This really looks good! Thanks for the suggestion. Will definitely try it out. 🙂
— Vivek
Apache Camel http://camel.apache.org/mail.html is another possibility .
(example from :
Map map = new HashMap();
map.put(“To”, “davsclaus@apache.org”);
map.put(“From”, “jstrachan@apache.org”);
map.put(“Subject”, “Camel rocks”);
String body = “Hello Claus.nYes it does.nnRegards James.”;
template.sendBodyAndHeaders(“smtp://davsclaus@apache.org”, body, map);
You could also use in-memory queues (seda) to send in the background. There are tons of possibilities in Camel.
Usage in Grails:
grails install-plugin routing (http://www.grails.org/plugin/routing)
BuildConfig.groovy changes for camel-mail support:
uncomment
mavenRepo “http://download.java.net/maven/2/”
in repositories.
Add this to dependencies:
runtime(“org.apache.camel:camel-mail:2.5.0”, “org.apache.camel:camel-test:2.5.0”) {
excludes ‘spring-aop’, ‘spring-beans’, ‘spring-core’, ‘spring-expression’, ‘spring-asm’, ‘spring-tx’, ‘spring-context’, ‘spring-context-support’
}
camel-test gives you TDD/Junit support.