2019-12-19 16:04:25 +01:00
|
|
|
package eu.dnetlib.uoamonitorservice.controllers;
|
|
|
|
|
2020-06-17 12:56:52 +02:00
|
|
|
import eu.dnetlib.uoaadmintoolslibrary.emailSender.EmailSender;
|
|
|
|
import eu.dnetlib.uoaadmintoolslibrary.entities.email.Email;
|
|
|
|
import eu.dnetlib.uoaadmintoolslibrary.entities.email.EmailRecaptcha;
|
|
|
|
import eu.dnetlib.uoaadmintoolslibrary.handlers.InvalidReCaptchaException;
|
|
|
|
import eu.dnetlib.uoaadmintoolslibrary.recaptcha.VerifyRecaptcha;
|
2022-10-10 14:27:17 +02:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2019-12-19 16:04:25 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
@CrossOrigin(origins = "*")
|
|
|
|
public class EmailController {
|
2022-10-10 14:27:17 +02:00
|
|
|
private final Logger log = LogManager.getLogger(this.getClass());
|
2019-12-19 16:04:25 +01:00
|
|
|
|
2024-03-06 00:27:38 +01:00
|
|
|
private final EmailSender emailSender;
|
|
|
|
private final VerifyRecaptcha verifyRecaptcha;
|
2019-12-19 16:04:25 +01:00
|
|
|
|
|
|
|
@Autowired
|
2024-03-06 00:27:38 +01:00
|
|
|
public EmailController(EmailSender emailSender, VerifyRecaptcha verifyRecaptcha) {
|
|
|
|
this.emailSender = emailSender;
|
|
|
|
this.verifyRecaptcha = verifyRecaptcha;
|
|
|
|
}
|
2019-12-19 16:04:25 +01:00
|
|
|
|
|
|
|
@RequestMapping(value = "/contact", method = RequestMethod.POST)
|
|
|
|
public Boolean contact(@RequestBody EmailRecaptcha form) throws InvalidReCaptchaException {
|
|
|
|
verifyRecaptcha.processResponse(form.getRecaptcha());
|
|
|
|
Email email = form.getEmail();
|
2024-03-06 00:27:38 +01:00
|
|
|
return emailSender.send(email.getRecipients(), email.getSubject(), email.getBody(), false);
|
2019-12-19 16:04:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/sendMail", method = RequestMethod.POST)
|
|
|
|
public Map<String, ArrayList<String>> sendEmail(@RequestBody Email email,
|
|
|
|
@RequestParam(required = false) Optional<Boolean> optional) {
|
|
|
|
String successString = "success";
|
|
|
|
String failureString = "failure";
|
|
|
|
Map<String, ArrayList<String>> mailResults = new HashMap<>();
|
2024-03-06 00:27:38 +01:00
|
|
|
boolean bcc = optional.orElse(true);
|
2019-12-19 16:04:25 +01:00
|
|
|
for(String userMail:email.getRecipients()){
|
|
|
|
ArrayList<String> sendTo = new ArrayList<>();
|
|
|
|
sendTo.add(userMail);
|
|
|
|
boolean success =emailSender.send(sendTo,email.getSubject(),email.getBody(), bcc);
|
|
|
|
if(success){
|
|
|
|
if(!mailResults.containsKey(successString)) {
|
|
|
|
mailResults.put(successString, new ArrayList<>());
|
|
|
|
}
|
|
|
|
mailResults.get(successString).add(userMail);
|
|
|
|
} else {
|
|
|
|
if(!mailResults.containsKey(failureString)) {
|
|
|
|
mailResults.put(failureString, new ArrayList<>());
|
|
|
|
}
|
|
|
|
mailResults.get(failureString).add(userMail);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mailResults;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|