Hkt
Hkt

Reputation: 65

Play Framework 2.4 sending email

I'm new to Scala and I got stuck in sending an email...

I'm working with Play! 2.4 and have to following in build.scala

"com.typesafe.play" %% "play-mailer" % "3.0.1"

In application.conf the following:

play.mailer {
    host=smtp.gmail.com
    port=587 
    ssl=true
    tls=true
    user="[email protected]"
    password=abracadabra
    debug=true

}

What I'm trying to do is to send an email to an user, for example user_1, when some other user, user_2, borrows a book from user_1.

In my book controller I have this methods:

def sendEmail(email_user: String, subject: String, msg: String): Unit = { 

    val email = Email(
      subject,
      "FROM <[email protected]>",
      Seq("TO <"+email_user+">"),

      bodyText = Some(msg)
    )

}

In the next method I call the sendEmail method if user_2 borrows a book from user_1

  def borrowBook(id_book: Long) = Action { implicit request =>
     val nr = Book.checkBook(id_book)
     val date = new Date()      

     if(nr > 0)
     {
        val list = Book.getCarte(id_book)
        val no = Friend.isFriend(list.id_user, userInfo.id.get)
        if(no == 1){ 
         Borrow.insert(Borrow(id_book, userInfo.id.get, date))

         sendEmail(userInfo.email, "Borrow book", "Your book has been borrowed")
         Ok(views.html.borrowBook())
         }else Ok(views.html.warningBorrow())
     }
     else Ok(views.html.failBorrow())

 }

The problem is that I don't receive any error or something like that and I don't know what I'm doing wrong.

Can anyone please help me?

Thank you very much!

Upvotes: 2

Views: 3418

Answers (2)

majesiu
majesiu

Reputation: 98

This is the answer to your latest comment (not enough rep to comment yet) about ports: try port 465 because you have enabled both SSL and TLS. Also try to switch off debug mode (just remove the line with it). Checked your code with appropariate send method with my own google account and it worked fine.

Upvotes: 2

Mike Curry
Mike Curry

Reputation: 1609

I don't believe you are actually sending the email. The sendEmail method is just creating the Email class, but it doesn't do anything else with and it doesn't return it. That does not actually send the email.

According to: https://github.com/playframework/play-mailer you are supposed to send the email by calling the MailerClient

The example linked shows code which basically configures the mail client to send via a particular server and then calls mailerClient.send(email):

In the example, the MailerClient used, was injected into the controller at construction time.

Upvotes: 3

Related Questions