Skip to content

Commit

Permalink
Update for Play 2.6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
gguan committed Nov 6, 2017
1 parent 28ce8a7 commit 219246a
Show file tree
Hide file tree
Showing 20 changed files with 120 additions and 122 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: scala
scala:
- "2.11.7"
- "2.11.11"
jdk:
- oraclejdk8
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Features:
- Super-easy to wire in (just add two values to `application.conf`)
- Send plain-text and/or HTML emails _with the one Twirl template_ (not finished yet)

[![Build Status](https://travis-ci.org/themillhousegroup/play2-mailgun.svg?branch=master)](https://travis-ci.org/themillhousegroup/play2-mailgun)
[![Build Status](https://travis-ci.org/playalot/play2-mailgun.svg?branch=master)](https://travis-ci.org/playalot/play2-mailgun)



Expand All @@ -24,7 +24,7 @@ Add the following to your ```build.sbt```:
```

#### Pick the right version for your Play app
There are versions available for Play 2.3 to 2.5.
There are versions available for Play 2.3 to 2.5.

If you are on Play 2.5, you'll need to use the latest from the `0.3.x` family, as shown below:

Expand Down Expand Up @@ -73,7 +73,7 @@ val m = EmailMessage(
```

##### Sender addressing
You might have noticed the first argument to `EmailMessage` was `Some("[email protected]")` -
You might have noticed the first argument to `EmailMessage` was `Some("[email protected]")` -
if you want emails to come from different senders depending on context, you should pass the sender's
email address in like this.

Expand All @@ -93,7 +93,7 @@ It returns a `Future[MailgunResponse]` (which you can ignore if you don't care):
##### Play 2.3 static-object style:

```
import com.themillhousegroup.play2.mailgun.MailgunEmailService
import cn.playalot.play2.mailgun.MailgunEmailService
...
Expand All @@ -107,7 +107,7 @@ MailgunEmailService.send(m).map { mailgunResponse =>
```
import play.api.mvc._
import com.google.inject.Inject
import com.themillhousegroup.play2.mailgun.MailgunEmailService
import cn.playalot.play2.mailgun.MailgunEmailService
class MyController @Inject() (val emailService:MailgunEmailService) extends Controller {
Expand Down Expand Up @@ -169,7 +169,7 @@ val replyToEmail = MulticastEmailMessage(
Use one custom template (with `.scala.email` extension) to define both plain text and HTML message bodies.

### Credits / References
- [MailGun](http://www.mailgun.com/)
- [MailGun](http://www.mailgun.com/)
- [This Issue in Play](https://github.com/playframework/playframework/issues/902)
- [This Stack Overflow question](http://stackoverflow.com/questions/10890362/play-2-0-how-to-post-multipartformdata-using-ws-url-or-ws-wsrequest)
- [Play Custom Template Formats](https://www.playframework.com/documentation/2.3.x/ScalaCustomTemplateFormat)
72 changes: 53 additions & 19 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,40 +1,74 @@
import scalariform.formatter.preferences._

name := "play2-mailgun"

val deps = Seq(
"com.typesafe.play" %% "play-ws" % "2.5.3" % "provided",
"com.typesafe.play" %% "play-json" % "2.6.7" % "provided",
"com.typesafe.play" %% "play-ws" % "2.6.7" % "provided",
"org.mockito" % "mockito-all" % "1.10.19" % "test",
"org.specs2" %% "specs2" % "2.3.13" % "test",
"commons-fileupload" % "commons-fileupload" % "1.3.1" % "test",
"javax.servlet" % "javax.servlet-api" % "3.1.0" % "test"
"org.specs2" %% "specs2" % "2.5" % "test",
"commons-fileupload" % "commons-fileupload" % "1.3.3" % "test",
"javax.servlet" % "javax.servlet-api" % "4.0.0" % "test"
)

lazy val commonSettings = Seq(
organization := "com.themillhousegroup",
organization := "cn.playalot",
// If the CI supplies a "build.version" environment variable, inject it as the rev part of the version number:
version := s"${sys.props.getOrElse("build.majorMinor", "0.3")}.${sys.props.getOrElse("build.version", "SNAPSHOT")}",
scalaVersion := "2.11.7",
version := "2.6.7",
scalaVersion := "2.12.4",
crossScalaVersions := Seq("2.11.11", "2.12.4"),
crossVersion := CrossVersion.binary,
libraryDependencies ++= deps
)

lazy val publishSettings = Seq(
organization := "cn.playalot",
publishMavenStyle := true,
publishArtifact in Test := false,
publishTo := {
val nexus = "https://oss.sonatype.org/"
if (isSnapshot.value)
Some("snapshots" at nexus + "content/repositories/snapshots")
else
Some("releases" at nexus + "service/local/staging/deploy/maven2")
},
pomExtra :=
<url>http://github.com/playalot/play2-mailgun</url>
<licenses>
<license>
<name>Apache 2</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm>
<url>github.com:playalot/play2-mailgun.git</url>
<connection>scm:git:github.com:playalot/play2-mailgun.git</connection>
</scm>
<developers>
<developer>
<id>gguan</id>
<name>Guan Guan</name>
<url>http://github.com/gguan</url>
</developer>
</developers>)


lazy val root = project.in(file(".")).settings(commonSettings:_*)
lazy val templating = project.in(file("templating")).settings(commonSettings:_*)

lazy val root = project.in(file(".")).settings(commonSettings:_*).settings(publishSettings: _*)
lazy val templating = project.in(file("templating")).settings(commonSettings:_*)

resolvers ++= Seq( "oss-snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
"oss-releases" at "https://oss.sonatype.org/content/repositories/releases",
"Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/")

jacoco.settings
resolvers ++= Seq(
"oss-snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
"oss-releases" at "https://oss.sonatype.org/content/repositories/releases",
"Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/")

publishArtifact in (Compile, packageDoc) := false

seq(bintraySettings:_*)

licenses += ("MIT", url("http://opensource.org/licenses/MIT"))

scalariformSettings

net.virtualvoid.sbt.graph.Plugin.graphSettings

scalariformPreferences := scalariformPreferences.value
.setPreference(AlignSingleLineCaseStatements, true)
.setPreference(DoubleIndentConstructorArguments, true)
.setPreference(DanglingCloseParenthesis, Preserve)
21 changes: 3 additions & 18 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")
addSbtPlugin("com.github.sbt" % "sbt-jacoco" % "3.0.3")

addSbtPlugin("de.johoop" % "jacoco4sbt" % "2.1.6")
addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.2")

addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.3.0")

addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.1.6")

addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.7.4")

resolvers += Resolver.url(
"bintray-sbt-plugin-releases",
url("http://dl.bintray.com/content/sbt/sbt-plugin-releases"))(
Resolver.ivyStylePatterns)

addSbtPlugin("me.lessis" % "bintray-sbt" % "0.1.2")


// Uncomment if this is a Scala 2.11(+) project to get scapegoat linting:
//addSbtPlugin("com.sksamuel.scapegoat" %% "sbt-scapegoat" % "0.94.6")
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.3.3")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.themillhousegroup.play2.mailgun
package cn.playalot.play2.mailgun

import java.io.File

Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
package com.themillhousegroup.play2.mailgun
package cn.playalot.play2.mailgun

import java.io.File
import javax.inject.Inject

import akka.stream.scaladsl.{ FileIO, Source }
import play.api.libs.concurrent.Execution.Implicits._
import akka.util.ByteString
import cn.playalot.play2.mailgun.MailgunEmailService.AttachmentPartType
import play.api.{ Configuration, Logger }
import play.api.http.Status
import play.api.libs.ws.{ WSAuthScheme, WSClient, WSRequest, WSResponse }
import play.api.mvc.MultipartFormData
import play.api.mvc.MultipartFormData.{ DataPart, FilePart, Part }
import play.api.Play
import play.api.Logger

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import play.api._
import play.api.http._
import play.api.libs.ws._
import play.api.Play.current
import javax.inject.Inject

import akka.util.ByteString
import com.themillhousegroup.play2.mailgun.MailgunEmailService.AttachmentPartType
import play.api.mvc.MultipartFormData

/** For static-style usage: */
object MailgunEmailService extends MailgunEmailService(WS.client, Play.current.configuration) {
object MailgunEmailService {
type AttachmentPartType = Source[ByteString, Any]
type PostData = Source[MultipartFormData.Part[AttachmentPartType], _]
}

class MailgunEmailService @Inject() (wsClient: WSClient, configuration: Configuration) extends MailgunResponseJson {

lazy val mailgunApiKey: String = configuration.getString("mailgun.api.key").get
lazy val defaultSender: Option[String] = configuration.getString("mailgun.default.sender")
lazy val mailgunUrl: String = configuration.getString("mailgun.api.url").get
lazy val mailgunApiKey: String = configuration.get[String]("mailgun.api.key")
lazy val defaultSender: Option[String] = configuration.getOptional[String]("mailgun.default.sender")
lazy val mailgunUrl: String = configuration.get[String]("mailgun.api.url")
lazy val ws: WSRequest = wsClient.url(mailgunUrl)

/** Sends the message via Mailgun's API, respecting any options provided */
Expand All @@ -39,8 +34,7 @@ class MailgunEmailService @Inject() (wsClient: WSClient, configuration: Configur
} else {
val sender = message.from.getOrElse(defaultSender.get)

ws
.withAuth("api", mailgunApiKey, WSAuthScheme.BASIC)
ws.withAuth("api", mailgunApiKey, WSAuthScheme.BASIC)
.post(buildMultipartRequest(sender, message, options))
.flatMap(handleMailgunResponse)
}
Expand Down Expand Up @@ -92,7 +86,7 @@ class MailgunEmailService @Inject() (wsClient: WSClient, configuration: Configur
Future.failed(
response.status match {
case Status.UNAUTHORIZED => new MailgunAuthenticationException((response.json \ "message").as[String])
case _ => new MailgunSendingException((response.json \ "message").as[String])
case _ => new MailgunSendingException((response.json \ "message").as[String])
}
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.themillhousegroup.play2.mailgun
package cn.playalot.play2.mailgun

import play.api.mvc.MultipartFormData.DataPart

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package cn.playalot.play2.mailgun

case class MailgunResponse(message: String, id: String) {
lazy val status: MailgunSentMessageStatus = MessageQueued
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cn.playalot.play2.mailgun

import play.api.libs.json.Json

trait MailgunResponseJson {
implicit val responseReads = Json.reads[MailgunResponse]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.themillhousegroup.play2.mailgun
package cn.playalot.play2.mailgun

/**
* Represents a problem returned by the Mailgun sending API
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package cn.playalot.play2.mailgun

trait MailgunSentMessageStatus
3 changes: 3 additions & 0 deletions src/main/scala/cn/playalot/play2/mailgun/MessageQueued.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package cn.playalot.play2.mailgun

case object MessageQueued extends MailgunSentMessageStatus
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.themillhousegroup.play2.mailgun.utils
package cn.playalot.play2.mailgun.utils

import scala.xml.{ NodeSeq, Node, Text }
import play.twirl.api.{ HtmlFormat, Html, Content }
import scala.xml.{ NodeSeq, Text }

object DualFormatMarkup {

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.themillhousegroup.play2.mailgun
package cn.playalot.play2.mailgun

import cn.playalot.play2.mailgun.utils.DualFormatMarkup
import cn.playalot.play2.mailgun.utils.DualFormatMarkup.str2NodeSeq
import org.specs2.mutable.Specification
import com.themillhousegroup.play2.mailgun.utils.DualFormatMarkup._
import play.twirl.api.Html
import com.themillhousegroup.play2.mailgun.utils.DualFormatMarkup
import scala.xml.NodeSeq

class DualFormatMarkupSpec extends Specification {
"Dual Format helpers" should {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.themillhousegroup.play2.mailgun
package cn.playalot.play2.mailgun

import org.specs2.mutable._
import play.twirl.api.Html
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
package com.themillhousegroup.play2.mailgun
package cn.playalot.play2.mailgun

import akka.stream.scaladsl.Source
import akka.util.ByteString
import org.specs2.mutable._
import org.specs2.mock.Mockito
import org.mockito.ArgumentCaptor
import play.twirl.api.Html
import play.api.libs.ws._
import play.api._
import play.api.http._
import play.api.libs.json._

import scala.concurrent.Future
import scala.concurrent.Await
import scala.concurrent.duration.Duration
import org.apache.commons.fileupload.disk._

import scala.Predef._
import scala.Some
import org.apache.commons.fileupload.{ FileItem, FileUpload, UploadContext }
import play.api.mvc.MultipartFormData
import play.api.mvc.MultipartFormData.DataPart
import play.api.Configuration
import play.api.libs.json.Json
import play.api.libs.ws._
import play.twirl.api.Html

class EmailServiceSpec extends Specification with Mockito {

Expand Down
10 changes: 1 addition & 9 deletions templating/build.sbt
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@

libraryDependencies ++= Seq(
"com.themillhousegroup" %% "scoup" % "0.2.26"
)


resolvers ++= Seq( "oss-snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
"oss-releases" at "https://oss.sonatype.org/content/repositories/releases",
"Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/",
"Millhouse Bintray" at "http://dl.bintray.com/themillhousegroup/maven"
"org.jsoup" % "jsoup" % "1.10.3"
)
Loading

0 comments on commit 219246a

Please sign in to comment.