diff --git a/backend/src/main/java/de/szut/casino/security/service/EmailService.java b/backend/src/main/java/de/szut/casino/security/service/EmailService.java index 31ad65b..9d39134 100644 --- a/backend/src/main/java/de/szut/casino/security/service/EmailService.java +++ b/backend/src/main/java/de/szut/casino/security/service/EmailService.java @@ -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); + } } } diff --git a/backend/src/main/resources/templates/email/registration.html b/backend/src/main/resources/templates/email/registration.html new file mode 100644 index 0000000..27fe15a --- /dev/null +++ b/backend/src/main/resources/templates/email/registration.html @@ -0,0 +1,129 @@ + + + + + + Willkommen bei Trustworthy Casino© + + + +
+
+

Trustworthy Casino©

+
+
+

Hallo ${username},

+ +

Herzlich willkommen bei Trustworthy Casino©! Wir freuen uns, Sie an Bord zu haben.

+ +
+ +

Bei uns erwarten Sie:

+ + +
+ +

Melden Sie sich jetzt an und beginnen Sie Ihr Spielerlebnis!

+ +
+ Jetzt Spielen +
+ +

Bei Fragen stehen wir Ihnen jederzeit zur Verfügung.

+ +

Mit freundlichen Grüßen,
+ Ihr Trustworthy Casino© Team

+
+ +
+ + \ No newline at end of file