Posted in Play, Scala

Sending Emails with Play 2.3 and Gmail

On recent project I found myself in the need to send emails from a Scala Play app. The documentation I hit first online was a bit misleading so I’ve written some basic notes.

Email was removed from the core play framework in 2.0 which is ok but means you have to evaluate your options. This basically comes to two options

  1. Directly use Apache Commons Email (http://commons.apache.org/proper/commons-email/). It is the defacto standard java library for email, its mature, feature rich and there is plenty of support.
  2. Play Mailer Plugin. This appears to be the defacto standard plugin to use email on play. It hasn’t tried to reinvent the wheel and instead it uses apache commons under the covers with integration into the play framwork. Unfortunately in the 2.3.1 version it still looks like a Java api. The current snapshot is looking much better but I’m sticking with stable versions.

I’ve gone with the Play Mailer Plugin. It’s integrated into play and lets me configure it using the play configuration and a number of other play plugins (e.g. SecureSocial) can use it as a dependency for sending email.

This first mistake I made was trying trying to follow the documentation at https://github.com/playframework/play-mailer. This is of course the readme from the master branch but it appears its been half updated so it still refers to configuring 2.3.1 but the examples don’t work. The real documentation you want to access is on v2.3.1 tag https://github.com/playframework/play-mailer/tree/2.3.1. Once you’re following the right guide its easy.

So to setup play to send emails using a gmail.

  1. Add the play mailer as a dependency in the sbt build file
    libraryDependencies ++= Seq(
      //...
      "com.typesafe.play.plugins" %% "play-plugins-mailer" % "2.3.1"
      //...
    )
    
  2. Configure the mailer as a play plugin by adding this line to the play.plugins config file
    1500:com.typesafe.plugin.CommonsMailerPlugin
    
  3. Configure sending of email using your mail account. (Gmail in this example)
    smtp {
      host = smtp.gmail.com
      port = 587
      user = "mygmail@gmail.com"
      password = "mypassword"
      from = "John User <mygmail@gmail.com>"
      tls = yes
      ssl = no
    }
    
  4. Lets test it out
    val mail = use[MailerPlugin].email
    mail.setSubject("mailer test")
    mail.setRecipient("myfriend@gmail.com")
    mail.send("Hello, this is a test. ")
    

It works!

Advertisements

One thought on “Sending Emails with Play 2.3 and Gmail

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s