mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 08:58:48 +00:00
terminado sign up
This commit is contained in:
@ -4,12 +4,13 @@ import jakarta.mail.MessagingException;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||||
import org.thymeleaf.TemplateEngine;
|
||||
import org.thymeleaf.context.Context;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
@ -32,44 +33,67 @@ public class EmailService {
|
||||
Map<String, Object> variables = Map.of(
|
||||
"fullName", fullName,
|
||||
"resetUrl", resetUrl);
|
||||
sendEmail(to, subject, "imprimelibros/email/reset-password", variables);
|
||||
sendEmail(to, subject, "imprimelibros/email/reset-password", variables, locale);
|
||||
}
|
||||
|
||||
public void sendVerificationEmail(String to, String fullName, String verifyUrl, Locale locale) {
|
||||
String subject = messageSource.getMessage("email.verify.title", null, locale);
|
||||
Map<String, Object> variables = Map.of(
|
||||
"fullName", fullName,
|
||||
"verifyUrl", verifyUrl);
|
||||
sendEmail(to, subject, "imprimelibros/email/verify", variables);
|
||||
"verifyUrl", verifyUrl,
|
||||
"minutes", 60);
|
||||
sendEmail(to, subject, "imprimelibros/email/verify", variables, locale);
|
||||
}
|
||||
|
||||
|
||||
// ->>>>>>>> PRIVATE METHODS <<<<<<<<<<<-
|
||||
|
||||
/******************
|
||||
* Envía un email usando una plantilla Thymeleaf.
|
||||
*
|
||||
* @param to
|
||||
* @param subject
|
||||
* @param template
|
||||
* @param variables
|
||||
**********************************************/
|
||||
|
||||
private void sendEmail(String to, String subject, String template, Map<String, Object> variables) {
|
||||
private void sendEmail(String to, String subject, String template, Map<String, Object> variables, Locale locale) {
|
||||
try {
|
||||
Context context = new Context();
|
||||
context.setVariables(variables);
|
||||
String html = templateEngine.process(template, context);
|
||||
Context ctx = new Context(locale);
|
||||
ctx.setVariables(variables);
|
||||
ctx.setVariable("subject", subject);
|
||||
ctx.setVariable("companyName", "ImprimeLibros");
|
||||
ctx.setVariable("year", java.time.Year.now().getValue());
|
||||
|
||||
MimeMessage message = mailSender.createMimeMessage();
|
||||
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
|
||||
helper.setFrom("no-reply@imprimelibros.com");
|
||||
helper.setTo(to);
|
||||
helper.setSubject(subject);
|
||||
helper.setText(html, true);
|
||||
// Incrusta el CSS (no uses <link> en emails)
|
||||
var cssRes = new ClassPathResource("static/assets/css/email.css");
|
||||
String emailCss = org.springframework.util.StreamUtils.copyToString(cssRes.getInputStream(),
|
||||
java.nio.charset.StandardCharsets.UTF_8);
|
||||
ctx.setVariable("emailCss", emailCss);
|
||||
|
||||
mailSender.send(message);
|
||||
} catch (MessagingException e) {
|
||||
e.printStackTrace();
|
||||
ctx.setVariable("template", template);
|
||||
String html = templateEngine.process("imprimelibros/email/layout", ctx);
|
||||
|
||||
MimeMessage msg = mailSender.createMimeMessage();
|
||||
|
||||
MimeMessageHelper h = new MimeMessageHelper(
|
||||
msg,
|
||||
MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,
|
||||
"UTF-8");
|
||||
|
||||
h.setFrom("no-reply@imprimelibros.com");
|
||||
h.setTo(to);
|
||||
h.setSubject(subject);
|
||||
|
||||
h.setText(html, true);
|
||||
|
||||
// 3) ahora el inline, con content-type explícito
|
||||
ClassPathResource logoRes = new ClassPathResource("static/assets/images/logo-light.png");
|
||||
h.addInline("logo", logoRes, "image/png");
|
||||
|
||||
mailSender.send(msg);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Error enviando email", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user