diff options
author | Anton Tananaev <anton@traccar.org> | 2023-03-16 09:55:19 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2023-03-16 09:55:19 -0700 |
commit | d4816aae87600334fe798e8b50b9f11df6f0b293 (patch) | |
tree | 1882a5335a653a63136b795df6661d988d33d3e7 | |
parent | 5c0962c257d51bef081d25405c972dd54035a0b2 (diff) | |
download | trackermap-server-d4816aae87600334fe798e8b50b9f11df6f0b293.tar.gz trackermap-server-d4816aae87600334fe798e8b50b9f11df6f0b293.tar.bz2 trackermap-server-d4816aae87600334fe798e8b50b9f11df6f0b293.zip |
Option for system only emails
7 files changed, 27 insertions, 14 deletions
diff --git a/src/main/java/org/traccar/api/resource/PasswordResource.java b/src/main/java/org/traccar/api/resource/PasswordResource.java index 2d87a8665..67c9cee97 100644 --- a/src/main/java/org/traccar/api/resource/PasswordResource.java +++ b/src/main/java/org/traccar/api/resource/PasswordResource.java @@ -63,7 +63,7 @@ public class PasswordResource extends BaseResource { if (user != null) { var velocityContext = textTemplateFormatter.prepareContext(permissionsService.getServer(), user); var fullMessage = textTemplateFormatter.formatMessage(velocityContext, "passwordReset", "full"); - mailManager.sendMessage(user, fullMessage.getSubject(), fullMessage.getBody()); + mailManager.sendMessage(user, true, fullMessage.getSubject(), fullMessage.getBody()); } return Response.ok().build(); } diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index 491c724aa..cb1ee63e1 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -836,6 +836,13 @@ public final class Keys { List.of(KeyType.CONFIG)); /** + * Restrict global SMTP configuration to system messages only (e.g. password reset). + */ + public static final ConfigKey<Boolean> MAIL_SMTP_SYSTEM_ONLY = new BooleanConfigKey( + "mail.smtp.systemOnly", + List.of(KeyType.CONFIG)); + + /** * Force SMTP settings from the config file and ignore user attributes. */ public static final ConfigKey<Boolean> MAIL_SMTP_IGNORE_USER_CONFIG = new BooleanConfigKey( diff --git a/src/main/java/org/traccar/mail/LogMailManager.java b/src/main/java/org/traccar/mail/LogMailManager.java index 4af68809c..5c7572b0c 100644 --- a/src/main/java/org/traccar/mail/LogMailManager.java +++ b/src/main/java/org/traccar/mail/LogMailManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 Anton Tananaev (anton@traccar.org) + * Copyright 2022 - 2023 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,12 +32,14 @@ public class LogMailManager implements MailManager { } @Override - public void sendMessage(User user, String subject, String body) throws MessagingException { - sendMessage(user, subject, body, null); + public void sendMessage( + User user, boolean system, String subject, String body) throws MessagingException { + sendMessage(user, system, subject, body, null); } @Override - public void sendMessage(User user, String subject, String body, MimeBodyPart attachment) throws MessagingException { + public void sendMessage( + User user, boolean system, String subject, String body, MimeBodyPart attachment) throws MessagingException { LOGGER.info( "Email sent\nTo: {}\nSubject: {}\nAttachment: {}\nBody:\n{}", user.getEmail(), subject, attachment != null ? attachment.getFileName() : null, body); diff --git a/src/main/java/org/traccar/mail/MailManager.java b/src/main/java/org/traccar/mail/MailManager.java index 69efbed32..3547fd37e 100644 --- a/src/main/java/org/traccar/mail/MailManager.java +++ b/src/main/java/org/traccar/mail/MailManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 Anton Tananaev (anton@traccar.org) + * Copyright 2022 - 2023 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,8 +24,10 @@ public interface MailManager { boolean getEmailEnabled(); - void sendMessage(User user, String subject, String body) throws MessagingException; + void sendMessage( + User user, boolean system, String subject, String body) throws MessagingException; - void sendMessage(User user, String subject, String body, MimeBodyPart attachment) throws MessagingException; + void sendMessage( + User user, boolean system, String subject, String body, MimeBodyPart attachment) throws MessagingException; } diff --git a/src/main/java/org/traccar/mail/SmtpMailManager.java b/src/main/java/org/traccar/mail/SmtpMailManager.java index 4a0b7048f..8d1afbd3e 100644 --- a/src/main/java/org/traccar/mail/SmtpMailManager.java +++ b/src/main/java/org/traccar/mail/SmtpMailManager.java @@ -93,19 +93,21 @@ public final class SmtpMailManager implements MailManager { return config.hasKey(Keys.MAIL_SMTP_HOST); } + @Override public void sendMessage( - User user, String subject, String body) throws MessagingException { - sendMessage(user, subject, body, null); + User user, boolean system, String subject, String body) throws MessagingException { + sendMessage(user, system, subject, body, null); } + @Override public void sendMessage( - User user, String subject, String body, MimeBodyPart attachment) throws MessagingException { + User user, boolean system, String subject, String body, MimeBodyPart attachment) throws MessagingException { Properties properties = null; if (!config.getBoolean(Keys.MAIL_SMTP_IGNORE_USER_CONFIG)) { properties = getProperties(new PropertiesProvider(user)); } - if (properties == null) { + if (properties == null && (system || !config.getBoolean(Keys.MAIL_SMTP_SYSTEM_ONLY))) { properties = getProperties(new PropertiesProvider(config)); } if (properties == null) { diff --git a/src/main/java/org/traccar/notificators/NotificatorMail.java b/src/main/java/org/traccar/notificators/NotificatorMail.java index 7f755f170..8818a5a75 100644 --- a/src/main/java/org/traccar/notificators/NotificatorMail.java +++ b/src/main/java/org/traccar/notificators/NotificatorMail.java @@ -44,7 +44,7 @@ public class NotificatorMail implements Notificator { public void send(Notification notification, User user, Event event, Position position) throws MessageException { try { var fullMessage = notificationFormatter.formatMessage(user, event, position, "full"); - mailManager.sendMessage(user, fullMessage.getSubject(), fullMessage.getBody()); + mailManager.sendMessage(user, false, fullMessage.getSubject(), fullMessage.getBody()); } catch (MessagingException e) { throw new MessageException(e); } diff --git a/src/main/java/org/traccar/reports/common/ReportMailer.java b/src/main/java/org/traccar/reports/common/ReportMailer.java index 221b35ae1..3ce41934f 100644 --- a/src/main/java/org/traccar/reports/common/ReportMailer.java +++ b/src/main/java/org/traccar/reports/common/ReportMailer.java @@ -55,7 +55,7 @@ public class ReportMailer { stream.toByteArray(), "application/octet-stream"))); User user = permissionsService.getUser(userId); - mailManager.sendMessage(user, "Report", "The report is in the attachment.", attachment); + mailManager.sendMessage(user, false, "Report", "The report is in the attachment.", attachment); } catch (StorageException | IOException | MessagingException e) { LOGGER.warn("Email report failed", e); } |