This commit is contained in:
@@ -19,6 +19,7 @@ import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.SecureRandom;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@@ -47,20 +48,34 @@ public class EmailService {
|
||||
@Value("${server.front.port}")
|
||||
private String frontPort;
|
||||
|
||||
public void sendVerificationEmail(User user, String code) throws MessagingException, IOException {
|
||||
public void sendEmail(String from, String sendTo, String subject, String body) throws MessagingException {
|
||||
MimeMessage message = mailSender.createMimeMessage();
|
||||
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
|
||||
|
||||
helper.setFrom("noreply@no-copy.ru");
|
||||
helper.setTo(user.getEmail());
|
||||
helper.setSubject(standartSubject);
|
||||
String htmlContent = loadAndProcessTemplate(user.getFullName(), code,
|
||||
"templates/email-verification.html");
|
||||
helper.setText(htmlContent, true);
|
||||
helper.setFrom(from);
|
||||
helper.setTo(sendTo);
|
||||
helper.setSubject(subject);
|
||||
helper.setText(body, true);
|
||||
|
||||
mailSender.send(message);
|
||||
}
|
||||
|
||||
public void sendVerificationEmail(User user, String code) throws MessagingException, IOException {
|
||||
String htmlContent = loadAndProcessTemplate(Map.of("username", user.getFullName(), "code", code),
|
||||
"templates/email-verification.html");
|
||||
sendEmail("noreply@no-copy.ru", user.getEmail(), standartAdressFrom, htmlContent);
|
||||
}
|
||||
|
||||
public void sendTokensNotFoundEmail(User user, Integer currentTokens, Integer missingTokens)
|
||||
throws MessagingException, IOException {
|
||||
String htmlContent = loadAndProcessTemplate(Map.of("username", user.getFullName(),
|
||||
"current_tokens", String.valueOf(currentTokens), "missing_tokens",
|
||||
String.valueOf(missingTokens), "link_to_token_board", baseUrl + "/pages/payment"),
|
||||
"templates/token-not-found.html");
|
||||
|
||||
sendEmail("noreply@no-copy.ru", user.getEmail(), standartAdressFrom, htmlContent);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public EmailVerificationToken createEmailVerificationToken(AuthToken authToken) {
|
||||
EmailVerificationToken existingToken = emailVerificationTokenRepository
|
||||
@@ -96,6 +111,18 @@ public class EmailService {
|
||||
.replace("{{code}}", safeCode);
|
||||
}
|
||||
|
||||
private String loadAndProcessTemplate(Map<String, String> fieldsForTemplate, String templatePath) throws IOException {
|
||||
ClassPathResource resource = new ClassPathResource(templatePath);
|
||||
String template = new String(resource.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
|
||||
|
||||
for (Map.Entry<String, String> entry : fieldsForTemplate.entrySet()) {
|
||||
template = template.replace("{{" + entry.getKey() + "}}", escapeHtml(entry.getValue()));
|
||||
}
|
||||
|
||||
return template;
|
||||
}
|
||||
|
||||
|
||||
private String escapeHtml(String text) {
|
||||
if (text == null) return "";
|
||||
return text
|
||||
|
||||
Reference in New Issue
Block a user