play-mailer 0

Play mailer module based on Amazon SES

3 years after

Scala mailer module for Play 2.5.x

Scala wrapper around java mail which allows you to send emails. The default configuration options exposed in Configuration work using Amazon SES SMTP

Installation

  val appDependencies = Seq(
    "net.kaliber" %% "play-mailer" % "5.0.0"
  )

  resolvers += "Kaliber Repository" at "https://jars.kaliber.io/artifactory/libs-release-local"

Configuration

application.conf should contain the following information:

mail.failTo="[email protected]"

mail.host=email-smtp.us-east-1.amazonaws.com
mail.port=465

#only required if mail.auth=true (the default)
mail.username="Smtp username as generated by Amazon"
mail.password="Smtp password"

application.conf can additionally contain the following information:

#default is smtps
mail.transport.protocol=smtp

#default is true
mail.auth=false

Usage

Creating an email

  import play.modules.mailer._

  Email(
    subject = "Test mail",
    from = EmailAddress("Erik Westra sender", "[email protected]"),
    text = "text",
    htmlText = "htmlText",
    replyTo = None,
    recipients = List(Recipient(
      RecipientType.TO, EmailAddress("Erik Westra recipient", "[email protected]"))),
    attachments = Seq.empty)

  // a more convenient way to create an email
  val email = Email(
    subject = "Test mail",
    from = EmailAddress("Erik Westra sender", "[email protected]"),
    text = "text",
    htmlText = "htmlText")
    .to("Erik Westra TO", "[email protected]")
    .cc("Erik Westra CC", "[email protected]")
    .bcc("Erik Westra BCC", "[email protected]")
    .replyTo("Erik Westra REPLY_TO", "[email protected]")
    .withAttachments(
      Attachment("attachment1", Array[Byte](0, 1), "application/octet-stream"),
      Attachment("attachment2", Array[Byte](0, 1), "application/octet-stream", Disposition.Inline))

Sending an email synchronously

  import play.modules.mailer._

  val result:Try[Unit] = Mailer.sendEmail(email)

  result match {
    case Success(_) =>
      //mail sent successfully
    case Failure(SendEmailException(email, cause)) =>
      //failed to send email, cause provides more information
    case Failure(SendEmailTransportCloseException(None, cause)) =>
        //failed to close the connection, no email was sent
    case Failure(SendEmailTransportCloseException(Some(Success(_)), cause)) =>
        //failed to close the connection, the email was sent
    case Failure(SendEmailTransportCloseException(Some(Failure(SendEmailException(email, cause1))), cause2)) =>
        //failed to close the connection, the email was not sent
  }

Sending multiple emails synchronously

  import play.modules.mailer._

  val result:Try[Seq[Try[Unit]]] = Mailer.sendEmails(email1, email2)

  result match {
    case Success(results) =>
      results.foreach {
        case Success(_) =>
          //mail sent successfully
        case Failure(SendEmailException(email, cause)) =>
          //failed to send email, cause provides more information
      }
    case Failure(SendEmailsTransportCloseException(None, cause)) =>
      //failed to close the connection, no email was sent
    case Failure(SendEmailsTransportCloseException(Some(Seq(Success(_), Failure(SendEmailException(email, cause1))), cause2)) =>
      //failed to close the connection, one of the emails was sent
  }

Sending mail asynchonously

  import play.modules.mailer._

  val result:Future[Unit] = AsyncMailer.sendEmail(email)

  result
    .map { unit =>
      // mail sent successfully
  }
  .recover {
    case SendEmailException(email, cause) =>
      // problem sending email
    case SendEmailTransportCloseException(result, cause) =>
      // problem closing connection
  }

Sending mails asynchonously

  import play.modules.mailer._

  val result:Future[Seq[Try[Unit]]] = AsyncMailer.sendEmails(email)

  result
    .map { results =>
      results.foreach {
        case Success(_) =>
          //mail sent successfully
        case Failure(SendEmailException(email, cause)) =>
          //failed to send email, cause provides more information
      }
    }
    .recover {
      case SendEmailException(email, cause) =>
        // problem sending email
      case SendEmailTransportCloseException(result, cause) =>
        // problem closing connection
    }

Related Repositories

ruby-style-guide

ruby-style-guide

A community-driven Ruby coding style guide ...

play-authenticate

play-authenticate

An authentication plugin for Play Framework 2.x (Java) ...

PlayStartApp

PlayStartApp

Starting APP Play Framework 2.x with Signup, Signin, Reset password, Change Emai ...

ruby-style-guide

ruby-style-guide

A community-driven Ruby coding style guide ...

Play-with-deadbolt-securesocial-reactivemongo-mailer

Play-with-deadbolt-securesocial-reactivemongo-mailer

Play 2.2.0 with Securesocial, Deadbolt-2-scala, Play-reactivemongo, Play-salat, ...


Top Contributors

EECOLOR jeantil eamelink klaasman jorkzijlstra jvankralingen jzijlstra phidias

Releases

-   v5.0.0 zip tar
-   v4.0.0 zip tar
-   v3.1.0 zip tar
-   v3.0.0 zip tar
-   v2.2.0 zip tar
-   v2.1.3 zip tar
-   v2.1.2 zip tar
-   v2.1.1 zip tar
-   v2.0.1 zip tar
-   v2.0.0 zip tar
-   v1.1.0 zip tar