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;
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);
}
}
}
}

View File

@ -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");

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
# 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:}