From 58e0d14feebf2b70b55e64934fb6804eb89c3430 Mon Sep 17 00:00:00 2001 From: Andreas Hufler Date: Mon, 13 May 2024 08:29:55 +0200 Subject: [PATCH] add replyTo feature --- .../springboot/emailservice/lib/EmailDto.java | 7 +++ .../lib/application/EmailParameter.java | 6 +++ .../lib/application/EmailServiceMigrator.java | 10 +++- .../lib/application/ManageEmail.java | 47 +++++++++++++++---- .../emailservice/lib/model/Email.java | 6 +++ .../lib/application/ManageEmailTest.java | 12 +++++ .../database/factory/EmailFactory.java | 2 + 7 files changed, 78 insertions(+), 12 deletions(-) diff --git a/src/main/java/it/aboutbits/springboot/emailservice/lib/EmailDto.java b/src/main/java/it/aboutbits/springboot/emailservice/lib/EmailDto.java index 82b818b..d910922 100644 --- a/src/main/java/it/aboutbits/springboot/emailservice/lib/EmailDto.java +++ b/src/main/java/it/aboutbits/springboot/emailservice/lib/EmailDto.java @@ -1,5 +1,7 @@ package it.aboutbits.springboot.emailservice.lib; +import org.springframework.lang.Nullable; + import java.time.OffsetDateTime; import java.util.List; import java.util.Set; @@ -14,6 +16,11 @@ public record EmailDto( String fromAddress, String fromName, + @Nullable + String replyToAddress, + @Nullable + String replyToName, + List recipients, String textBody, diff --git a/src/main/java/it/aboutbits/springboot/emailservice/lib/application/EmailParameter.java b/src/main/java/it/aboutbits/springboot/emailservice/lib/application/EmailParameter.java index d6e1856..21cd008 100644 --- a/src/main/java/it/aboutbits/springboot/emailservice/lib/application/EmailParameter.java +++ b/src/main/java/it/aboutbits/springboot/emailservice/lib/application/EmailParameter.java @@ -5,6 +5,7 @@ import lombok.Builder; import lombok.NonNull; import lombok.Singular; +import org.springframework.lang.Nullable; import java.io.InputStream; import java.time.OffsetDateTime; @@ -40,6 +41,11 @@ public record Email( @NotBlank String fromName, + @Nullable + String replyToAddress, + @Nullable + String replyToName, + @Singular @NonNull Set attachments diff --git a/src/main/java/it/aboutbits/springboot/emailservice/lib/application/EmailServiceMigrator.java b/src/main/java/it/aboutbits/springboot/emailservice/lib/application/EmailServiceMigrator.java index 1223d40..07d6795 100644 --- a/src/main/java/it/aboutbits/springboot/emailservice/lib/application/EmailServiceMigrator.java +++ b/src/main/java/it/aboutbits/springboot/emailservice/lib/application/EmailServiceMigrator.java @@ -14,7 +14,9 @@ public EmailServiceMigrator(JdbcTemplate jdbcTemplate) { void migrate() { log.info("EmailService: running DB migrations..."); - jdbcTemplate.execute(""" + jdbcTemplate.execute( + //@formatter:off + """ create table if not exists email_service_emails ( @@ -63,7 +65,11 @@ updated_at timestamp with time zone default now() not null, create index if not exists email_service_email_attachments_email_id_index on email_service_email_attachments (email_id); - """); + alter table email_service_emails add column if not exists reply_to_address text; + alter table email_service_emails add column if not exists reply_to_name text; + """ + //@formatter:on + ); log.info("EmailService: migrations done!"); } diff --git a/src/main/java/it/aboutbits/springboot/emailservice/lib/application/ManageEmail.java b/src/main/java/it/aboutbits/springboot/emailservice/lib/application/ManageEmail.java index 6b0f224..1fe4c24 100644 --- a/src/main/java/it/aboutbits/springboot/emailservice/lib/application/ManageEmail.java +++ b/src/main/java/it/aboutbits/springboot/emailservice/lib/application/ManageEmail.java @@ -14,6 +14,7 @@ import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import org.springframework.core.io.ByteArrayResource; +import org.springframework.lang.Nullable; import org.springframework.mail.MailException; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; @@ -38,7 +39,8 @@ public ManageEmail( EmailRepository emailRepository, JavaMailSender mailSender, AttachmentDataSource attachmentDataSource, - final EmailMapper emailMapper) { + final EmailMapper emailMapper + ) { this.emailRepository = emailRepository; this.mailSender = mailSender; @@ -115,6 +117,8 @@ private Email fromParameter(EmailParameter parameter) throws AttachmentException email.setRecipients(emailData.recipients()); email.setFromAddress(emailData.fromAddress()); email.setFromName(emailData.fromName()); + email.setReplyToAddress(emailData.replyToAddress()); + email.setReplyToName(emailData.replyToName()); var attachments = new HashSet(); for (var attachment : parameter.email().attachments()) { @@ -134,19 +138,34 @@ private Email fromParameter(EmailParameter parameter) throws AttachmentException return email; } - private void sendMail(Email notification) throws MessagingException, IOException, AttachmentException { + private void sendMail(Email email) throws MessagingException, IOException, AttachmentException { sendMail( - notification.getFromAddress(), - notification.getFromName(), - notification.getRecipients(), - notification.getSubject(), - notification.getHtmlBody(), - notification.getTextBody(), - notification.getAttachments() + email.getFromAddress(), + email.getFromName(), + email.getReplyToAddress(), + email.getReplyToName(), + email.getRecipients(), + email.getSubject(), + email.getHtmlBody(), + email.getTextBody(), + email.getAttachments() ); } - private void sendMail(String fromAddress, String fromName, List recipients, String subject, String htmlBody, String plainTextBody, Set attachments) throws MessagingException, IOException, AttachmentException { + @SuppressWarnings("checkstyle:ParameterNumber") + private void sendMail( + String fromAddress, + String fromName, + @Nullable + String replyToAddress, + @Nullable + String replyToName, + List recipients, + String subject, + String htmlBody, + String plainTextBody, + Set attachments + ) throws MessagingException, IOException, AttachmentException { var message = mailSender.createMimeMessage(); var helper = new MimeMessageHelper(message, true, "UTF-8"); @@ -154,6 +173,14 @@ private void sendMail(String fromAddress, String fromName, List recipien helper.setTo(recipients.toArray(String[]::new)); helper.setSubject(subject); + if (replyToAddress != null) { + if (replyToName != null) { + helper.setReplyTo(replyToAddress, replyToName); + } else { + helper.setReplyTo(replyToAddress); + } + } + if (!htmlBody.isBlank()) { helper.setText(plainTextBody, htmlBody); } else { diff --git a/src/main/java/it/aboutbits/springboot/emailservice/lib/model/Email.java b/src/main/java/it/aboutbits/springboot/emailservice/lib/model/Email.java index a8ee7f9..20ea001 100644 --- a/src/main/java/it/aboutbits/springboot/emailservice/lib/model/Email.java +++ b/src/main/java/it/aboutbits/springboot/emailservice/lib/model/Email.java @@ -21,6 +21,7 @@ import org.hibernate.annotations.JdbcTypeCode; import org.hibernate.annotations.UpdateTimestamp; import org.hibernate.type.SqlTypes; +import org.springframework.lang.Nullable; import java.time.OffsetDateTime; import java.util.List; @@ -57,6 +58,11 @@ public class Email { private String fromAddress; private String fromName; + @Nullable + private String replyToAddress; + @Nullable + private String replyToName; + @JdbcTypeCode(SqlTypes.JSON) private List recipients; diff --git a/src/test/java/it/aboutbits/springboot/emailservice/lib/application/ManageEmailTest.java b/src/test/java/it/aboutbits/springboot/emailservice/lib/application/ManageEmailTest.java index 3cc1626..e91b1e6 100644 --- a/src/test/java/it/aboutbits/springboot/emailservice/lib/application/ManageEmailTest.java +++ b/src/test/java/it/aboutbits/springboot/emailservice/lib/application/ManageEmailTest.java @@ -56,6 +56,8 @@ void givenRequiredParameters_schedule_shouldCreateNewNotification() throws Email assertThat(result.subject()).isEqualTo(parameter.email().subject()); assertThat(result.fromAddress()).isEqualTo(parameter.email().fromAddress()); assertThat(result.fromName()).isEqualTo(parameter.email().fromName()); + assertThat(result.replyToAddress()).isEqualTo(parameter.email().replyToAddress()); + assertThat(result.replyToName()).isEqualTo(parameter.email().replyToName()); assertThat(result.recipients()).containsAll(parameter.email().recipients()); assertThat(result.textBody()).isEqualTo(parameter.email().textBody()); assertThat(result.htmlBody()).isEqualTo(parameter.email().htmlBody()); @@ -76,6 +78,8 @@ void givenRequiredParameterWithAttachedFiles_schedule_shouldCreateNewNotificatio assertThat(result.subject()).isEqualTo(parameter.email().subject()); assertThat(result.fromAddress()).isEqualTo(parameter.email().fromAddress()); assertThat(result.fromName()).isEqualTo(parameter.email().fromName()); + assertThat(result.replyToAddress()).isEqualTo(parameter.email().replyToAddress()); + assertThat(result.replyToName()).isEqualTo(parameter.email().replyToName()); assertThat(result.recipients()).containsAll(parameter.email().recipients()); assertThat(result.textBody()).isEqualTo(parameter.email().textBody()); assertThat(result.htmlBody()).isEqualTo(parameter.email().htmlBody()); @@ -94,6 +98,8 @@ void givenRequiredParameters_sendOrFail_shouldCreateNewNotification() throws Ema assertThat(result.subject()).isEqualTo(parameter.email().subject()); assertThat(result.fromAddress()).isEqualTo(parameter.email().fromAddress()); assertThat(result.fromName()).isEqualTo(parameter.email().fromName()); + assertThat(result.replyToAddress()).isEqualTo(parameter.email().replyToAddress()); + assertThat(result.replyToName()).isEqualTo(parameter.email().replyToName()); assertThat(result.recipients()).containsAll(parameter.email().recipients()); assertThat(result.textBody()).isEqualTo(parameter.email().textBody()); assertThat(result.htmlBody()).isEqualTo(parameter.email().htmlBody()); @@ -115,6 +121,8 @@ void givenRequiredParameterWithAttachedFiles_sendOrFail_shouldCreateNewNotificat assertThat(result.subject()).isEqualTo(parameter.email().subject()); assertThat(result.fromAddress()).isEqualTo(parameter.email().fromAddress()); assertThat(result.fromName()).isEqualTo(parameter.email().fromName()); + assertThat(result.replyToAddress()).isEqualTo(parameter.email().replyToAddress()); + assertThat(result.replyToName()).isEqualTo(parameter.email().replyToName()); assertThat(result.recipients()).containsAll(parameter.email().recipients()); assertThat(result.textBody()).isEqualTo(parameter.email().textBody()); assertThat(result.htmlBody()).isEqualTo(parameter.email().htmlBody()); @@ -179,6 +187,8 @@ private static EmailParameter getValidParameterWithoutAttachment() { .recipient("person2@example.com") .fromAddress("somebody@aboutbits.it") .fromName("somebody") + .replyToAddress("somebodyElse@aboutbits.it") + .replyToName("somebodyElse") .build() ).build(); } @@ -201,6 +211,8 @@ private static EmailParameter getValidParameterWithAttachment() { ) .fromAddress("somebody@aboutbits.it") .fromName("somebody") + .replyToAddress("somebodyElse@aboutbits.it") + .replyToName("somebodyElse") .build() ).build(); } diff --git a/src/test/java/it/aboutbits/springboot/emailservice/support/database/factory/EmailFactory.java b/src/test/java/it/aboutbits/springboot/emailservice/support/database/factory/EmailFactory.java index bb8566e..ef841d8 100644 --- a/src/test/java/it/aboutbits/springboot/emailservice/support/database/factory/EmailFactory.java +++ b/src/test/java/it/aboutbits/springboot/emailservice/support/database/factory/EmailFactory.java @@ -24,6 +24,8 @@ public static Email.EmailBuilder once() { .scheduledAt(OffsetDateTime.now()) .fromAddress(FAKER.internet().emailAddress()) .fromName(FAKER.name().fullName()) + .replyToAddress(FAKER.internet().emailAddress()) + .replyToName(FAKER.name().fullName()) .recipients(List.of(FAKER.internet().emailAddress(), FAKER.internet().emailAddress())); } }