Sending Emails in Playframework 2.0

后端 未结 3 1777
广开言路
广开言路 2020-12-24 07:50

In Playframework 2.0, it does not appear to be as simple to send emails (see comments on Using sendmail as SMTP server in Play Framework) as it did in Play 1.x. There is no

相关标签:
3条回答
  • 2020-12-24 07:55

    The accepted answer is that Play needs a plugin to send e-mails. This is false. You can easily adapt any JVM mailing library for your Play app. Here's an example using Apache Commons Email, adapted for simplicity from here and our own production code.

    import org.apache.commons.mail._
    import scala.util.Try
    
    private val emailHost = Play.configuration.getString("email.host").get
    
    /**
     *  Sends an email
     *  @return Whether sending the email was a success
     */
    def sendMail(from: (String, String), // (email -> name)
                 to: Seq[String],
                 cc: Seq[String] = Seq.empty,
                 bcc: Seq[String] = Seq.empty,
                 subject: String,
                 message: String,
                 richMessage: Option[String] = None,
                 attachment: Option[java.io.File] = None) = {
    
      val commonsMail: Email = if(mail.attachment.isDefined) {
          val attachment = new EmailAttachment()
          attachment.setPath(mail.attachment.get.getAbsolutePath)
          attachment.setDisposition(EmailAttachment.ATTACHMENT)
          attachment.setName("screenshot.png")
          new MultiPartEmail().attach(attachment).setMsg(mail.message)
        } else if(mail.richMessage.isDefined) {
          new HtmlEmail().setHtmlMsg(mail.richMessage.get).setTextMsg(mail.message)
        } else {
          new SimpleEmail().setMsg(mail.message)
        }
      }
    
      commonsMail.setHostName(emailHost)
    
      to.foreach(commonsMail.addTo(_))
      cc.foreach(commonsMail.addCc(_))
      bcc.foreach(commonsMail.addBcc(_))
    
      val preparedMail = commonsMail.
        setFrom(mail.from._2, mail.from._1).
        setSubject(mail.subject)
    
      // Send the email and check for exceptions
      Try(preparedMail.send).isSuccess
    }
    
    def sendMailAsync(...) = Future(sendMail(...))
    

    Given that e-mail sending is so trivially accomplished in Play, I'm surprised plugins are recommended at all. Depending on a plugin can hurt you if you want to upgrade Play versions, and I don't feel something that takes 30 LoC to accomplish yourself is worth it. Our code has worked unmodified upgrading from Play 2.0 to 2.1 to 2.2.

    0 讨论(0)
  • 2020-12-24 08:00

    Playframework 2.x requires a plugin for Mail to work. It was not added to the core because developers felt it was trivial to get Email working, so decided it was better to create a plugin. However, the quick flurry of messages on google groups suggested they got it wrong...people expected the same functionality as Play 1.x.

    As you expect from the community though, a plugin was quickly built. See https://github.com/playframework/play-mailer.

    There will be more plugins to look out for as well, but this is the typesafe supported one by a core developer, so I expect it to be the best maintained.

    0 讨论(0)
  • 2020-12-24 08:15

    I quickly hacked plugin with support for attachments, because so far the one mentioned @Codemwnci doesn't have it. You can check it out.

    0 讨论(0)
提交回复
热议问题