Provides Mail support to a running Grails application supported by SpringSource
40% of Grails users
Dependency :
compile ":mail:1.0.1"Custom repositories :
mavenRepo "http://download.java.net/maven/2/"
Summary
This plug-in provides a MailService class as well as configuring the necessary beans withinthe Spring ApplicationContext.It also adds a "sendMail" method to all controller classes.
Installation
To install the mail plug-in just run the following command
grails install-plugin mail
Description
Mail Plug-in
The mail plug-in provides e-mail sending capabilities to a Grails application by configuring a Spring MailSender based on sensible defaults.There is a screencast showing how to use the basic features of the plugin (v0.4)Usage
The mail plug-in provides a MailService that can be used anywhere in your Grails application. The MailService provides a single method called sendMail that takes a closure. In addition the sendMail method is injected into all controllers to simplify access.An example of the sendMail method can be seen below:sendMail {
to "fred@g2one.com"
subject "Hello Fred"
body 'How are you?'
}mailService.sendMail {
to "fred@g2one.com","ginger@g2one.com"
from "john@g2one.com"
cc "marge@g2one.com", "ed@g2one.com"
bcc "joe@g2one.com"
subject "Hello John"
body 'this is some text'
} html method instead of the body method:sendMail {
to "fred@g2one.com"
subject "Hello John"
html '<b>Hello</b> World'
}sendMail {
to "john@g2one.com"
subject "Hello John"
html g.render(template:"myMailTemplate")
}sendMail {
to "john@g2one.com"
subject "Hello John"
body( view:"/emailconfirmation/mail/confirmationRequest",
plugin:"email-confirmation",
model:[fromAddress:'bill@microsoft.com'])
}<%@ page contentType="text/html"%>Multiple recipients
You can send mail to multiple recipients (in either of 'to', 'cc' or 'bcc') at once.sendMail {
to "fred@g2one.com","ginger@g2one.com"
subject "Hello to mutliple recipients"
body "Hello Fred! Hello Ginger!"
}List for storing the recipients. You'll have to invoke toArray when providing it to the builder, like this:sendMail {
to issue.watchers.email.toArray()
subject "The issue you watch has been updated"
body "Hello Watcher!"
}toArray , Groovy will convert the list (even a list with a single entry) to a String (the same way it does on the interactive console). The result will be something that is not a valid email address and you'll face javax.mail.internet.AddressException .Attachments
Since version 0.9 attachment support has been improved. It is possible to have both, email body and multiple attachments. In order to activate multipart support, the 'multipart true' must be the first element in the closure passed to the sendMail method, e.g.:sendMail {
multipart true
to issue.watchers.email.toArray()
subject "The issue you watch has been updated"
body "Hello Watcher!"
attachBytes "Some-File-Name.xml", "text/xml", contentOrder.getBytes("UTF-8")
//To get started quickly, try the following
//attachBytes './web-app/images/grails_logo.jpg','image/jpg', new File('./web-app/images/grails_logo.jpg').readBytes()
}Configuration
By default the plugin assumes an unsecured mail server configured at localhost on port 25. However you can change this via the grails-app/Config.groovy file. For example here is how you would configure the default sender to send with a Gmail account:grails {
mail {
host = "smtp.gmail.com"
port = 465
username = "youracount@gmail.com"
password = "yourpassword"
props = ["mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"465",
"mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback":"false"] }
}grails {
mail {
host = "smtp.live.com"
port = 587
username = "youracount@live.com"
password = "yourpassword"
props = ["mail.smtp.starttls.enable":"true",
"mail.smtp.port":"587"] }
}grails {
mail {
host = "smtp.correo.yahoo.es"
port = 465
username = "myuser"
password = "mypassword"
props = [ "mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"465",
"mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback":"false" ]
}
}jndiName setting:grails.mail.jndiName = "myMailSession"// Since 0.2-SNAPSHOT in SVN grails.mail.default.from="server@yourhost.com"
grails.mail.disabled=truegrails.mail.overrideAddress="test@address.com"TODO
- Attachment support
- Support for multiple parts
- Support for MailSender artefact and sendMail(with: 'defaultSMTP') {..} for multiple smtp servers
- Inline images
- Send mails in an async process
- etc.
Troubleshooting
- If an installation failed because of unresolved dependencies ...
(like "javax.activation#activation;1.1: not found", or "org.springframework#spring-test;latest.release: not found"),
… check if mavenCentral() is uncommented in your BuildConfig.groovy - If you face
javax.mail.internet.AddressExceptionerrors referring to "Illegal address in string" or "Missing ']' in string", it could be that you try to send to aListof recipients, which is not supported by the plugin. See 'Multiple recipients' above for information how to deal with that. - If you call sendMail in a service class rendering a gsp template, you may realize that the plugin is rendering the template as text/plain instead of html. To solve this issue you have to put in your gsp <%@ page contentType="text/html" %> or <%@ page contentType="text/xhtml" %> without setting the encoding