Skip to content

Commit

Permalink
Implement "ReplyTo" for Inkjet
Browse files Browse the repository at this point in the history
  • Loading branch information
guillermocalvo committed Jul 21, 2023
1 parent fbec524 commit acae562
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions email-mailjet/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ dependencies {
implementation(mnValidation.micronaut.validation)
testImplementation(projects.testSuiteUtils)
testImplementation(mn.micronaut.http)
testImplementation(mn.jackson.databind)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,11 +44,22 @@
*/
@Singleton
public class MailjetEmailComposer implements EmailComposer<MailjetRequest> {

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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit acae562

Please sign in to comment.