Sending Emails in Playframework 2.0

17,052

Solution 1

Playframework 2.x requires a plugin for Mail to work. It was not added to the core because developers felt it was trivial to get Email working, so decided it was better to create a plugin. However, the quick flurry of messages on google groups suggested they got it wrong...people expected the same functionality as Play 1.x.

As you expect from the community though, a plugin was quickly built. See https://github.com/playframework/play-mailer.

There will be more plugins to look out for as well, but this is the typesafe supported one by a core developer, so I expect it to be the best maintained.

Solution 2

The accepted answer is that Play needs a plugin to send e-mails. This is false. You can easily adapt any JVM mailing library for your Play app. Here's an example using Apache Commons Email, adapted for simplicity from here and our own production code.

import org.apache.commons.mail._
import scala.util.Try

private val emailHost = Play.configuration.getString("email.host").get

/**
 *  Sends an email
 *  @return Whether sending the email was a success
 */
def sendMail(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) = {

  val commonsMail: Email = if(mail.attachment.isDefined) {
      val attachment = new EmailAttachment()
      attachment.setPath(mail.attachment.get.getAbsolutePath)
      attachment.setDisposition(EmailAttachment.ATTACHMENT)
      attachment.setName("screenshot.png")
      new MultiPartEmail().attach(attachment).setMsg(mail.message)
    } else if(mail.richMessage.isDefined) {
      new HtmlEmail().setHtmlMsg(mail.richMessage.get).setTextMsg(mail.message)
    } else {
      new SimpleEmail().setMsg(mail.message)
    }
  }

  commonsMail.setHostName(emailHost)

  to.foreach(commonsMail.addTo(_))
  cc.foreach(commonsMail.addCc(_))
  bcc.foreach(commonsMail.addBcc(_))

  val preparedMail = commonsMail.
    setFrom(mail.from._2, mail.from._1).
    setSubject(mail.subject)

  // Send the email and check for exceptions
  Try(preparedMail.send).isSuccess
}

def sendMailAsync(...) = Future(sendMail(...))

Given that e-mail sending is so trivially accomplished in Play, I'm surprised plugins are recommended at all. Depending on a plugin can hurt you if you want to upgrade Play versions, and I don't feel something that takes 30 LoC to accomplish yourself is worth it. Our code has worked unmodified upgrading from Play 2.0 to 2.1 to 2.2.

Solution 3

I quickly hacked plugin with support for attachments, because so far the one mentioned @Codemwnci doesn't have it. You can check it out.

Share:
17,052

Related videos on Youtube

Codemwnci
Author by

Codemwnci

I am A Software Architect working for a FTSE 100 Insurance company in the UK. Author of Introducing the Play Framework. A technologist. My main areas of interest are Kotlin / Java The Play Framework Web Programming

Updated on July 10, 2022

Comments

  • Codemwnci
    Codemwnci almost 2 years

    In Playframework 2.0, it does not appear to be as simple to send emails (see comments on Using sendmail as SMTP server in Play Framework) as it did in Play 1.x. There is no out of the box mailer functionality... so, how do I send emails?

  • Eve Freeman
    Eve Freeman almost 12 years
    This list of plugins for 2.0 has been handy--note that it is supposed to be temporary: github.com/playframework/Play20/wiki/Modules
  • Samo
    Samo almost 12 years
    @Codemwnci: I'm looking through this module for attachment support but it seems to be either hiding or non-existent. Tips?
  • Trick
    Trick over 11 years
    Path is wrong in readme - try "com.typesafe" % "play-plugins-mailer_2.9.1" % "2.0.4". notice _2.9.1.
  • Levente Holló
    Levente Holló about 11 years
    If you're using Play! 2.1, use this dependency: "com.typesafe" %% "play-plugins-mailer" % "2.1.0"
  • Kyle Luke
    Kyle Luke about 8 years
    @LeventeHolló what's the dependency for 2.2.6? Thank you
  • John
    John over 5 years
    I appreciate your answer!