chore: crazy ahh email template and styles

This commit is contained in:
csimonis 2025-05-08 15:39:02 +02:00 committed by Constantin Simonis
commit 13b3443127
2 changed files with 163 additions and 15 deletions

View file

@ -1,19 +1,26 @@
package de.szut.casino.security.service;
import de.szut.casino.user.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import java.util.List;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
@Service
public class EmailService {
private JavaMailSenderImpl mailSender;
private MailConfig mailConfig;
@Value("${app.frontend-host}")
private String feUrl;
public EmailService(JavaMailSenderImpl mailSender, MailConfig mailConfig) {
try {
@ -31,18 +38,30 @@ public class EmailService {
}
public void sendRegistrationEmail(UserEntity user) {
String body = "Hallo " + user.getUsername() + ",\n\nA Willkommen bei Trustworthy Casino©! Wir freuen uns, Sie an Bord zu haben.\n\n";
SimpleMailMessage mail = new SimpleMailMessage();
try {
String template = loadTemplate("email/registration.html");
String htmlContent = template
.replace("${username}", user.getUsername())
.replace("${feUrl}", feUrl);
mail.setFrom(mailConfig.fromAddress);
mail.setSubject("Willkommen bei Trustworthy Casino©");
mail.setTo(user.getEmail());
mail.setText(body);
sendEmail(mail);
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
helper.setFrom(mailConfig.fromAddress);
helper.setTo(user.getEmail());
helper.setSubject("Willkommen bei Trustworthy Casino©");
helper.setText(htmlContent, true);
mailSender.send(message);
} catch (MessagingException | IOException e) {
System.err.println("Failed to send registration email: " + e.getMessage());
}
}
private void sendEmail(SimpleMailMessage mail) {
this.mailSender.send(mail);
private String loadTemplate(String templatePath) throws IOException {
ClassPathResource resource = new ClassPathResource("templates/" + templatePath);
try (Reader reader = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)) {
return FileCopyUtils.copyToString(reader);
}
}
}