Skip to content
This repository has been archived by the owner on Dec 24, 2019. It is now read-only.

kaliber-scala/scala-mailer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

91 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This repository is no longer maintained

Just create a fork, if you want I can list it here.


Scala mailer module with support 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

You can add the scala-mailer to SBT, by adding the following to your build.sbt file

  libraryDependencies += "net.kaliber" %% "play-mailer" % "6.0.0"

Usage

Creating an email

  import net.kaliber.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 asynchronously

  import net.kaliber.mailer._

  val mailerSettings = MailerSettings(
    protocol = Some("smtps"),
    host = "email-smtp.us-east-1.amazonaws.com",
    port = "465",
    failTo = "[email protected]",
    auth = Some(true),
    username = Some("Smtp username as generated by Amazon"),
    password = Some("Smtp password")
    
  val result = new Mailer(Session.fromSetting(mailerSettings)).sendEmail(email)
    
  result
    .map { unit =>
      // mail sent successfully
    }
    .recover {
      case SendEmailException(email, cause) =>
      // problem sending email
      case SendEmailTransportCloseException(result, cause) =>
      // problem closing connection
    }

Sending multiple emails asynchronously

  import net.kaliber.mailer._
  
  val mailerSettings = MailerSettings(
    protocol = Some("smtps"),
    host = "email-smtp.us-east-1.amazonaws.com",
    port = "465",
    failTo = "[email protected]",
    auth = Some(true),
    username = Some("Smtp username as generated by Amazon"),
    password = Some("Smtp password")
      
  val result = new Mailer(Session.fromSetting(mailerSettings)).sendEmails(email1, email2)

  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
    }

Play 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 with Play configuration

Sending an email asynchronously

  import net.kaliber.mailer._
  import net.kaliber.play.mailer.Session

  val result = new Mailer(Session.fromConfiguration(configuration)).sendEmail(email)
  
  result
    .map { unit =>
      // mail sent successfully
    }
    .recover {
      case SendEmailException(email, cause) =>
      // problem sending email
      case SendEmailTransportCloseException(result, cause) =>
      // problem closing connection
    }

Sending multiple emails asynchronously

  import net.kaliber.mailer._
  import net.kaliber.play.mailer.Session

  val results = new Mailer(Session.fromConfiguration(configuration)).sendEmails(Seq(email1, email2))
  
  results
    .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
    }