From 251579fa403d25c82f418ecb5961282b0391d780 Mon Sep 17 00:00:00 2001 From: David Francoeur Date: Mon, 2 Dec 2024 23:15:38 -0500 Subject: [PATCH 1/4] feat: retrieve the message-id from the message sent BREAKING --- courier/src/main/scala/mailer.scala | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/courier/src/main/scala/mailer.scala b/courier/src/main/scala/mailer.scala index d275f4b..7cb4524 100644 --- a/courier/src/main/scala/mailer.scala +++ b/courier/src/main/scala/mailer.scala @@ -15,7 +15,7 @@ case class Mailer(_session: MailSession = Defaults.session, mimeMessageFactory: MailSession => MimeMessage = Defaults.mimeMessageFactory) { def session = Session.Builder(this) - def apply(e: Envelope)(implicit ec: ExecutionContext): Future[Unit] = { + def apply(e: Envelope)(implicit ec: ExecutionContext): Future[Option[String]] = { val msg = mimeMessageFactory(_session) e.subject.foreach { @@ -37,7 +37,9 @@ case class Mailer(_session: MailSession = Defaults.session, } Future { - Transport.send(msg) + // sending the email sets the message id + val _ = Transport.send(msg) + Option(msg.getMessageID()) } } } From 5fe4dd9c740ec14659c4e5512da5262ce7cf18cc Mon Sep 17 00:00:00 2001 From: David Francoeur Date: Mon, 2 Dec 2024 23:16:00 -0500 Subject: [PATCH 2/4] tests: add integration test --- build.sbt | 11 ++++++++- .../src/test/scala/mailspec.scala | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 integration-tests/src/test/scala/mailspec.scala diff --git a/build.sbt b/build.sbt index 2ce03eb..222661f 100644 --- a/build.sbt +++ b/build.sbt @@ -105,4 +105,13 @@ lazy val courier = (project in file("courier")) "org.jvnet.mock-javamail" % "mock-javamail" % "1.9" % Test ), testFrameworks += new TestFramework("munit.Framework") - ) \ No newline at end of file + ) +lazy val it = (project in file("integration-tests")) + .settings(commonSettings ++ cFlags) + .settings( + name := "courier-integration-tests", + libraryDependencies ++= Seq( + "org.scalameta" %% "munit" % "0.7.27" % Test, + ), + testFrameworks += new TestFramework("munit.Framework") + ).dependsOn(courier) \ No newline at end of file diff --git a/integration-tests/src/test/scala/mailspec.scala b/integration-tests/src/test/scala/mailspec.scala new file mode 100644 index 0000000..4cecc9c --- /dev/null +++ b/integration-tests/src/test/scala/mailspec.scala @@ -0,0 +1,23 @@ +package courier + +import scala.concurrent.Await +import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent.duration._ + + +class MailSpec extends munit.FunSuite { + test("the mailer should send an email") { + assume(sys.env("CI").isEmpty(), "This test is meant to be ran locally.") + val email = sys.env("IT_EMAIL") + val password = sys.env("IT_PASSWORD") + val mailer = Mailer("smtp.gmail.com", 587) + .auth(true) + .as(email, password) + .startTls(true)() + val mId = Await.result(mailer(Envelope.from(email.addr) + .to(email.addr) + .subject("miss you") + .content(Text("hi mom"))), 10.seconds) + assert(mId.nonEmpty, "Message-ID should be set by the Transport.send call") + } +} \ No newline at end of file From 4770edcab6eb3f524c2332fdd0f4e50c86ef842c Mon Sep 17 00:00:00 2001 From: David Francoeur Date: Mon, 2 Dec 2024 23:16:37 -0500 Subject: [PATCH 3/4] chore: load .env --- .envrc | 1 + .gitignore | 2 ++ 2 files changed, 3 insertions(+) create mode 100644 .envrc diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..fe7c01a --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +dotenv diff --git a/.gitignore b/.gitignore index 54e1aae..c707f1c 100644 --- a/.gitignore +++ b/.gitignore @@ -48,3 +48,5 @@ worker*.log project/.gnupg/ .bloop/ .metals/ +.bsp/ +.env From 523206930a508c6c36a989001236e472c45ba634 Mon Sep 17 00:00:00 2001 From: David Francoeur Date: Mon, 2 Dec 2024 23:23:31 -0500 Subject: [PATCH 4/4] tests: add a note on how to generate password --- integration-tests/src/test/scala/mailspec.scala | 1 + 1 file changed, 1 insertion(+) diff --git a/integration-tests/src/test/scala/mailspec.scala b/integration-tests/src/test/scala/mailspec.scala index 4cecc9c..720d38d 100644 --- a/integration-tests/src/test/scala/mailspec.scala +++ b/integration-tests/src/test/scala/mailspec.scala @@ -6,6 +6,7 @@ import scala.concurrent.duration._ class MailSpec extends munit.FunSuite { + // create a gmail app password https://myaccount.google.com/apppasswords test("the mailer should send an email") { assume(sys.env("CI").isEmpty(), "This test is meant to be ran locally.") val email = sys.env("IT_EMAIL")