Search code examples
scalaemailplayframeworkmailer

error Using bcc with play mailer and scala: addBcc is not a member of play.api.libs.mailer.Email


I'm using play mailer (https://github.com/playframework/play-mailer) to send emails from my application to a group of person but I don't want that a recipient can see the addresses of the other recipients (in the TO field) I found that bcc can resolve this, I tried this but it didn't work:

package controllers

import play.api.libs.mailer._
import java.io.File
import org.apache.commons.mail.EmailAttachment
import javax.inject.Inject
import play.api._
import play.api.mvc._
import scala.concurrent.Future
import play.api.libs.json._

class MailerApi @Inject() (mailerClient: MailerClient) extends Controller {
  def sendEmail = Action.async(parse.json) { request =>
    val subject: String = (request.body \ "subject").as[String]

    val cid = "1234"
    val email = Email(
      subject,
      "Me <email1@gmail.com>",
      Seq("Miss TO email2@gmail.com"),
      // adds attachment
      attachments = Seq(),
      // sends text, HTML or both...
      bodyText = Some("A text message"),
      bodyHtml = Some("content")).addBcc("<email1@gmail.com>")
    mailerClient.send(email)
    Future.successful(Ok("ok !!"))
  }
}

I get the error:

value addBcc is not a member of play.api.libs.mailer.Email
Read from stdout: possible cause: maybe a semicolon is missing before `value addBcc'?

Solution

  • Here you go:

    package controllers

    import play.api.libs.mailer._
    import java.io.File
    import org.apache.commons.mail.EmailAttachment
    import javax.inject.Inject
    import play.api._
    import play.api.mvc._
    import scala.concurrent.Future
    import play.api.libs.json._
    
    class MailerApi @Inject() (mailerClient: MailerClient) extends Controller {
      def sendEmail = Action.async(parse.json) { request =>
        val subject: String = (request.body \ "subject").as[String]
    
        val cid = "1234"
        val email = Email(
          subject,
          "ExcelWay <email1@gmail.com>",
          Seq("Miss TO email2@gmail.com"),
          // adds attachment
          attachments = Seq(),
          // sends text, HTML or both...
          bodyText = Some("A text message"),
          bodyHtml = Some("content"),
          bcc= Seq("bccemail@example.com")
          )
        mailerClient.send(email)
        Future.successful(Ok("ok !!"))
      }
    }