Created
December 16, 2015 16:58
-
-
Save biniama/7ec152189512c318c6fa to your computer and use it in GitHub Desktop.
Grails emailing using templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ 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