Skip to content

Commit

Permalink
recover from sending emails to malformed addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Oct 14, 2024
1 parent 3539837 commit 7bb7790
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions modules/mailer/src/main/Mailer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import play.api.libs.mailer.{ Email, SMTPConfiguration, SMTPMailer }
import scalalib.ThreadLocalRandom
import scalatags.Text.all.{ html as htmlTag, * }
import scalatags.Text.tags2.title as titleTag
import org.apache.commons.mail.EmailException

import scala.concurrent.blocking

Expand Down Expand Up @@ -39,14 +40,19 @@ final class Mailer(
Chronometer.syncMon(_.email.send.time):
blocking:
val (client, config) = randomClient()
client.send:
Email(
subject = msg.subject,
from = config.sender,
to = Seq(msg.to.value),
bodyText = msg.text.some,
bodyHtml = msg.htmlBody.map { body => Mailer.html.wrap(msg.subject, body).render }
)
val email = Email(
subject = msg.subject,
from = config.sender,
to = Seq(msg.to.value),
bodyText = msg.text.some,
bodyHtml = msg.htmlBody.map { body => Mailer.html.wrap(msg.subject, body).render }
)
client.send(email)
.recoverWith:
case e: EmailException if msg.to.normalize.value != msg.to.value =>
logger.warn(s"Email ${msg.to} is invalid, trying ${msg.to.normalize}")
send(msg.copy(to = msg.to.normalize.into(EmailAddress)))
.void

object Mailer:

Expand Down

0 comments on commit 7bb7790

Please sign in to comment.