Skip to content

Commit

Permalink
Merge pull request #92 from correctexam/develop
Browse files Browse the repository at this point in the history
sendEmail with RaplyTo
  • Loading branch information
barais authored Oct 11, 2023
2 parents 3fd46a7 + 3814b00 commit 97db863
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
22 changes: 22 additions & 0 deletions src/main/java/fr/istic/service/MailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ public CompletionStage<Void> sendEmail(String mail, String body, String subject)
);
}

public CompletionStage<Void> sendEmail(String mail, String body, String subject, String replyto) {
return reactiveMailer.send(Mail.withText(mail,subject,body).addReplyTo(replyto)).subscribeAsCompletionStage().thenAccept(
it -> {
log.debug("Sent email to mail '{}'", mail);
}
);
}


public CompletionStage<Void> sendEmailWithAttachement(String mail, String body, String subject, String filename, byte[] content,String contentType) {
Mail m = Mail.withText(mail,subject,body);
m.addAttachment(filename, content,contentType);
Expand All @@ -82,6 +91,19 @@ public CompletionStage<Void> sendEmailWithAttachement(String mail, String body,
}


public CompletionStage<Void> sendEmailWithAttachement(String mail, String body, String subject, String filename, byte[] content,String contentType, String replyTo) {
Mail m = Mail.withText(mail,subject,body);
m.addReplyTo(replyTo);
m.addAttachment(filename, content,contentType);
return reactiveMailer.send(m).subscribeAsCompletionStage().thenAccept(
it -> {
log.debug("Sent email to mail '{}'", mail);
}
);
}



public CompletionStage<Void> sendActivationEmail(User user) {
log.debug("Sending activation email to '{}'", user.email);
return sendEmailFromTemplate(user, activationEmail, "CorrectExam account activation is required");
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/fr/istic/web/rest/ExtendedAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,21 @@ public Response sendResultToStudent(MailResultDTO dto, @PathParam("examId") long
if (!securityService.canAccess(ctx, examId, Exam.class)) {
return Response.status(403, "Current user cannot access to this ressource").build();
}

var userLogin = Optional
.ofNullable(ctx.getUserPrincipal().getName());
if (!userLogin.isPresent()){
throw new AccountResourceException("Current user login not found");
}
var user = User.findOneByLogin(userLogin.get());
if (!user.isPresent()) {
throw new AccountResourceException("User could not be found");
}
String _replyTo = user.get().email;
if (_replyTo == null || "".equals(_replyTo)){
_replyTo= "[email protected]";
}
final String replyTo = _replyTo;
Exam ex = this.computeFinalNote(examId);

List<Student> students = Student.findStudentsbyCourseId(ex.course.id).list();
Expand All @@ -492,6 +507,7 @@ public Response sendResultToStudent(MailResultDTO dto, @PathParam("examId") long
FinalResult r = FinalResult.findFinalResultByStudentIdAndExamId(student.id, ex.id).firstResult();
ExamSheet sheet = ExamSheet.findExamSheetByScanAndStudentId(ex.scanfile.id, student.id).firstResult();
String uuid = sheet.name;

String body = dto.getBody();
body = body.replace("${url}", this.jHipsterProperties.mail().baseUrl() + "/copie/" + uuid + "/1");
body = body.replace("${firstname}", student.firstname);
Expand All @@ -504,20 +520,20 @@ public Response sendResultToStudent(MailResultDTO dto, @PathParam("examId") long
in = this.cacheStudentPdfFService.getFile(examId, sheet.name + ".pdf");
byte[] bytes = IOUtils.toByteArray(in);
mailService.sendEmailWithAttachement(student.mail, body, dto.getSubject(),
student.firstname + "_" + student.name + ".pdf", bytes, "application/pdf");
student.firstname + "_" + student.name + ".pdf", bytes, "application/pdf",replyTo);
} catch (Exception e) {
e.printStackTrace();
}
} else {
mailService.sendEmail(student.mail, body, dto.getSubject());
mailService.sendEmail(student.mail, body, dto.getSubject(),replyTo);
}

} else {
if (dto.isMailabi()) {
String body = dto.getBodyabi();
body = body.replace("${firstname}", student.firstname);
body = body.replace("${lastname}", student.name);
mailService.sendEmail(student.mail, body, dto.getSubject());
mailService.sendEmail(student.mail, body, dto.getSubject(),replyTo);
}
}
});
Expand Down

0 comments on commit 97db863

Please sign in to comment.