grails mail plugin

Posted By : Manish Kumar Narang | 30-Sep-2017

In Web applications, sending email is an imtegral feature. In different frameworks, this task is performed differently. In this blog i will talk about it in reference of Grails framework. In grails framework this functionality is provided by the grails mail plugin. Mail can be sent using the @mailService via the @sendMail method. Here is an example -

def sendEmail(){

    mailService.sendMail {
        to "email","**email**"
        from "email"
        subject "Hi"
        body 'Hi'
    }
}

 The plugin is easy to configure and provides some very useful features which can be configured in the config.groovy file. 

grails {
    mail {
      host = "smtp.gmail.com"
      port = 465
      username = "email"
      password = "pwd"
      props = ["mail.smtp.auth":"true",
               "mail.smtp.socketFactory.port":"465",
               "mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
               "mail.smtp.socketFactory.fallback":"false"]
    }
 }
 

I will discuss three of these features -

  • grails.mail.overrideAddress
  • grails.mail.default.from
  • grails.mail.default.to

The email sending functionality in grails framework can be disabled by setting the parameter grails.mail.disabled to be true.

grails.mail.disabled = true

Grails provide an alternative to the disabling mail functionality and i.e to override the email addresses, in which case, the email addresses (from, to, cc and bcc) will be replaced by the email address provided in the parameter grails.mail.overrideAddress. This functionality can be used both the development and testing environment. One can also have seperate email addresses for different environments. By default the override address is a String. Therefore, one can only add one email address. Though by doing some modifications in the inner workings of the plugin, multiple email addresses can be used. That i shall discuss in some other blog.

The grails.mail.default.from is clear from its name itself. If the override functionality is enabled, the from addresses of the email is replaced by the email address provided in grails.mail.default.from parameter. One should keep in mind that this will happen only if the grails.mail.overrideAddress is provided.

The grails.mail.default.to is similar to grails.mail.default.from parameter. The replacement will happen in the to addresses of the email. 

 

 

 

 

About Author

Author Image
Manish Kumar Narang

Manish Narang has skills in software development. He is enthusiastic about web applications and loves to work in a team.

Request for Proposal

Name is required

Comment is required

Sending message..