Novamail
Description
The Novamail plug-in provides e-mail sending and receiving capabilities to a Grails application. It is also capable of sending emails asynchronously by using a scheduled Job.
I recommend you add the following to application.groovy config file. Please create the file if it doesn't exist. Configuration
Add your email provider properties to grails configuration file: Example Assuming you want to add config for a gmail account for 'john@gmail.com' then add the following to your grails config file.
novamail.hostProps = [
["host":'imap.gmail.com'],
["mail.imap.host":"imap.gmail.com"],
["mail.store.protocol": "imaps"],
["mail.imap.socketFactory.class": "javax.net.ssl.SSLSocketFactory"],
["mail.imap.socketFactory.fallback": "false"],
["mail.imaps.partialfetch":"false"],
["mail.mime.address.strict": "false"],
["mail.smtp.starttls.enable": "true"],
["mail.smtp.host": "smtp.gmail.com"],
["mail.smtp.auth": "true"],
["mail.smtp.socketFactory.port": "465"],
["mail.smtp.socketFactory.class": "javax.net.ssl.SSLSocketFactory"],
["mail.smtp.socketFactory.fallback": "false"]
]
novamail{
hostname= System.getenv("CS_HOSTNAME")
username= System.getenv("CS_USERNAME")
password= System.getenv("CS_PASSWORD")
store= System.getenv("CS_STORE")
}
Avoid having passwords in your code. Store them as Environment variables.
Novamail will try to use predefined host props for some popular email providers if you do not provide hostProps Side note
Usage
Inject messagingService into your class
def messagingService
messagingService is a Grails service that provides a single method called sendEmail that takes parameters. Please note that 'sendEmail()' is overloaded 'see http://en.wikipedia.org/wiki/Function_overloading' and can take various variations of parameters.
One simple form is:
sendEmail(Map map)
Where ......
map contains parameters... map.to: Email recipient eg recipient@gmail.com
map.subject: "Your email subject"
map.body: "The body of your message"
Example
An example usage can be seen below.
Class YourController{
def messagingService
...
def yourMethod(){
def map = [to:"recipient@gmail.com",subject:"Email subject",body:"email body"]
messagingService.sendEmail(map)
}
}
Second form
Requirements
To use the messagingService
with mapped parameters, you need to declare a
map with the required variables. These are,
hostname, username, password,
from, to, subject, body, html, attachments, hostProps
.
- hostname : String
- username: Stiring
- password : String
- from : String
- to : String
- subject : String
- body : String
- html : boolean
- attachments : List
html
is boolean that defaults to true
,
attachments
is a List of type File (for file attachments) and is optional,
and
hostProps
is a map of host properties (see above).
If hostname, username, password, from, hostProps
have been set in the
Config.groovy file, they do not have to be added to your map parameter.
html
defaults to true
so that can be
omitted as well except when set explicitly (your choice).
Example Usage
Class MyController {
def messagingService
def myMethod() {
...
def map = [username:"john@doe.com", password:"john_password", from:"JOHN Doe<john@doe.com>", to: "recepeitn@gmail.com", subject: "Hello there!", body: "Just to test out awesome Novamail"]
messagingService.sendEmail(map) // Call the messagingService sendEmail method passing in the map
}
}