forked from playframework/play-mailer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to programmatically configure the mailer client
Resolves playframework#51
- Loading branch information
1 parent
b44494a
commit f2f4250
Showing
8 changed files
with
145 additions
and
48 deletions.
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,6 +1,7 @@ | ||
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; | ||
|
@@ -9,6 +10,8 @@ | |
|
||
import javax.inject.Inject; | ||
import java.io.File; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ApplicationJava extends Controller { | ||
|
||
|
@@ -31,5 +34,16 @@ public Result send() { | |
String id = mailer.send(email); | ||
return ok("Email " + id + " sent!"); | ||
} | ||
|
||
|
||
public Result configureAndSend() { | ||
final Email email = new Email(); | ||
email.setSubject("Simple email"); | ||
email.setFrom("[email protected]"); | ||
email.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,6 +4,7 @@ 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} | ||
|
@@ -25,4 +26,10 @@ class ApplicationScala @Inject()(mailer: MailerClient) extends Controller { | |
val id = mailer.send(email) | ||
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) | ||
Ok(s"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
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 |
---|---|---|
|
@@ -5,13 +5,15 @@ import javax.mail.Part | |
|
||
import org.apache.commons.mail.{EmailConstants, HtmlEmail, MultiPartEmail} | ||
import org.specs2.mutable._ | ||
import play.api.{PlayConfig, Configuration} | ||
import play.api.test._ | ||
|
||
class MailerPluginSpec extends Specification { | ||
|
||
object SimpleMailerClient extends MailerClient { | ||
override def send(data: Email): String = "" | ||
override def convert(data: play.libs.mailer.Email) = super.convert(data) | ||
override def configure(configuration: Configuration): MailerClient = this | ||
} | ||
class MockMultiPartEmail extends MultiPartEmail { | ||
override def getPrimaryBodyPart = super.getPrimaryBodyPart | ||
|
@@ -26,7 +28,7 @@ class MailerPluginSpec extends Specification { | |
object MockSMTPMailer extends MockSMTPMailerWithTimeouts(None, None) | ||
|
||
class MockSMTPMailerWithTimeouts(smtpTimeout: Option[Int], smtpConnectionTimeout: Option[Int]) | ||
extends SMTPMailer("typesafe.org", 1234, true, false, Some("user"), Some("password"), false, smtpTimeout, smtpConnectionTimeout) { | ||
extends SMTPMailer(PlayConfig(Configuration.empty), Some(SMTPConfiguration("typesafe.org", 1234, ssl = true, tls = false, Some("user"), Some("password"), debugMode = false, smtpTimeout, smtpConnectionTimeout))) { | ||
override def send(email: MultiPartEmail) = "" | ||
override def createMultiPartEmail(): MultiPartEmail = new MockMultiPartEmail | ||
override def createHtmlEmail(): HtmlEmail = new MockHtmlEmail | ||
|
@@ -47,6 +49,27 @@ class MailerPluginSpec extends Specification { | |
email.getMailSession.getProperty("mail.debug") mustEqual "false" | ||
} | ||
|
||
"reconfigure SMTP" in { | ||
val mailer = MockSMTPMailer | ||
mailer.configure(Configuration.from(Map( | ||
"host" -> "playframework.com", | ||
"port" -> 5678, | ||
"ssl" -> false, | ||
"tls" -> true | ||
))) | ||
val email = mailer.createEmail(Email( | ||
subject = "Subject", | ||
from = "James Roper <[email protected]>" | ||
)) | ||
email.getSmtpPort mustEqual "5678" | ||
// Default value | ||
email.getSslSmtpPort mustEqual "465" | ||
email.getMailSession.getProperty("mail.smtp.auth") must beNull | ||
email.getMailSession.getProperty("mail.smtp.host") mustEqual "playframework.com" | ||
email.getMailSession.getProperty("mail.smtp.starttls.enable") mustEqual "true" | ||
email.getMailSession.getProperty("mail.debug") mustEqual "false" | ||
} | ||
|
||
"configure the SMTP timeouts if configured" in { | ||
val mailer = new MockSMTPMailerWithTimeouts(Some(10), Some(99)) | ||
val email = mailer.createEmail(Email( | ||
|
@@ -208,11 +231,11 @@ class MailerPluginSpec extends Specification { | |
convert.headers.size mustEqual 1 | ||
convert.headers.head mustEqual ("key", "value") | ||
convert.attachments.size mustEqual 2 | ||
convert.attachments(0) must beAnInstanceOf[AttachmentFile] | ||
convert.attachments(0).asInstanceOf[AttachmentFile].name mustEqual "play icon" | ||
convert.attachments(0).asInstanceOf[AttachmentFile].file mustEqual getPlayIcon | ||
convert.attachments(0).asInstanceOf[AttachmentFile].description mustEqual Some("A beautiful icon") | ||
convert.attachments(0).asInstanceOf[AttachmentFile].disposition mustEqual Some(Part.ATTACHMENT) | ||
convert.attachments.head must beAnInstanceOf[AttachmentFile] | ||
convert.attachments.head.asInstanceOf[AttachmentFile].name mustEqual "play icon" | ||
convert.attachments.head.asInstanceOf[AttachmentFile].file mustEqual getPlayIcon | ||
convert.attachments.head.asInstanceOf[AttachmentFile].description mustEqual Some("A beautiful icon") | ||
convert.attachments.head.asInstanceOf[AttachmentFile].disposition mustEqual Some(Part.ATTACHMENT) | ||
convert.attachments(1) must beAnInstanceOf[AttachmentData] | ||
convert.attachments(1).asInstanceOf[AttachmentData].name mustEqual "data.txt" | ||
convert.attachments(1).asInstanceOf[AttachmentData].data mustEqual "data".getBytes | ||
|