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
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.