Skip to content

Commit

Permalink
Move attachment support to SimpleEmailBuilder, add inline support to it
Browse files Browse the repository at this point in the history
  • Loading branch information
ttoino committed Jun 28, 2023
1 parent 65d97f7 commit 8c9a313
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package pt.up.fe.ni.website.backend.email

import jakarta.activation.DataSource
import jakarta.activation.FileDataSource
import jakarta.activation.URLDataSource
import jakarta.validation.Valid
import jakarta.validation.constraints.Email
import java.io.File
import java.net.URL
import org.springframework.mail.javamail.MimeMessageHelper
import pt.up.fe.ni.website.backend.config.email.EmailConfigProperties
import pt.up.fe.ni.website.backend.model.Account
Expand All @@ -17,7 +12,6 @@ abstract class BaseEmailBuilder : EmailBuilder {
private var to: MutableSet<String> = mutableSetOf()
private var cc: MutableSet<String> = mutableSetOf()
private var bcc: MutableSet<String> = mutableSetOf()
private var attachments: MutableList<Attachment> = mutableListOf()

fun from(@Email email: String, personal: String = email) = apply {
from = email
Expand Down Expand Up @@ -48,30 +42,11 @@ abstract class BaseEmailBuilder : EmailBuilder {
bcc.addAll(users.map { it.email })
}

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

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

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

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

to.forEach(helper::setTo)
cc.forEach(helper::setCc)
bcc.forEach(helper::setBcc)

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

protected data class Attachment(
val name: String,
val content: DataSource
)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
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
import pt.up.fe.ni.website.backend.config.email.EmailConfigProperties

class SimpleEmailBuilder : BaseEmailBuilder() {
private var text: String? = null
private var html: String? = null
private var subject: String? = null
private var attachments: MutableList<EmailFile> = mutableListOf()
private var inlines: MutableList<EmailFile> = mutableListOf()

fun text(text: String) = apply {
this.text = text
Expand All @@ -20,6 +27,30 @@ class SimpleEmailBuilder : BaseEmailBuilder() {
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, emailConfigProperties: EmailConfigProperties) {
super.build(helper, emailConfigProperties)

Expand All @@ -34,5 +65,13 @@ class SimpleEmailBuilder : BaseEmailBuilder() {
if (subject != null) {
helper.setSubject(subject!!)
}

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
)
}

0 comments on commit 8c9a313

Please sign in to comment.