diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/EmailConfig.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/EmailConfig.java index f600d442c4..91e12e1306 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/EmailConfig.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/EmailConfig.java @@ -1,20 +1,30 @@ package com.appsmith.server.configurations; +import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; +import org.springframework.util.StringUtils; + +import javax.mail.internet.InternetAddress; +import java.io.UnsupportedEncodingException; @Getter @Setter @Configuration +@Slf4j public class EmailConfig { @Value("${mail.enabled}") private boolean emailEnabled = true; - @Value("${mail.from}") - private String mailFrom; + @Setter(AccessLevel.NONE) + private InternetAddress mailFrom; + + private static final String DEFAULT_MAIL_FROM = "appsmith@localhost"; @Value("${reply.to}") private String replyTo; @@ -25,4 +35,23 @@ public class EmailConfig { @Value("${mail.support}") private String supportEmailAddress; + @Autowired + public void setMailFrom(@Value("${mail.from}") String value) { + if (!StringUtils.hasText(value)) { + value = DEFAULT_MAIL_FROM; + } + + try { + mailFrom = new InternetAddress(value, "Appsmith"); + } catch (UnsupportedEncodingException e) { + log.error("Encoding error creating Appsmith mail from address. Using default from address instead.", e); + try { + mailFrom = new InternetAddress(DEFAULT_MAIL_FROM, "Appsmith"); + } catch (UnsupportedEncodingException ignored) { + // We shouldn't see this error here with the default address parsing. + log.error("Encoding error with default from address. This should've never happened.", e); + } + } + } + } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/notifications/EmailSender.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/notifications/EmailSender.java index 51b9623f47..698cbb5cbd 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/notifications/EmailSender.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/notifications/EmailSender.java @@ -28,15 +28,12 @@ public class EmailSender { private final EmailConfig emailConfig; - private final InternetAddress MAIL_FROM; - private final InternetAddress REPLY_TO; public EmailSender(JavaMailSender javaMailSender, EmailConfig emailConfig) { this.javaMailSender = javaMailSender; this.emailConfig = emailConfig; - MAIL_FROM = makeFromAddress(); REPLY_TO = makeReplyTo(); } @@ -96,8 +93,8 @@ public class EmailSender { try { helper.setTo(to); - if (MAIL_FROM != null) { - helper.setFrom(MAIL_FROM); + if (emailConfig.getMailFrom() != null) { + helper.setFrom(emailConfig.getMailFrom()); } if (REPLY_TO != null) { helper.setReplyTo(REPLY_TO); @@ -114,15 +111,6 @@ public class EmailSender { } } - private InternetAddress makeFromAddress() { - try { - return new InternetAddress(this.emailConfig.getMailFrom(), "Appsmith"); - } catch (UnsupportedEncodingException e) { - log.error("Encoding error creating Appsmith mail from address.", e); - return null; - } - } - private InternetAddress makeReplyTo() { try { return new InternetAddress(this.emailConfig.getReplyTo(), "Appsmith"); diff --git a/app/server/appsmith-server/src/main/resources/application.properties b/app/server/appsmith-server/src/main/resources/application.properties index df608371fe..68a61cf586 100644 --- a/app/server/appsmith-server/src/main/resources/application.properties +++ b/app/server/appsmith-server/src/main/resources/application.properties @@ -58,7 +58,7 @@ spring.redis.url=${APPSMITH_REDIS_URL} # default localhost:25 SMTP server and throw an error. If false, this error won't happen because there's no attempt # to send an email. mail.enabled=${APPSMITH_MAIL_ENABLED:false} -mail.from=${APPSMITH_MAIL_FROM:appsmith@localhost} +mail.from=${APPSMITH_MAIL_FROM:} mail.support=${APPSMITH_MAIL_SUPPORT:support@appsmith.com} reply.to=${APPSMITH_REPLY_TO:appsmith@localhost} spring.mail.host=${APPSMITH_MAIL_HOST:}