Pages

Thursday, March 5, 2015

Grails mail plugin: email validation (part 1)

Long story short - i couldn't find a easy and understandable tutorial about this stuff, so i will just save the knowledge here.

1) Add to build config:  compile ":mail:1.0.7"

2) Add email account configuration settings in Config.groovy:

grails.mail.default.from="YOURNAME@gmail.com"

grails {
mail {
  host = "smtp.gmail.com"
  port = 465
  username = "your username"  //ex "bob@gmail.com"
  password = "aplication_specific_password_from_gmail"
  props = ["mail.smtp.auth":"true",
  "mail.smtp.socketFactory.port":"465",
  "mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
  "mail.smtp.socketFactory.fallback":"false"]
}

 }

These settings are for gmail. There are also setting for hotmai/live and yahoo. Actually this can be configured for any email service. Just like you would configure outlook for one. 

Now your email account is set up.

3) Now in your controllers you can use such a closure:

sendMail {
to "email adress"
subject "subject"
body "This is the email body"
}

Or you can use the next approach, like i did:

def sendMail(String email, String confirmationCode){
sendMail {
to email
subject "Queit - confirm your email"
Map model = [confirmationCode:confirmationCode]
html g.render(template:"emailConfirmationTemplate", model:model)
}
}

For additional info check the official documentation: http://grails.org/plugin/mail

No comments:

Post a Comment