Search code examples
scalaemailplayframeworkmailer

How to send an email to multiple recipients and hide their addresses using Play Mailer


I am using Play Mailer with Scala, and I am trying to send the same email to multiple recipients. Is there any way to send my email to multiple recipients separately which means that a recipient cannot see the addresses of the other recipients ? Here is the code used to send the same email to a sequence of emails, so the recipients can see all other addresses in the TO field.

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>",<email3@gmail.com>),
      // adds attachment
      attachments = Seq(),
      // sends text, HTML or both...
      bodyText = Some("A text message"),
      bodyHtml = Some("content")
      )
    mailerClient.send(email)
    Future.successful(Ok("ok !!"))
  }
}

Solution

  • The best practice to send a mail to several recipients without them knowing there are other recipients is to send an email to yourself and add all your recipients as BCC.

    Add this to your Mail object :

    bcc = Seq("email1@domain.com", "email2@domain.com")
    

    and set the TO field with your own mail address.