aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/services/MailService.scala
blob: 53f897a627b5538e27a3868d82578536708b5d60 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package xyz.driver.pdsuidomain.services

import java.io.{InputStream, StringReader, StringWriter}

import xyz.driver.pdsuidomain.services.MailService.Template
import com.github.mustachejava.DefaultMustacheFactory
import com.twitter.mustache.ScalaObjectHandler

import scala.io.Source

object MailService {

  trait Template {
    val subject: String
    def parameters: Map[String, Any]
    def filename: String
    val contentType: String = "text/html"

    protected val factory = new DefaultMustacheFactory()
    factory.setObjectHandler(new ScalaObjectHandler)

    protected def inputStream: InputStream = getClass.getClassLoader.getResourceAsStream(filename)
    protected def templateContent: String  = Source.fromInputStream(inputStream).getLines().mkString

    def content: String = {
      val template = factory.compile(new StringReader(templateContent), filename)
      val writer   = new StringWriter
      template
        .execute(writer, parameters)
        .close()
      writer.toString
    }
  }
}

trait MailService {

  def sendTo(email: String, template: Template): Boolean
}