Application
Send email over SMTP, with templates and attachments.
Most applications send email: welcome messages, receipts, password resets. mailer sends through any SMTP server (your provider's, a company relay, a test server).
Set it up
Kest.run<AppModule> {
import(MailerModule.forRoot {
host = "smtp.example.com"; port = 587; startTls = true // port 587 with STARTTLS; use ssl = true with port 465
user = "apikey"; password = System.getenv("SMTP_PASSWORD") // never put secrets in code
from = "Shop <no-reply@shop.example>" // default sender
})
}Send a message
@Injectable
class Welcome(private val mailer: MailerService) {
fun send(user: User) {
mailer.send(Mail(
to = listOf(user.email), subject = "Welcome",
html = "<p>Hi ${user.name}</p>", text = "Hi ${user.name}", // send both: clients that cannot show HTML use the text
attachments = listOf(MailAttachment("guide.pdf", pdfBytes, "application/pdf")),
))
}
}Mail takes to, cc, bcc, from, replyTo, headers, text, html and attachments. send blocks until the SMTP server accepts the message and throws if it refuses (wrong password, rejected address), so an error is never silent.
Templates
Write the message once as a template and fill it with data. Put it in src/main/resources/templates/welcome.html, and an optional plain-text version in welcome.txt:
<p>Hello <b>{{name}}</b>!</p>
{{#perks}}<li>{{.}}</li>{{/perks}}mailer.sendView(listOf(user.email), "Welcome, {{name}}", "welcome", mapOf("name" to user.name, "perks" to listOf("free shipping", "early access")))- The subject is a template too.
- Values are HTML-escaped (a name like
<script>is shown as text, not run); use{{{raw}}}only for markup you trust. - A missing or broken template throws before anything is sent.
- Inline strings work with
sendTemplate(to, subject, htmlTemplate, values).
The default engine is Mustache over the classpath. To use FreeMarker or Thymeleaf, or another directory:
MailerModule.forRoot {
views = FreeMarkerEngine("mail") // mail/welcome.ftl
textViews = FreeMarkerEngine("mail", ".txt")
}Sending reliably
Sending blocks the caller, and the SMTP server can be slow or down. Do not make a user wait for it, and do not lose the message:
@Processor("emails")
class EmailWorker(private val mailer: MailerService) {
@Process("welcome") fun send(job: WelcomeEmail) { mailer.sendView(listOf(job.to), "Welcome", "welcome", mapOf("name" to job.name)) } // retried by the queue
}
// in the request:
queues.queue("emails").add("welcome", WelcomeEmail(user.email, user.name), JobOptions(attempts = 3, backoff = Duration.ofSeconds(5), exponentialBackoff = true))A queue retries failures with backoff. Write the job in the same database transaction with the outbox if "the order exists, so the mail must be sent" must be guaranteed.
In development and tests
Use a catch-all test server (Mailpit, MailHog): it accepts every message and shows it in a web page, and sends nothing out. The framework's own tests do exactly this and read the messages back through its API.
Tested
Against a real SMTP server (Mailpit): HTML with attachment, and a templated message with a loop and escaping, found through the server's API.
Not built
A built-in queue with retries (use a queue job as above), inline images, DKIM signing (your SMTP provider usually signs), and transport pooling.