play2-mailgun


License

License

MIT
GroupId

GroupId

cn.playalot
ArtifactId

ArtifactId

play2-mailgun_2.12
Last Version

Last Version

2.6.7
Release Date

Release Date

Type

Type

jar
Description

Description

play2-mailgun
play2-mailgun
Project URL

Project URL

https://github.com/playalot/play2-mailgun
Project Organization

Project Organization

cn.playalot
Source Code Management

Source Code Management

https://github.com/playalot/play2-mailgun

Download play2-mailgun_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/cn.playalot/play2-mailgun_2.12/ -->
<dependency>
    <groupId>cn.playalot</groupId>
    <artifactId>play2-mailgun_2.12</artifactId>
    <version>2.6.7</version>
</dependency>
// https://jarcasting.com/artifacts/cn.playalot/play2-mailgun_2.12/
implementation 'cn.playalot:play2-mailgun_2.12:2.6.7'
// https://jarcasting.com/artifacts/cn.playalot/play2-mailgun_2.12/
implementation ("cn.playalot:play2-mailgun_2.12:2.6.7")
'cn.playalot:play2-mailgun_2.12:jar:2.6.7'
<dependency org="cn.playalot" name="play2-mailgun_2.12" rev="2.6.7">
  <artifact name="play2-mailgun_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='cn.playalot', module='play2-mailgun_2.12', version='2.6.7')
)
libraryDependencies += "cn.playalot" % "play2-mailgun_2.12" % "2.6.7"
[cn.playalot/play2-mailgun_2.12 "2.6.7"]

Dependencies

compile (1)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.4

provided (2)

Group / Artifact Type Version
com.typesafe.play : play-json_2.12 jar 2.6.7
com.typesafe.play : play-ws_2.12 jar 2.6.7

test (4)

Group / Artifact Type Version
org.mockito : mockito-all jar 1.10.19
org.specs2 : specs2_2.12 jar 2.5
commons-fileupload : commons-fileupload jar 1.3.3
javax.servlet : javax.servlet-api jar 4.0.0

Project Modules

There are no modules declared in this project.

play2-mailgun

Features:

  • Requires no extra dependencies (uses Play Framework libraries only)
  • 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

Installation

Add the release repository

Add the following to your build.sbt:

   resolvers ++= Seq(
     "Millhouse Bintray"  at "http://dl.bintray.com/themillhousegroup/maven"
   )

Pick the right version for your Play app

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:

   libraryDependencies ++= Seq(
     "com.themillhousegroup" %% "play2-mailgun" % "0.3.284"
   )

Development of new features for the Play 2.3/4 has stopped, but the library is still available and works well. Bugfixes (where applicable) from master are backported to the 0.2.x family (i.e. Play 2.4). Just substitute the appropriate version number in the above specifier:

  • For Play 2.3 the version you want is 0.1.256
  • For Play 2.4 the version you want is 0.2.285

Usage

Once you have play2-mailgun added to your project, you can start using it like this:

Put your Mailgun credentials into application.conf

You need the following two entries:

mailgun.api.key=key-abcdef123456abcdef123456abc12345
mailgun.api.url="https://api.mailgun.net/v3/mg.example.com/messages"

Build an EmailMessage containing your email content

Supply plain text and HTML versions of your message:

import com.themillhousegroup.play2.mailgun.EmailMessage
import play.twirl.api.Html

val plainText = "This is the plain text"

val html = Html("<h5>This is <em>actual</em> <strong>HTML!</strong></h5>")

val m = EmailMessage(
      Some("[email protected]"),
      "[email protected]",
      "This is the subject",
      plainText,
      html
    )
Sender addressing

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.

If your requirements are simpler, and you just have a global email address that all emails should "come from" (like a [email protected] or similar) then you can simply set that in your application.conf as follows:

mailgun.default.sender="[email protected]"

And then just pass None as the first argument to EmailMessage() - you can still override it on a case-by-case basis if necessary.

Pass the EmailMessage to MailgunEmailService.send()

It returns a Future[MailgunResponse] (which you can ignore if you don't care):

Play 2.3 static-object style:
import cn.playalot.play2.mailgun.MailgunEmailService

...

MailgunEmailService.send(m).map { mailgunResponse =>
	s"id: ${mailgunResponse.id} - message: ${mailgunResponse.message}"
}
Play 2.4+ dependency-injected style:
import play.api.mvc._
import com.google.inject.Inject
import cn.playalot.play2.mailgun.MailgunEmailService

class MyController @Inject() (val emailService:MailgunEmailService) extends Controller  {

  ...
		emailService.send(m).map { mailgunResponse =>
			s"id: ${mailgunResponse.id} - message: ${mailgunResponse.message}"
		}
	...
}

You can of course use the MailgunEmailService in static style, but it's more in keeping with the Play 2.4+ philosophy to inject this dependency.

Advanced Usage

Sending to multiple recipients

Use a MulticastEmailMessage instance instead of an EmailMessage - it gives you the opportunity to specify multiple To, CC and BCC recipients

import com.themillhousegroup.play2.mailgun.MulticastEmailMessage
import play.twirl.api.Html

val multiEmail = MulticastEmailMessage(
            None,
            None,
            Seq("[email protected]", "[email protected]"),
            Seq("[email protected]", "[email protected]"),
            Seq("[email protected]", "[email protected]"),
            subject,
            plainText,
            html
          )

You can then pass your MulticastEmailMessage to the MailgunEmailService as before.

Using a custom Reply-To address

Use a MulticastEmailMessage as above, and set the second parameter to a Some. If you don't need multiple recipients, just create a Seq around the single recipient, or use Nil as required:

import com.themillhousegroup.play2.mailgun.MulticastEmailMessage
import play.twirl.api.Html

val replyToEmail = MulticastEmailMessage(
            None,
            Some("[email protected]"),
            Seq("[email protected]"),
            Nil, // No CCs
            Nil, // No BCCs
            subject,
            plainText,
            html
          )

Still To-Do

Use one custom template (with .scala.email extension) to define both plain text and HTML message bodies.

Credits / References

cn.playalot

Play

PLAY - 玩具控

Versions

Version
2.6.7