Skip to content

Instantly share code, notes, and snippets.

@ggrossetie
Last active August 29, 2015 14:28
Show Gist options
  • Save ggrossetie/9e632d1b61f9fd5882d4 to your computer and use it in GitHub Desktop.
Save ggrossetie/9e632d1b61f9fd5882d4 to your computer and use it in GitHub Desktop.

Configure mailer instance

Runtime Dependency Injection

By default the plugin automatically configure the injected instances with the application.conf file:

class MyComponent @Inject() (mailer: MailerClient) {
  // ...
}

If you want to configure the injected instances from another source, you will need to override the default provider:

  1. Create a custom configuration provider:

    CustomSMTPConfigurationProvider.scala
    class CustomSMTPConfigurationProvider extends Provider[SMTPConfiguration] {
      override def get() {
        // Custom configuration
        new SMTPConfiguration("typesafe.org", 1234)
      }
    }
    
    class CustomMailerConfigurationModule extends Module {
      def bindings(environment: Environment, configuration: Configuration) = Seq(
        bind[SMTPConfiguration].toProvider[CustomSMTPConfigurationProvider]
      )
    }
  2. Override the default provider in the application.conf file:

    application.conf
    play {
      modules {
        # Disable the default provider
        disabled += "play.api.libs.mailer.SMTPConfigurationModule"
        # Enable the custom provider (see above)
        enabled += "controllers.CustomMailerConfigurationModule"
      }
    }

New instances

You can also use the SMTPMailer constructor to create new instances with custom configuration:

val email = Email("Simple email", "Mister FROM <[email protected]>")
new SMTPMailer(SMTPConfiguration("typesafe.org", 1234)).send(email)
new SMTPMailer(SMTPConfiguration("playframework.com", 5678)).send(email)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment