Note
|
For Play 2.3.x please read the README on the 2.3.x branch. |
-
Add a library dependency on play-mailer:
"com.typesafe.play" %% "play-mailer" % "{version}"
See below for the version matrix.
-
Configure the mailer in your
conf/application.conf
:play.mailer { host (mandatory) port (defaults to 25) ssl (defaults to no) tls (defaults to no) user (optional) password (optional) debug (defaults to no, to take effect you also need to set the log level to "DEBUG" for the application logger) timeout (defaults to 60s) connectiontimeout (defaults to 60s) mock (defaults to no, will only log all the email properties instead of sending an email) }
import play.libs.mailer.Email;
import play.libs.mailer.MailerClient;
import javax.inject.Inject;
import java.io.File;
import org.apache.commons.mail.EmailAttachment;
public class MyComponent {
@Inject MailerClient mailerClient;
public void sendEmail() {
Email email = new Email();
email.setSubject("Simple email");
email.setFrom("Mister FROM <[email protected]>");
email.addTo("Miss TO <[email protected]>");
// adds attachment
email.addAttachment("attachment.pdf", new File("/some/path/attachment.pdf"));
// adds inline attachment from byte array
email.addAttachment("data.txt", "data".getBytes(), "text/plain", "Simple data", EmailAttachment.INLINE);
// sends text, HTML or both...
email.setBodyText("A text message");
email.setBodyHtml("<html><body><p>An <b>html</b> message</p></body></html>");
mailerClient.send(email);
}
}
import play.api.libs.mailer._
import javax.inject.Inject
import java.io.File
import org.apache.commons.mail.EmailAttachment
class MyComponent @Inject() (mailerClient: MailerClient) {
def sendEmail {
val email = Email(
"Simple email",
"Mister FROM <[email protected]>",
Seq("Miss TO <[email protected]>"),
// adds attachment
attachments = Seq(
AttachmentFile("attachment.pdf", new File("/some/path/attachment.pdf")),
// adds inline attachment from byte array
AttachmentData("data.txt", "data".getBytes, "text/plain", Some("Simple data"), Some(EmailAttachment.INLINE))
),
// sends text, HTML or both...
bodyText = Some("A text message"),
bodyHtml = Some("<html><body><p>An <b>html</b> message</p></body></html>")
)
mailerClient.send(email)
}
}
The Play Mailer plugin supports several different versions of Play.
Plugin version | Play version |
---|---|
2.x |
2.3.x |
3.x |
2.4.x |
For Play 2.3:
"com.typesafe.play" %% "play-mailer" % "2.4.1"
For Play 2.4:
"com.typesafe.play" %% "play-mailer" % "3.0.1"
This software is licensed under the Apache 2 license, quoted below.
Copyright 2012 Typesafe (http://www.typesafe.com).
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.