Skip to content

Commit

Permalink
Merge pull request #6 from aboutbits/add-reply-to
Browse files Browse the repository at this point in the history
add replyTo feature
  • Loading branch information
SirCotare authored May 17, 2024
2 parents 4b3e1da + 58e0d14 commit 81017ab
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,6 +16,11 @@ public record EmailDto(
String fromAddress,
String fromName,

@Nullable
String replyToAddress,
@Nullable
String replyToName,

List<String> recipients,

String textBody,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,6 +41,11 @@ public record Email(
@NotBlank
String fromName,

@Nullable
String replyToAddress,
@Nullable
String replyToName,

@Singular
@NonNull
Set<Attachment> attachments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
(
Expand Down Expand Up @@ -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!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,7 +39,8 @@ public ManageEmail(
EmailRepository emailRepository,
JavaMailSender mailSender,
AttachmentDataSource attachmentDataSource,
final EmailMapper emailMapper) {
final EmailMapper emailMapper
) {

this.emailRepository = emailRepository;
this.mailSender = mailSender;
Expand Down Expand Up @@ -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<EmailAttachment>();
for (var attachment : parameter.email().attachments()) {
Expand All @@ -134,26 +138,49 @@ 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<String> recipients, String subject, String htmlBody, String plainTextBody, Set<EmailAttachment> attachments) throws MessagingException, IOException, AttachmentException {
@SuppressWarnings("checkstyle:ParameterNumber")
private void sendMail(
String fromAddress,
String fromName,
@Nullable
String replyToAddress,
@Nullable
String replyToName,
List<String> recipients,
String subject,
String htmlBody,
String plainTextBody,
Set<EmailAttachment> attachments
) throws MessagingException, IOException, AttachmentException {
var message = mailSender.createMimeMessage();
var helper = new MimeMessageHelper(message, true, "UTF-8");

helper.setFrom(fromAddress, fromName);
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> recipients;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -179,6 +187,8 @@ private static EmailParameter getValidParameterWithoutAttachment() {
.recipient("[email protected]")
.fromAddress("[email protected]")
.fromName("somebody")
.replyToAddress("[email protected]")
.replyToName("somebodyElse")
.build()
).build();
}
Expand All @@ -201,6 +211,8 @@ private static EmailParameter getValidParameterWithAttachment() {
)
.fromAddress("[email protected]")
.fromName("somebody")
.replyToAddress("[email protected]")
.replyToName("somebodyElse")
.build()
).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}

0 comments on commit 81017ab

Please sign in to comment.