Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add email service #123

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ dependencies {
implementation("com.cloudinary:cloudinary:1.0.14")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
implementation("org.springframework.boot:spring-boot-starter-validation:3.1.1")
implementation("org.springframework.boot:spring-boot-starter-mail:3.0.2")
implementation("org.springframework.boot:spring-boot-starter-mustache:3.0.2")
implementation("org.commonmark:commonmark:0.21.0")
implementation("org.commonmark:commonmark-ext-yaml-front-matter:0.21.0")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("com.h2database:h2")
testImplementation("org.springframework.boot:spring-boot-starter-test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.runApplication
import org.springframework.data.jpa.repository.config.EnableJpaAuditing
import pt.up.fe.ni.website.backend.config.auth.AuthConfigProperties
import pt.up.fe.ni.website.backend.config.email.EmailConfigProperties
import pt.up.fe.ni.website.backend.config.upload.UploadConfigProperties

@SpringBootApplication
@EnableConfigurationProperties(AuthConfigProperties::class, UploadConfigProperties::class)
@EnableConfigurationProperties(AuthConfigProperties::class, UploadConfigProperties::class, EmailConfigProperties::class)
@EnableJpaAuditing
class BackendApplication

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package pt.up.fe.ni.website.backend.config.email

import com.samskivert.mustache.Mustache
import org.commonmark.ext.front.matter.YamlFrontMatterExtension
import org.commonmark.parser.Parser
import org.commonmark.renderer.html.HtmlRenderer
import org.commonmark.renderer.text.TextContentRenderer
import org.springframework.boot.autoconfigure.mustache.MustacheResourceTemplateLoader
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class EmailConfig(
private val emailConfigProperties: EmailConfigProperties
) {
@Bean
fun mustacheCompiler() = Mustache.compiler().withLoader(
MustacheResourceTemplateLoader(emailConfigProperties.templatePrefix, emailConfigProperties.templateSuffix)
)

@Bean
fun commonmarkParser() = Parser.builder().extensions(
listOf(
YamlFrontMatterExtension.create()
)
).build()

@Bean
fun commonmarkHtmlRenderer() = HtmlRenderer.builder().build()

@Bean
fun commonmarkTextRenderer() = TextContentRenderer.builder().build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package pt.up.fe.ni.website.backend.config.email

import org.springframework.boot.context.properties.ConfigurationProperties

@ConfigurationProperties(prefix = "email")
data class EmailConfigProperties(
val from: String,
val fromPersonal: String = from,
ttoino marked this conversation as resolved.
Show resolved Hide resolved
val templatePrefix: String = "classpath:templates/email/",
val templateSuffix: String = ".mustache",
val defaultHtmlLayout: String = "layout.html",
val defaultStyle: String = "classpath:email/style.css"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package pt.up.fe.ni.website.backend.email

import jakarta.validation.Valid
import jakarta.validation.constraints.Email
import org.springframework.mail.javamail.MimeMessageHelper
import pt.up.fe.ni.website.backend.config.ApplicationContextUtils
import pt.up.fe.ni.website.backend.config.email.EmailConfigProperties
import pt.up.fe.ni.website.backend.model.Account

abstract class BaseEmailBuilder : EmailBuilder {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing the builders should be trivial with unit tests (testing validation, whether emails are added in the right places, etc.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One can also mock the java mail library and verify if the methods are being called with the correct arguments

protected open val emailConfigProperties = ApplicationContextUtils.getBean(EmailConfigProperties::class.java)

var from: String? = null
var fromPersonal: String? = null
var to: MutableSet<String> = mutableSetOf()
var cc: MutableSet<String> = mutableSetOf()
var bcc: MutableSet<String> = mutableSetOf()

fun from(@Email email: String, personal: String = email) = apply {
BrunoRosendo marked this conversation as resolved.
Show resolved Hide resolved
from = email
fromPersonal = personal
}

fun to(@Email vararg emails: String) = apply {
to.addAll(emails)
}

fun to(@Valid vararg users: Account) = apply {
to.addAll(users.map { it.email })
}

fun cc(@Email vararg emails: String) = apply {
cc.addAll(emails)
}

fun cc(@Valid vararg users: Account) = apply {
cc.addAll(users.map { it.email })
}

fun bcc(@Email vararg emails: String) = apply {
bcc.addAll(emails)
}

fun bcc(@Valid vararg users: Account) = apply {
bcc.addAll(users.map { it.email })
}

override fun build(helper: MimeMessageHelper) {
helper.setFrom(from ?: emailConfigProperties.from, fromPersonal ?: emailConfigProperties.fromPersonal)

to.forEach(helper::setTo)
cc.forEach(helper::setCc)
bcc.forEach(helper::setBcc)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package pt.up.fe.ni.website.backend.email

import org.springframework.mail.javamail.MimeMessageHelper

interface EmailBuilder {
fun build(helper: MimeMessageHelper)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package pt.up.fe.ni.website.backend.email

import jakarta.activation.DataSource
import jakarta.activation.FileDataSource
import jakarta.activation.URLDataSource
import java.io.File
import java.net.URL
import org.springframework.mail.javamail.MimeMessageHelper

class SimpleEmailBuilder : BaseEmailBuilder() {
private var text: String? = null
private var html: String? = null
private var subject: String? = null
private var attachments: MutableList<EmailFile> = mutableListOf()
// Inlines - similar to attachments, not shown as downloadable but can be inserted in an email. For example, inline images.
private var inlines: MutableList<EmailFile> = mutableListOf()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are inlines?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An inline is a file that is sent along with an email, similar to an attachment, except it doesn't show as a downloadable file and instead can be referenced from the email html using the cid: protocol. This is how inline images are implemented.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, nice. Maybe we should have a comment explaining that since I'm guessing a lot of people won't know that.


fun text(text: String) = apply {
this.text = text
}

fun html(html: String) = apply {
this.html = html
}

fun subject(subject: String) = apply {
this.subject = subject
}

fun attach(name: String, content: DataSource) = apply {
attachments.add(EmailFile(name, content))
}

fun attach(name: String, content: File) = apply {
attachments.add(EmailFile(name, FileDataSource(content)))
}

fun attach(name: String, path: String) = apply {
attachments.add(EmailFile(name, URLDataSource(URL(path))))
}

fun inline(name: String, content: DataSource) = apply {
inlines.add(EmailFile(name, content))
}

fun inline(name: String, content: File) = apply {
inlines.add(EmailFile(name, FileDataSource(content)))
}

fun inline(name: String, path: String) = apply {
inlines.add(EmailFile(name, URLDataSource(URL(path))))
}

override fun build(helper: MimeMessageHelper) {
super.build(helper)

when {
text != null && html != null -> helper.setText(text!!, html!!)
html != null -> helper.setText(html!!, true)
text != null -> helper.setText(text!!)
}

subject?.let { helper.setSubject(it) }

attachments.forEach { helper.addAttachment(it.name, it.content) }
inlines.forEach { helper.addInline(it.name, it.content) }
}

private data class EmailFile(
val name: String,
val content: DataSource
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package pt.up.fe.ni.website.backend.email

import com.samskivert.mustache.Mustache
import org.commonmark.ext.front.matter.YamlFrontMatterVisitor
import org.commonmark.parser.Parser
import org.commonmark.renderer.html.HtmlRenderer
import org.commonmark.renderer.text.TextContentRenderer
import org.springframework.core.io.Resource
import org.springframework.core.io.UrlResource
import org.springframework.mail.javamail.MimeMessageHelper
import org.springframework.util.ResourceUtils
import pt.up.fe.ni.website.backend.config.ApplicationContextUtils

abstract class TemplateEmailBuilder<T>(
private val template: String
) : BaseEmailBuilder() {
private val commonmarkParser = ApplicationContextUtils.getBean(Parser::class.java)
private val commonmarkHtmlRenderer = ApplicationContextUtils.getBean(HtmlRenderer::class.java)
private val commonmarkTextRenderer = ApplicationContextUtils.getBean(TextContentRenderer::class.java)
private val mustache = ApplicationContextUtils.getBean(Mustache.Compiler::class.java)

private var data: T? = null

fun data(data: T) = apply {
this.data = data
}

override fun build(helper: MimeMessageHelper) {
super.build(helper)

if (data == null) return

val markdown = mustache.loadTemplate(template).execute(data)

val doc = commonmarkParser.parse(markdown)
val htmlContent = commonmarkHtmlRenderer.render(doc)
val text = commonmarkTextRenderer.render(doc)

val yamlVisitor = YamlFrontMatterVisitor()
doc.accept(yamlVisitor)

val subject = yamlVisitor.data["subject"]?.firstOrNull()
subject?.let { helper.setSubject(it) }

val styles = yamlVisitor.data.getOrDefault("styles", mutableListOf()).apply {
if (yamlVisitor.data["no_default_style"].isNullOrEmpty()) {
this.add(emailConfigProperties.defaultStyle)
}
}.map {
ResourceUtils.getFile(it).readText()
}

val htmlTemplate = yamlVisitor.data["layout"]?.firstOrNull() ?: emailConfigProperties.defaultHtmlLayout
val html = mustache.loadTemplate(htmlTemplate).execute(
mapOf(
"subject" to subject,
"content" to htmlContent,
"styles" to styles
)
)

helper.setText(text, html)

yamlVisitor.data.getOrDefault("attachments", emptyList()).forEach { addFile(helper::addAttachment, it) }
yamlVisitor.data.getOrDefault("inline", emptyList()).forEach { addFile(helper::addInline, it) }
}

private fun addFile(fn: (String, Resource) -> Any, file: String) {
val split = file.split("\\s*::\\s*".toRegex(), 2)

if (split.isEmpty()) return

val name = split[0]
val path = split.getOrElse(1) { split[0] }

fn(name, UrlResource(path))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package pt.up.fe.ni.website.backend.service

import org.springframework.mail.javamail.JavaMailSender
import org.springframework.mail.javamail.MimeMessageHelper
import org.springframework.stereotype.Service
import pt.up.fe.ni.website.backend.email.EmailBuilder

@Service
class EmailService(
private val mailSender: JavaMailSender
) {
fun send(email: EmailBuilder) {
val message = mailSender.createMimeMessage()

val helper = MimeMessageHelper(message, true)
email.build(helper)

mailSender.send(message)
}
}
14 changes: 14 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,17 @@ upload.static-serve=http://localhost:3000/static

# Cors Origin
cors.allow-origin = http://localhost:3000

# Email config
spring.mail.host=
spring.mail.port=
spring.mail.username=
spring.mail.password=
Comment on lines +43 to +46
Copy link
Member

@BrunoRosendo BrunoRosendo Mar 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we need to configure this in prod? It'd be nice to have some example in the wiki if so

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I'll add a section on how to configure it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is it? I may be legally blind

spring.mail.properties[mail.smtp.auth]=true
spring.mail.properties[mail.smtp.starttls.enable]=true
spring.mail.properties[mail.smtp.connectiontimeout]=5000
spring.mail.properties[mail.smtp.timeout]=3000
spring.mail.properties[mail.smtp.writetimeout]=5000

[email protected]
email.from-personal=NIAEFEUP
3 changes: 3 additions & 0 deletions src/main/resources/email/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
TODO
*/
ttoino marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 20 additions & 0 deletions src/main/resources/templates/email/layout.html.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>{{{subject}}}</title>

{{#styles}}
<style>
{{.}}
</style>
{{/styles}}
</head>
<body>
<!-- TODO -->
ttoino marked this conversation as resolved.
Show resolved Hide resolved
{{{content}}}
</body>
</html>
Loading
Loading