Grails Email Plugin Integration

compile ":mail:1.0.7"
paste the follwoing line
-------------------------------------------------------------------
config.groovy
-------------------------------------------------------------------
grails {
mail {
  host = "smtp.mandrillapp.com" //host name
  port = 587        //your host port number
  username = "support@jad.com" //usernmae
  password = "***************" //password
  props = ["mail.smtp.auth":"true",
     "mail.smtp.socketFactory.port":"587"]
}
}

--------------------------------------------------------------------
BuildConfig.groovy
--------------------------------------------------------------------
//In plugins add mail plugin as shown
plugins {
       // plugins for the build system only
       build ":tomcat:7.0.54"
       // plugins for the compile step
       compile ":scaffolding:2.1.2"
       compile ':cache:1.1.7'
       compile ":asset-pipeline:1.8.11"
 compile ":mail:1.0.7"
       // plugins needed at runtime but not for compilation
       runtime ":hibernate4:4.3.5.4" // or ":hibernate:3.6.10.16"
       runtime ":database-migration:1.4.0"
       runtime ":jquery:1.11.1"
       // Uncomment these to enable additional asset-pipeline capabilities
       //compile ":sass-asset-pipeline:1.7.4"
       //compile ":less-asset-pipeline:1.7.0"
       //compile ":coffee-asset-pipeline:1.7.0"
       //compile ":handlebars-asset-pipeline:1.3.0.3"
    }
---------------------------------------------------------------
email.groovy
---------------------------------------------------------------
package com.practise
class Email {
String name
String emailId
String message

static constraints = {}
}
------------------------------------------------------------
EmailController.groovy
-------------------------------------------------------------
//Here i am using default mailService provided by mail plugin if you want
//you can write your own service class
package com.practise
class EmailController {
def mailService

def index(){}
def send(){
 println "$params"
 mailService.sendMail {
  to "${params.emailId}"
  from "admin@jad.com"
  subject "Welcome To JAD"
  html g.render(template:"myMailTemplate")

  }
 render view :"success"
}
}
To send HTML mail you can use the html method instead of the body method:
sendMail {
 to "fred@g2one.com"
 subject "Hello John"
 html 'Hello World'
}
If your HTML is contained within a GSP template you can use the render tag called as a method (available in controllers and tag libraries):
sendMail {
 to "john@g2one.com"
 subject "Hello John"
 html g.render(template:"myMailTemplate")
}
If you wish to render a mail body from a GSP view from anywhere in your application, including from services or jobs where there may or may not be a current request, you can use the new body method variant (v0.4):
sendMail {
 to "john@g2one.com"
 subject "Hello John"
 body( view:"/emailconfirmation/mail/confirmationRequest",
     plugin:"email-confirmation",
     model:[fromAddress:'bill@microsoft.com'])
}
-------------------------------------------------------------
Index.gsp
-------------------------------------------------------------

----------------------------------------------------------------------------------------
myMailTemplate
------------------------------------------------------------------------------------------


You can create a template as i created and use this template while sending mail
to create mail template you can refer this site
https://www.campaignmonitor.com/?utm_campaign=pubcanvas&utm_source=download&utm_medium=email

No comments:

Post a Comment