-
-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Immutable mailer instance #63
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package controllers; | ||
|
||
import org.apache.commons.mail.EmailAttachment; | ||
import play.Configuration; | ||
import play.Play; | ||
import play.api.libs.mailer.MailerClient; | ||
import play.libs.mailer.Email; | ||
|
@@ -10,8 +9,6 @@ | |
|
||
import javax.inject.Inject; | ||
import java.io.File; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ApplicationJava extends Controller { | ||
|
||
|
@@ -34,16 +31,4 @@ public Result send() { | |
String id = mailer.send(email); | ||
return ok("Email " + id + " sent!"); | ||
} | ||
|
||
public Result configureAndSend() { | ||
final Email email = new Email() | ||
.setSubject("Simple email") | ||
.setFrom("[email protected]") | ||
.addTo("[email protected]"); | ||
Map<String, Object> conf = new HashMap<>(); | ||
conf.put("host", "typesafe.org"); | ||
conf.put("port", 1234); | ||
String id = mailer.configure(new Configuration(conf)).send(email); | ||
return ok("Email " + id + " sent!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ import java.io.File | |
import javax.inject.Inject | ||
|
||
import org.apache.commons.mail.EmailAttachment | ||
import play.api.Configuration | ||
import play.api.Play.current | ||
import play.api.libs.mailer._ | ||
import play.api.mvc.{Action, Controller} | ||
|
@@ -27,9 +26,9 @@ class ApplicationScala @Inject()(mailer: MailerClient) extends Controller { | |
Ok(s"Email $id sent!") | ||
} | ||
|
||
def configureAndSend = Action { | ||
val email = Email("Simple email", "[email protected]", Seq("[email protected]")) | ||
val id = mailer.configure(Configuration.from(Map("host" -> "typesafe.org", "port" -> 1234))).send(email) | ||
def sendWithCustomMailer = Action { | ||
val mailer = new SMTPMailer(SMTPConfiguration("typesafe.org", 1234)) | ||
val id = mailer.send(Email("Simple email", "Mister FROM <from@email.com>")) | ||
Ok(s"Email $id sent!") | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
samples/runtimeDI/app/controllers/CustomSMTPConfigurationProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package controllers | ||
|
||
import javax.inject.Provider | ||
|
||
import play.api.libs.mailer.SMTPConfiguration | ||
import play.api.{Configuration, Environment} | ||
import play.api.inject.Module | ||
|
||
class CustomSMTPConfigurationProvider extends Provider[SMTPConfiguration] { | ||
override def get() = new SMTPConfiguration("typesafe.org", 1234) | ||
} | ||
|
||
class CustomMailerConfigurationModule extends Module { | ||
def bindings(environment: Environment, configuration: Configuration) = Seq( | ||
bind[SMTPConfiguration].toProvider[CustomSMTPConfigurationProvider] | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,38 @@ | ||
package play.api.libs.mailer | ||
|
||
import java.io.{File, FilterOutputStream, PrintStream} | ||
import javax.inject.Inject | ||
import javax.inject.{Inject, Provider} | ||
import javax.mail.internet.InternetAddress | ||
|
||
import org.apache.commons.mail._ | ||
import play.api.inject._ | ||
import play.api.{Configuration, Environment, Logger, PlayConfig} | ||
import play.libs.mailer.{Email => JEmail, MailerClient => JMailerClient} | ||
import play.{Configuration => JConfiguration} | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
// for compile-time injection | ||
trait MailerComponents { | ||
def configuration: Configuration | ||
lazy val mailerClient = new CommonsMailer(configuration) | ||
lazy val mailerClient = new SMTPMailer(new SMTPConfigurationProvider(configuration).get()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perfect merge 👍 |
||
} | ||
|
||
// for runtime injection | ||
class MailerModule extends Module { | ||
def bindings(environment: Environment, configuration: Configuration) = Seq( | ||
bind[MailerClient].to[CommonsMailer], | ||
bind[MailerClient].to[SMTPMailer], | ||
bind[JMailerClient].to(bind[MailerClient]), | ||
bind[MailerClient].qualifiedWith("mock").to[MockMailer], | ||
bind[JMailerClient].qualifiedWith("mock").to[MockMailer] | ||
) | ||
} | ||
|
||
class SMTPConfigurationModule extends Module { | ||
def bindings(environment: Environment, configuration: Configuration) = Seq( | ||
bind[SMTPConfiguration].toProvider[SMTPConfigurationProvider] | ||
) | ||
} | ||
|
||
// API | ||
|
||
@deprecated("Use injected MailerClient instead", "2.4.0") | ||
|
@@ -46,18 +51,6 @@ trait MailerClient extends JMailerClient { | |
*/ | ||
def send(data: Email): String | ||
|
||
/** | ||
* Configure the underlying instance of mailer. | ||
* | ||
* @param configuration configuration | ||
* @return the mailer client | ||
*/ | ||
def configure(configuration: Configuration): MailerClient | ||
|
||
override def configure(configuration: JConfiguration): JMailerClient = { | ||
configure(Configuration(configuration.underlying())) | ||
} | ||
|
||
override def send(data: JEmail): String = { | ||
val email = convert(data) | ||
send(email) | ||
|
@@ -97,16 +90,13 @@ trait MailerClient extends JMailerClient { | |
|
||
// Implementations | ||
|
||
class CommonsMailer @Inject()(configuration: Configuration) extends MailerClient { | ||
|
||
private val defaultConfig = PlayConfig(configuration).getDeprecatedWithFallback("play.mailer", "smtp") | ||
private lazy val mock = defaultConfig.get[Boolean]("mock") | ||
class SMTPMailer @Inject() (smtpConfiguration: SMTPConfiguration) extends MailerClient { | ||
|
||
private lazy val instance = { | ||
if (mock) { | ||
if (smtpConfiguration.mock) { | ||
new MockMailer() | ||
} else { | ||
new SMTPMailer(defaultConfig) { | ||
new CommonsMailer(smtpConfiguration) { | ||
override def send(email: MultiPartEmail): String = email.send() | ||
override def createMultiPartEmail(): MultiPartEmail = new MultiPartEmail() | ||
override def createHtmlEmail(): HtmlEmail = new HtmlEmail() | ||
|
@@ -117,13 +107,9 @@ class CommonsMailer @Inject()(configuration: Configuration) extends MailerClient | |
override def send(data: Email): String = { | ||
instance.send(data) | ||
} | ||
|
||
override def configure(configuration: Configuration) = { | ||
instance.configure(configuration) | ||
} | ||
} | ||
|
||
abstract class SMTPMailer(defaultConfig: PlayConfig, var config: Option[SMTPConfiguration] = None) extends MailerClient { | ||
abstract class CommonsMailer(conf: SMTPConfiguration) extends MailerClient { | ||
|
||
def send(email: MultiPartEmail): String | ||
|
||
|
@@ -133,13 +119,7 @@ abstract class SMTPMailer(defaultConfig: PlayConfig, var config: Option[SMTPConf | |
|
||
override def send(data: Email): String = send(createEmail(data)) | ||
|
||
override def configure(configuration: Configuration) = { | ||
config = Some(SMTPConfiguration(PlayConfig(Configuration.reference.getConfig("play.mailer").get ++ configuration))) | ||
this | ||
} | ||
|
||
def createEmail(data: Email): MultiPartEmail = { | ||
val conf = config.getOrElse(SMTPConfiguration(defaultConfig)) | ||
val email = createEmail(data.bodyText, data.bodyHtml, data.charset.getOrElse("utf-8")) | ||
email.setSubject(data.subject) | ||
setAddress(data.from) { (address, name) => email.setFrom(address, name) } | ||
|
@@ -201,18 +181,18 @@ abstract class SMTPMailer(defaultConfig: PlayConfig, var config: Option[SMTPConf | |
*/ | ||
private def createEmail(bodyText: Option[String], bodyHtml: Option[String], charset: String): MultiPartEmail = { | ||
(bodyHtml.filter(_.trim.nonEmpty), bodyText.filter(_.trim.nonEmpty)) match { | ||
case (Some(bodyHtml), bodyTextOpt) => | ||
case (Some(htmlMsg), bodyTextOpt) => | ||
val htmlEmail = createHtmlEmail() | ||
htmlEmail.setCharset(charset) | ||
htmlEmail.setHtmlMsg(bodyHtml) | ||
htmlEmail.setHtmlMsg(htmlMsg) | ||
bodyTextOpt.foreach { bodyText => | ||
htmlEmail.setTextMsg(bodyText) | ||
} | ||
htmlEmail | ||
case (None, Some(bodyText)) => | ||
case (None, Some(msg)) => | ||
val multiPartEmail = createMultiPartEmail() | ||
multiPartEmail.setCharset(charset) | ||
multiPartEmail.setMsg(bodyText) | ||
multiPartEmail.setMsg(msg) | ||
multiPartEmail | ||
case _ => | ||
createMultiPartEmail() | ||
|
@@ -254,8 +234,6 @@ class MockMailer @Inject() extends MailerClient { | |
email.headers.foreach(header => Logger.info(s"header: $header")) | ||
"" | ||
} | ||
|
||
override def configure(configuration: Configuration) = this | ||
} | ||
|
||
sealed trait Attachment | ||
|
@@ -288,23 +266,47 @@ case class SMTPConfiguration(host: String, | |
port: Int, | ||
ssl: Boolean = false, | ||
tls: Boolean = false, | ||
user: Option[String], | ||
password: Option[String], | ||
user: Option[String] = None, | ||
password: Option[String] = None, | ||
debugMode: Boolean = false, | ||
timeout: Option[Int] = None, | ||
connectionTimeout: Option[Int] = None) | ||
connectionTimeout: Option[Int] = None, | ||
mock: Boolean = false) | ||
|
||
object SMTPConfiguration { | ||
|
||
def apply(config: PlayConfig) = new SMTPConfiguration( | ||
config.getOptional[String]("host").getOrElse(throw new RuntimeException("host needs to be set in order to use this plugin (or set play.mailer.mock to true in application.conf)")), | ||
resolveHost(config), | ||
config.get[Int]("port"), | ||
config.get[Boolean]("ssl"), | ||
config.get[Boolean]("tls"), | ||
config.getOptional[String]("user"), | ||
config.getOptional[String]("password"), | ||
config.get[Boolean]("debug"), | ||
config.getOptional[Int]("timeout"), | ||
config.getOptional[Int]("connectiontimeout") | ||
config.getOptional[Int]("connectiontimeout"), | ||
config.get[Boolean]("mock") | ||
) | ||
|
||
def resolveHost(config: PlayConfig) = { | ||
if (config.get[Boolean]("mock")) { | ||
// host won't be used anyway... | ||
"" | ||
} else { | ||
config.getOptional[String]("host").getOrElse(throw new RuntimeException("host needs to be set in order to use this plugin (or set play.mailer.mock to true in application.conf)")) | ||
} | ||
} | ||
} | ||
|
||
class SMTPConfigurationProvider @Inject()(configuration: Configuration) extends Provider[SMTPConfiguration] { | ||
override def get() = { | ||
val config = PlayConfig(configuration).getDeprecatedWithFallback("play.mailer", "smtp") | ||
SMTPConfiguration(config) | ||
} | ||
} | ||
|
||
class MailerConfigurationModule extends Module { | ||
def bindings(environment: Environment, configuration: Configuration) = Seq( | ||
bind[SMTPConfiguration].toProvider[SMTPConfigurationProvider] | ||
) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, I think it adds a lot of flexibility.