Skip to content

Instantly share code, notes, and snippets.

@hdeshev
Created March 6, 2012 20:20
Show Gist options
  • Save hdeshev/1988753 to your computer and use it in GitHub Desktop.
Save hdeshev/1988753 to your computer and use it in GitHub Desktop.
My own mailer wannabe inspired by Lift mailers
object AuthMailer extends Mailer {
def sendWelcomeEmail(account: Account)(implicit settings: MailerSettings) {
val subject = "[Progstr Filer] Welcome to Progstr Filer!"
send(
From("support@mysite.com", "Mysite Support"),
To(account.email -> account.name),
Subject(subject),
HtmlTemplate("welcome-html",
'name -> "Hristo", 'subject -> subject, 'account -> account),
TextTemplate("welcome-text",
'name -> "Hristo", 'subject -> subject, 'account -> account)
)
}
}
import org.apache.commons.mail._
import org.fusesource.scalate._
trait Mailer {
type Address = (String, String)
type Attribute = (Symbol, Any)
def send(commands: EmailCommand*)(implicit settings: MailerSettings) {
val email = new HtmlEmail
email.setHostName(settings.server)
for (command <- commands) {
command match {
case From(address, name) => email.setFrom(address, name)
case To((address, name)) => email.addTo(address, name)
case Cc((address, name)) => email.addCc(address, name)
case Bcc((address, name)) => email.addBcc(address, name)
case Subject(text) => email.setSubject(text)
case Text(text) => email.setTextMsg(text)
case TextTemplate(location, attributes) =>
email.setTextMsg(renderTemplate(location, attributes))
case Html(text) => email.setHtmlMsg(text)
case HtmlTemplate(location, attributes) =>
email.setHtmlMsg(renderTemplate(location, attributes))
}
}
email.send();
}
lazy val engine = new TemplateEngine {
override def isDevelopmentMode = Settings.environment.name == "dev"
}
def renderTemplate(location: String, attributes: Map[String, Any] = Map.empty): String = {
val withExtension = if (location.toLowerCase endsWith(".ssp"))
location
else
location + ".ssp"
engine.layout(templatePath + "/" + withExtension, attributes)
}
def templatePath = getClass.getPackage.getName.replace(".", "/")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment