Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sending mails fluently in Scala
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[(] = None
object send {
def a(mail: Mail) {
import org.apache.commons.mail._
val format =
if (mail.attachment.isDefined) MultiPart
else if (mail.richMessage.isDefined) Rich
else Plain
val commonsMail: Email = 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()
new MultiPartEmail().attach(attachment).setMsg(mail.message)
// TODO Set authentication from your configuration, sys properties or w/e
// Can't add these via fluent API because it produces exceptions foreach (commonsMail.addTo(_)) foreach (commonsMail.addCc(_))
mail.bcc foreach (commonsMail.addBcc(_))
setFrom(mail.from._1, mail.from._2).
package something
object Demo {
import mail._
send a new Mail (
from = ("", "John Smith"),
to = "",
cc = "",
subject = "Import stuff",
message = "Dear Boss..."
send a new Mail (
from = "" -> "John Smith",
to = Seq("", ""),
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",
to = "" :: "" :: 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"/home/boss/important-presentation.ppt")

Dinduks commented Mar 4, 2014

Thank you for sharing this.

MMMarcy commented Dec 22, 2014

Thanks man, really appreciate

Thank you for sharing this

hraban commented Mar 31, 2015

Hi, thanks for sharing. What's the license on this code? Can it be used in commercial projects?

Can you add a license header?

schon commented Oct 26, 2015

Thank you for sharing this

azickh commented Jan 22, 2016

Hi, I'm posting html, but get html content like attachments. What is the problem?

val html = """<html>Thanks for Joining!
               |You’re going to love
               |To complete your registration for ***, please <a href="">confirm</a> your email.
               |Need help? Have feedback? Feel free to contact us</html>""".stripMargin

send a new Mail(
  from = -> "noreplay",
  to = Seq(""),
  subject = "Some subject",
  message = "Some plain content",
  richMessage = html

Thanks, Aziz

This is awesome

Thank you for sharing this

this is so fluent. thanks @mariussoutier

Beautifully written. Thanks. 👍

Thanks a lot

Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment