Play Framework 2 mail plugin
This project is maintained by mcveat
To your Build.scala
add:
resolvers += (
Resolver.url("mcveat.github.com", url("http://mcveat.github.com/releases"))(Resolver.ivyStylePatterns)
)
and "play.modules.mail" %% "play2-mail-plugin" % "0.6"
as a dependency.
Checkout the project, build it from the sources with sbt package
command. Then either:
target/scala-2.10.0
to the lib folder of your play appsbt publish-local
and add "play.modules.mail" %% "play2-mail-plugin" % "0.7-SNAPSHOT"
to your build settings.You can add it as a submodule of your play project.
Checkout the project in modules/mail-plugin, then do git submodule add
In your project Build.scala add the dependency to the plugin :
val mailPlugin = Project("mailPlugin", file("modules/mail-plugin"))
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA)
.dependsOn(mailPlugin)
import mail._
import Mail._
def sendMail = Action.async { request =>
val attachment = Source.fromBytes("Ninja should wear black".toCharArray.map(_.toByte))
val result = Mail()
.from("sender", "sender@example.com")
.to("receiver", "receiver@example.com")
.replyTo("ninja master", "master@ninja.com")
.withSubject("A subject")
.withText("body")
.withAttachments(Attachment("ninja code", attachment, "text/plain")
.send()
result.flatMap { _ => Ok("It works") }
}
Mail class utilizes statically typed builder pattern, so send()
method is not available before sender, receiver,
subject and message body (either text or html) is set.
You can check this plugin scaladoc too.
In application.conf
:
#put this setting in you want to mock the mail server in development
mail.mock=true
#smtp server settings
smtp.host=smtp.server.com
smtp.port=25
smtp.username=
smtp.password=
smtp.transport=
Supported transports: SMTP_PLAIN
(default), SMTP_SSL
, SMTP_TLS
send()
method from Unit
to Future[Unit]