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:
Arpit Mohan 2020-07-15 15:47:33 +05:30 committed by GitHub
parent 2956f1b3d0
commit c15f83dbc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 10 deletions

View File

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

View File

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

View File

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

View File

@ -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=