Skip to content

Commit

Permalink
TRUNK-6153 - Email messages are not sent with correct from address (o…
Browse files Browse the repository at this point in the history
  • Loading branch information
mseaton committed Dec 9, 2022
1 parent b0b072b commit 823d28b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
5 changes: 4 additions & 1 deletion api/src/main/java/org/openmrs/api/impl/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -737,7 +738,9 @@ public User setUserActivationKey(User user) throws MessageException {
String link = adminService.getGlobalProperty(OpenmrsConstants.GP_PASSWORD_RESET_URL)
.replace("{activationKey}", token);

String sender = adminService.getGlobalProperty("mail.from");
Properties mailProperties = Context.getMailProperties();

String sender = mailProperties.getProperty("mail.from");

String subject = messages.getMessage("mail.passwordreset.subject",null, locale);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.Properties;

public class MailMessageSender implements MessageSender {

private static final Logger log = LoggerFactory.getLogger(MailMessageSender.class);
Expand Down Expand Up @@ -87,22 +89,25 @@ public MimeMessage createMimeMessage(Message message) throws Exception {
if (message.getRecipients() == null) {
throw new MessageException("Message must contain at least one recipient");
}

Properties mailProperties = Context.getMailProperties();

// set the content-type to the default if it isn't defined in Message
if (!StringUtils.hasText(message.getContentType())) {
String contentType = Context.getAdministrationService().getGlobalProperty("mail.default_content_type");
message.setContentType(StringUtils.hasText(contentType) ? contentType : "text/plain");
String contentType = mailProperties.getProperty("mail.default_content_type", "text/plain");
message.setContentType(contentType);
}

MimeMessage mimeMessage = new MimeMessage(session);

if (message.getSender() != null) {
mimeMessage.setSender(new InternetAddress(message.getSender()));
} else {
String defaultFromMailAddress = Context.getAdministrationService().getGlobalProperty("mail.from");
if (StringUtils.hasText(defaultFromMailAddress)) {
mimeMessage.setSender(new InternetAddress(defaultFromMailAddress));
}
String sender = message.getSender();
if (!StringUtils.hasText(sender)) {
sender = mailProperties.getProperty("mail.from");
}
if (StringUtils.hasText(sender)) {
InternetAddress senderAddress = new InternetAddress(sender);
mimeMessage.setFrom(senderAddress);
mimeMessage.setSender(senderAddress);
}

mimeMessage.setRecipients(javax.mail.Message.RecipientType.TO,
Expand Down

0 comments on commit 823d28b

Please sign in to comment.