Skip to content

Instantly share code, notes, and snippets.

@dimitrisli
Forked from mariussoutier/Mail.scala
Last active March 22, 2017 08:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dimitrisli/7170169 to your computer and use it in GitHub Desktop.
Save dimitrisli/7170169 to your computer and use it in GitHub Desktop.
Makes sense to wrap in Some instead of Option since the case is exhaustive
email.username=
email.password=
email.hostname=smtp.gmail.com
email.port=25
email.is.ssl=true
package object mail {
implicit def stringToSeq(single: String): Seq[String] = Seq(single)
implicit def liftToOption[T](t: T): Option[T] = Some(t)
sealed abstract class MailType
case object Plain extends MailType
case object Rich extends MailType
case object MultiPart extends MailType
case class Mail(
from: (String, String), // (email -> name)
to: Seq[String],
cc: Seq[String] = Seq.empty,
bcc: Seq[String] = Seq.empty,
subject: String,
message: String,
richMessage: Option[String] = None,
attachment: Option[(java.io.File)] = None
)
object send {
def a(mail: Mail) {
import org.apache.commons.mail._
import com.typesafe.config.ConfigFactory
val format =
if (mail.attachment.isDefined) MultiPart
else if (mail.richMessage.isDefined) Rich
else Plain
def withResources = (email:Email) => {
val conf = ConfigFactory.load
email.setHostName(conf.getString("email.hostname"))
email.setSmtpPort(conf.getInt("email.port"))
email.setAuthentication(conf.getString("email.username"),conf.getString("email.password"))
email.setSSL(conf.getBoolean("email.is.ssl"))
email
}
val commonsMail: Email = Some(format match {
case Plain => new SimpleEmail().setMsg(mail.message)
case Rich => new HtmlEmail().setHtmlMsg(mail.richMessage.get).setTextMsg(mail.message)
case MultiPart => {
val attachment = new EmailAttachment()
attachment.setPath(mail.attachment.get.getAbsolutePath)
attachment.setDisposition(EmailAttachment.ATTACHMENT)
attachment.setName(mail.attachment.get.getName)
new MultiPartEmail().attach(attachment).setMsg(mail.message)
}
}).map(withResources).get
// Can't add these via fluent API because it produces exceptions
mail.to foreach (commonsMail.addTo(_))
mail.cc foreach (commonsMail.addCc(_))
mail.bcc foreach (commonsMail.addBcc(_))
commonsMail.
setFrom(mail.from._1, mail.from._2).
setSubject(mail.subject).
send()
}
}
}
package something
object Demo {
import mail._
send a new Mail (
from = ("john.smith@mycompany.com", "John Smith"),
to = "boss@mycompany.com",
cc = "hr@mycompany.com",
subject = "Import stuff",
message = "Dear Boss..."
)
send a new Mail (
from = "john.smith@mycompany.com" -> "John Smith",
to = Seq("dev@mycompany.com", "marketing@mycompany.com"),
subject = "Our New Strategy (tm)",
message = "Please find attach the latest strategy document.",
richMessage = "Here's the <blink>latest</blink> <strong>Strategy</strong>..."
)
send a new Mail (
from = "john.smith@mycompany.com" -> "John Smith",
to = "dev@mycompany.com" :: "marketing@mycompany.com" :: Nil,
subject = "Our 5-year plan",
message = "Here is the presentation with the stuff we're going to for the next five years.",
attachment = new java.io.File("/home/boss/important-presentation.ppt")
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment