diff --git a/email-mailjet/build.gradle.kts b/email-mailjet/build.gradle.kts index cff6cea4..48446222 100644 --- a/email-mailjet/build.gradle.kts +++ b/email-mailjet/build.gradle.kts @@ -10,4 +10,5 @@ dependencies { implementation(mnValidation.micronaut.validation) testImplementation(projects.testSuiteUtils) testImplementation(mn.micronaut.http) + testImplementation(mn.jackson.databind) } diff --git a/email-mailjet/src/main/java/io/micronaut/email/mailjet/MailjetEmailComposer.java b/email-mailjet/src/main/java/io/micronaut/email/mailjet/MailjetEmailComposer.java index e36c37ad..641e87bb 100644 --- a/email-mailjet/src/main/java/io/micronaut/email/mailjet/MailjetEmailComposer.java +++ b/email-mailjet/src/main/java/io/micronaut/email/mailjet/MailjetEmailComposer.java @@ -29,6 +29,8 @@ import jakarta.inject.Singleton; import org.json.JSONArray; import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import jakarta.validation.Valid; import jakarta.validation.constraints.NotNull; @@ -42,11 +44,22 @@ */ @Singleton public class MailjetEmailComposer implements EmailComposer { + + private static final Logger LOG = LoggerFactory.getLogger(MailjetEmailComposer.class); + @Override @NonNull public MailjetRequest compose(@NonNull @NotNull @Valid Email email) throws EmailException { JSONObject message = new JSONObject(); message.put(Emailv31.Message.FROM, createJsonObject(email.getFrom())); + if (CollectionUtils.isNotEmpty(email.getReplyToCollection())) { + if (email.getReplyToCollection().size() > 1) { + if (LOG.isWarnEnabled()) { + LOG.warn("Mailjet does not support multiple 'replyTo' addresses (Email has {} replyTo addresses)", email.getReplyToCollection().size()); + } + } + message.put(Emailv31.Message.REPLYTO, createJsonObject(CollectionUtils.last(email.getReplyToCollection()))); + } to(email).ifPresent(jsonArray -> message.put(Emailv31.Message.TO, jsonArray)); message.put(Emailv31.Message.SUBJECT, email.getSubject()); Body body = email.getBody(); diff --git a/email-mailjet/src/test/groovy/io/micronaut/email/mailjet/MailjetEmailComposerSpec.groovy b/email-mailjet/src/test/groovy/io/micronaut/email/mailjet/MailjetEmailComposerSpec.groovy new file mode 100644 index 00000000..48599f99 --- /dev/null +++ b/email-mailjet/src/test/groovy/io/micronaut/email/mailjet/MailjetEmailComposerSpec.groovy @@ -0,0 +1,44 @@ +package io.micronaut.email.mailjet + +import com.fasterxml.jackson.databind.json.JsonMapper +import com.mailjet.client.MailjetRequest +import io.micronaut.email.Email +import io.micronaut.test.extensions.spock.annotation.MicronautTest +import jakarta.inject.Inject +import spock.lang.Specification + +@MicronautTest(startApplication = false) +class MailjetEmailComposerSpec extends Specification { + + @Inject + MailjetEmailComposer mailjetEmailComposer + + void "from, to, only the last reply to and subject are put to the request"() { + given: + String from = "sender@example.com" + String to = "receiver@example.com" + String replyTo1 = "sender.reply.to.one@example.com" + String replyTo2 = "sender.reply.to.two@example.com" + String subject = "Apple Music" + String body = "Lore ipsum body" + + Email email = Email.builder() + .from(from) + .to(to) + .replyTo(replyTo1) + .replyTo(replyTo2) + .subject(subject) + .body(body) + .build() + when: + MailjetRequest request = mailjetEmailComposer.compose(email) + Map map = new JsonMapper().readValue(request.body, Map) + + then: + map["Messages"][0]["ReplyTo"]["Email"] == replyTo2 + map["Messages"][0]["TextPart"] == body + map["Messages"][0]["From"]["Email"] == from + map["Messages"][0]["To"][0]["Email"] == to + map["Messages"][0]["Subject"] == subject + } +}