Skip to content

Instantly share code, notes, and snippets.

@cortiz
Last active December 14, 2017 18:47
Show Gist options
  • Save cortiz/234bcdca1d7f24b3c3fce0c3703f1b45 to your computer and use it in GitHub Desktop.
Save cortiz/234bcdca1d7f24b3c3fce0c3703f1b45 to your computer and use it in GitHub Desktop.
import scripts.commons.service.MailService
def service = new MailService()
// SMTP server information:
service.host=localhost
service.port= 2525
service.sendEmail("from@email.com", "toMe@email.com", "email1@localhost.loc,email2@localhost", "Sample Email" ,
"/templates/mail/MyEmailTemplate.ftl",[data:"modle", "of":"theTemplate"])
package scripts.commons.service
import org.craftercms.commons.mail.EmailFactory
import org.craftercms.engine.service.context.SiteContext
import java.util.Properties
import org.craftercms.commons.mail.impl.EmailFactoryImpl
import org.craftercms.engine.exception.HttpStatusCodeException
import org.springframework.mail.javamail.JavaMailSenderImpl
class MailService {
def mailSender = new JavaMailSenderImpl()
def emailFactory = new EmailFactoryImpl()
def javaMailProperties = new Properties()
def MailService() {
javaMailProperties["mail.smtp.auth"] = "false"
javaMailProperties["mail.smtp.starttls.enable"] = "false"
mailSender.javaMailProperties = javaMailProperties
emailFactory.mailSender = mailSender
}
def setHost(host) {
mailSender.host = host
}
def setPort(port) {
mailSender.port = Integer.parseInt(port)
}
def setProtocol(protocol) {
mailSender.protocol = protocol
}
def setEncoding(encoding) {
mailSender.defaultEncoding = encoding
}
def sendEmail(from, to, cc, subject, templateName, templateModel) {
emailFactory.freeMarkerConfig = SiteContext.current.freeMarkerConfig.configuration
println("SENDING EMAIL")
if (cc != null) {
emailFactory.getEmail(from, to.split("[,\\s]+"), cc.split("[,\\s]+"), null, subject, templateName, templateModel, true).send()
} else {
emailFactory.getEmail(from, to.split("[,\\s]+"), null, null, subject, templateName, templateModel, true).send()
}
}
def sendEmail(from, to, cc, subject, body) {
println("SENDING EMAIL")
emailFactory.freeMarkerConfig = SiteContext.current.freeMarkerConfig.configuration
if (cc != null) {
emailFactory.getEmail(from, to.split("[,\\s]+"), cc.split("[,\\s]+"), null, subject, body, true).send()
} else {
emailFactory.getEmail(from, to.split("[,\\s]+"), null, null, subject, body, true).send()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment