Fix sending email when from address is empty string (#9126)

This commit is contained in:
Shrikant Sharat Kandula 2021-11-12 16:25:04 +05:30 committed by GitHub
parent a7b1902afd
commit 255b6bf940
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 17 deletions

View File

@ -1,20 +1,30 @@
package com.appsmith.server.configurations; package com.appsmith.server.configurations;
import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; 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.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import javax.mail.internet.InternetAddress;
import java.io.UnsupportedEncodingException;
@Getter @Getter
@Setter @Setter
@Configuration @Configuration
@Slf4j
public class EmailConfig { public class EmailConfig {
@Value("${mail.enabled}") @Value("${mail.enabled}")
private boolean emailEnabled = true; private boolean emailEnabled = true;
@Value("${mail.from}") @Setter(AccessLevel.NONE)
private String mailFrom; private InternetAddress mailFrom;
private static final String DEFAULT_MAIL_FROM = "appsmith@localhost";
@Value("${reply.to}") @Value("${reply.to}")
private String replyTo; private String replyTo;
@ -25,4 +35,23 @@ public class EmailConfig {
@Value("${mail.support}") @Value("${mail.support}")
private String supportEmailAddress; 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);
}
}
}
} }

View File

@ -28,15 +28,12 @@ public class EmailSender {
private final EmailConfig emailConfig; private final EmailConfig emailConfig;
private final InternetAddress MAIL_FROM;
private final InternetAddress REPLY_TO; private final InternetAddress REPLY_TO;
public EmailSender(JavaMailSender javaMailSender, EmailConfig emailConfig) { public EmailSender(JavaMailSender javaMailSender, EmailConfig emailConfig) {
this.javaMailSender = javaMailSender; this.javaMailSender = javaMailSender;
this.emailConfig = emailConfig; this.emailConfig = emailConfig;
MAIL_FROM = makeFromAddress();
REPLY_TO = makeReplyTo(); REPLY_TO = makeReplyTo();
} }
@ -96,8 +93,8 @@ public class EmailSender {
try { try {
helper.setTo(to); helper.setTo(to);
if (MAIL_FROM != null) { if (emailConfig.getMailFrom() != null) {
helper.setFrom(MAIL_FROM); helper.setFrom(emailConfig.getMailFrom());
} }
if (REPLY_TO != null) { if (REPLY_TO != null) {
helper.setReplyTo(REPLY_TO); 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() { private InternetAddress makeReplyTo() {
try { try {
return new InternetAddress(this.emailConfig.getReplyTo(), "Appsmith"); return new InternetAddress(this.emailConfig.getReplyTo(), "Appsmith");

View File

@ -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 # 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. # to send an email.
mail.enabled=${APPSMITH_MAIL_ENABLED:false} 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} mail.support=${APPSMITH_MAIL_SUPPORT:support@appsmith.com}
reply.to=${APPSMITH_REPLY_TO:appsmith@localhost} reply.to=${APPSMITH_REPLY_TO:appsmith@localhost}
spring.mail.host=${APPSMITH_MAIL_HOST:} spring.mail.host=${APPSMITH_MAIL_HOST:}