Skip to content

Instantly share code, notes, and snippets.

@biniama
Created December 16, 2015 16:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biniama/7ec152189512c318c6fa to your computer and use it in GitHub Desktop.
Save biniama/7ec152189512c318c6fa to your computer and use it in GitHub Desktop.
Grails emailing using templates
/**
* Grails Mail Plugin Configurations
*/
grails {
mail {
host = "smtp.gmail.com"
port = 465
username = "email@gmail.com"
password = "password123"
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.to = 'emailto@gmail.com'
package com.github.biniama
class EmailService {
def grailsApplication
def groovyPageRenderer
MailService mailService
def sendEmail(String senderName, String emailSubject, String messageBody) {
// Assuming Model is defined in as a 'Grails Domain' and has id, name and message fields
List<Model> myList = new ArrayList<Model>()
try {
mailService.sendMail {
to grailsApplication.config.grails.mail.to
subject emailSubject
html groovyPageRenderer.render(view: '/emailTemplate', model: [senderName: senderName, messageBody: messageBody, myList: myList])
}
log.debug("E-mail is sent.")
} catch (Exception ex) {
log.error("Error: ${ex.getMessage()}")
throw ex
}
}
}
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>Email title</title>
</head>
<body>
<p>Dear team members,</p>
<p>This message is sent from <b>${senderName}</b></p>
<p>${messageBody}</p>
<g:each in="${myList}" status="i" var="log">
<p>${myList.id} <b>${myList.name}</b> ${myList.message}</p>
</g:each>
<p>Good bye.</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment