DEV Community

Marcin Parśniak
Marcin Parśniak

Posted on

User Notification Settings - Finovara

Hello everyone!

In the latest update to Finovara,
I implemented user notification settings.
This feature allows users to decide whether they want to receive email notifications for important account events.

What this feature does

Users can now enable or disable notifications for important events happening in their account.

When notifications are enabled and the user:

  • changes their password
  • changes username
  • deleted account

the system will send an email notification to the address assigned to his account informing his of the change.

Example of mail sending method:

 @Async
    public void sendEmail(User user, String subject, String templatePath, String username, String email) {
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

            helper.setTo(user.getEmail());
            helper.setFrom("Finovara <" + senderAddress + ">");
            helper.setReplyTo(senderAddress);
            helper.setSubject(subject);

            String html = loadTemplate(templatePath, username, email);
            helper.setText(html, true);

            javaMailSender.send(message);

        } catch (Exception e) {
            throw new RuntimeException("Failed to send email", e);
        }
    }
Enter fullscreen mode Exit fullscreen mode

Example loadTemplate:

 private String loadTemplate(String templatePath, String username, String email) {
        try {
            ClassPathResource resource = new ClassPathResource(templatePath);
            try (InputStream inputStream = resource.getInputStream()) {
                String html = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);

                if (username != null) {
                    html = html.replace("{{username}}", username);
                }
                if (email != null) {
                    html = html.replace("{{email}}", email);
                }
                return html;
            }
        } catch (Exception e) {
            throw new RuntimeException("Failed to load email template", e);
        }
    }
Enter fullscreen mode Exit fullscreen mode

Thanks for reading!

I encourage you to take a look at the process of creating the Finovara application at: https://github.com/M4rc1nek/finovara-backend

Top comments (0)