-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
alerter HTML email (template) (#126)
* added test code to debug sendmail * alerter html email and template * alerter html email and template
- Loading branch information
Showing
8 changed files
with
118 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
package it.gov.innovazione.ndc.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.context.event.ApplicationStartedEvent; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.mail.SimpleMailMessage; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.mail.javamail.MimeMessageHelper; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.mail.internet.MimeMessage; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
|
@@ -17,8 +19,6 @@ class EmailService { | |
private final JavaMailSender javaMailSender; | ||
@Value("${alerter.mail.sender}") | ||
private final String from; | ||
@Value("${spring.mail.properties.mail.debug:false}") | ||
private boolean mailDebug; | ||
|
||
void sendEmail(String to, String subject, String text) { | ||
SimpleMailMessage message = new SimpleMailMessage(); | ||
|
@@ -29,13 +29,14 @@ void sendEmail(String to, String subject, String text) { | |
javaMailSender.send(message); | ||
} | ||
|
||
@EventListener(ApplicationStartedEvent.class) | ||
void debugSendMail() { | ||
if (mailDebug) { | ||
log.info("Sending test email"); | ||
sendEmail("[email protected]", "Test", "Test"); | ||
log.info("Test email sent"); | ||
} | ||
@SneakyThrows | ||
void sendHtmlEmail(String to, String subject, String htmlMessage) { | ||
MimeMessage mimeMessage = javaMailSender.createMimeMessage(); | ||
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "utf-8"); | ||
helper.setText(htmlMessage, true); | ||
helper.setTo(to); | ||
helper.setSubject(subject); | ||
helper.setFrom(from); | ||
javaMailSender.send(mimeMessage); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/it/gov/innovazione/ndc/service/TemplateService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package it.gov.innovazione.ndc.service; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.SneakyThrows; | ||
import org.apache.commons.io.IOUtils; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
@Getter | ||
public class TemplateService { | ||
|
||
@Value("classpath:templates/alerter_mail_template.html") | ||
private final Resource alerterMailTemplateResource; | ||
@Value("classpath:templates/event_list_template.html") | ||
private final Resource eventListTemplateResource; | ||
@Value("classpath:templates/event_template.html") | ||
private final Resource eventTemplateResource; | ||
|
||
private String alerterMailTemplate; | ||
private String eventListTemplate; | ||
private String eventTemplate; | ||
|
||
@PostConstruct | ||
public void init() { | ||
alerterMailTemplate = readTemplate(alerterMailTemplateResource); | ||
eventListTemplate = readTemplate(eventListTemplateResource); | ||
eventTemplate = readTemplate(eventTemplateResource); | ||
} | ||
|
||
@SneakyThrows | ||
private String readTemplate(Resource eventTemplateResource) { | ||
return IOUtils.toString(eventTemplateResource.getInputStream(), StandardCharsets.UTF_8); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<p>Ciao <b>${recipient.name} ${recipient.surname}</b></p> | ||
<p>Di seguito i dettagli degli eventi riscontrati:</p> | ||
${eventList} | ||
<p>Origine: Generata automaticamente dall'harvester.</p> | ||
<p>Cordiali saluti,<br/>Il team di supporto di Schemagov</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<ol> | ||
${events} | ||
</ol> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<li style='margin-bottom:2em'><b>${event.name}</b> | ||
<ul> | ||
<li><b>Descrizione:</b> ${event.description}</li> | ||
<li><b>Severity:</b> ${event.severity}</li> | ||
<li><b>Contesto:</b><br/> ${event.context}</li> | ||
<li><b>Creato da:</b> ${event.createdBy}</b></li> | ||
<li><b>Creato il:</b> ${event.createdAt}</b></li> | ||
</ul> | ||
</li> |