Adding configuration variables for MAIL_FROM and REPLY_TO in EmailConfig (#97)
The MAIL_FROM & REPLY_TO addresses default to appsmith@localhost. This should be changed in the generated docker.env file during deployment to actual values. Most email providers will not send emails unless they originate from a valid sender ID.
This commit is contained in:
parent
2956f1b3d0
commit
c15f83dbc3
|
|
@ -13,4 +13,9 @@ public class EmailConfig {
|
|||
@Value("${mail.enabled}")
|
||||
private boolean emailEnabled = true;
|
||||
|
||||
@Value("${mail.from}")
|
||||
private String mailFrom;
|
||||
|
||||
@Value("${reply.to}")
|
||||
private String replyTo;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import org.springframework.mail.MailException;
|
|||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
import reactor.core.Exceptions;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
|
|
@ -30,7 +31,9 @@ public class EmailSender {
|
|||
|
||||
final EmailConfig emailConfig;
|
||||
|
||||
private static final InternetAddress MAIL_FROM = makeFromAddress();
|
||||
private final InternetAddress MAIL_FROM;
|
||||
|
||||
private final InternetAddress REPLY_TO;
|
||||
|
||||
public static final Pattern VALID_EMAIL_ADDRESS_REGEX =
|
||||
Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
|
||||
|
|
@ -38,6 +41,9 @@ public class EmailSender {
|
|||
public EmailSender(JavaMailSender javaMailSender, EmailConfig emailConfig) {
|
||||
this.javaMailSender = javaMailSender;
|
||||
this.emailConfig = emailConfig;
|
||||
|
||||
MAIL_FROM = makeFromAddress();
|
||||
REPLY_TO = makeReplyTo();
|
||||
}
|
||||
|
||||
private static boolean validateEmail(String emailStr) {
|
||||
|
|
@ -75,11 +81,6 @@ public class EmailSender {
|
|||
return;
|
||||
}
|
||||
|
||||
if (MAIL_FROM == null) {
|
||||
log.error("MAIL_FROM is null, no From address object to send an email. Not sending email '{}'.", subject);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if the email address is valid. It's possible for certain OAuth2 providers to not return the email ID
|
||||
if (to == null || !validateEmail(to)) {
|
||||
log.error("The email ID: {} is not valid. Not sending an email", to);
|
||||
|
|
@ -92,7 +93,12 @@ public class EmailSender {
|
|||
|
||||
try {
|
||||
helper.setTo(to);
|
||||
helper.setFrom(MAIL_FROM);
|
||||
if (MAIL_FROM != null) {
|
||||
helper.setFrom(MAIL_FROM);
|
||||
}
|
||||
if (REPLY_TO != null) {
|
||||
helper.setReplyTo(REPLY_TO);
|
||||
}
|
||||
helper.setSubject(subject);
|
||||
helper.setText(text, true);
|
||||
javaMailSender.send(mimeMessage);
|
||||
|
|
@ -120,11 +126,20 @@ public class EmailSender {
|
|||
return stringWriter.toString();
|
||||
}
|
||||
|
||||
private static InternetAddress makeFromAddress() {
|
||||
private InternetAddress makeFromAddress() {
|
||||
try {
|
||||
return new InternetAddress("hello@appsmith.com", "Appsmith");
|
||||
return new InternetAddress(this.emailConfig.getMailFrom(), "Appsmith");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
log.error("Encoding error creating Appsmith from address.", e);
|
||||
log.error("Encoding error creating Appsmith mail from address.", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private InternetAddress makeReplyTo() {
|
||||
try {
|
||||
return new InternetAddress(this.emailConfig.getReplyTo(), "Appsmith");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
log.error("Encoding error creating Appsmith reply to address.", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,6 +43,8 @@ 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}
|
||||
reply.to=${APPSMITH_REPLY_TO:appsmith@localhost}
|
||||
spring.mail.host=${APPSMITH_MAIL_HOST:}
|
||||
spring.mail.port=${APPSMITH_MAIL_PORT:}
|
||||
spring.mail.username=${APPSMITH_MAIL_USERNAME:}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,8 @@ cat > docker.env << EOF
|
|||
|
||||
# ***** Email **********
|
||||
APPSMITH_MAIL_ENABLED=false
|
||||
# APPSMITH_MAIL_FROM=
|
||||
# APPSMITH_REPLY_TO=
|
||||
# APPSMITH_MAIL_HOST=
|
||||
# APPSMITH_MAIL_PASSWORD=
|
||||
# APPSMITH_MAIL_PORT=
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user