use of io.jmix.email.EmailAttachment in project jmix by jmix-framework.
the class EmailTemplatesImpl method generateEmail.
@Override
public EmailInfo generateEmail(EmailTemplate emailTemplate, Collection<ReportWithParams> params) throws TemplateNotFoundException, ReportParameterTypeChangedException {
if (emailTemplate == null) {
throw new TemplateNotFoundException(messages.getMessage(EmailTemplates.class, "nullTemplate"));
}
List<ReportWithParams> parameters = new ArrayList<>(params);
TemplateReport bodyReport = emailTemplate.getEmailBodyReport();
ReportWithParams bodyReportWithParams = bodyReport != null ? getReportWithParams(bodyReport, parameters) : null;
Map<TemplateReport, ReportWithParams> attachmentsWithParams = new HashMap<>();
List<TemplateReport> attachedTemplateReports = emailTemplate.getAttachedTemplateReports();
if (attachedTemplateReports != null) {
for (TemplateReport templateReport : attachedTemplateReports) {
ReportWithParams reportWithParams = getReportWithParams(templateReport, parameters);
attachmentsWithParams.put(templateReport, reportWithParams);
}
}
EmailInfo emailInfo = generateEmailInfoWithoutAttachments(bodyReportWithParams);
List<EmailAttachment> templateAttachments = new ArrayList<>();
templateAttachments.addAll(createReportAttachments(attachmentsWithParams));
templateAttachments.addAll(createFilesAttachments(emailTemplate.getAttachedFiles()));
emailInfo.setSubject(Boolean.TRUE.equals(emailTemplate.getUseReportSubject()) ? emailInfo.getSubject() : emailTemplate.getSubject());
emailInfo.setAddresses(emailTemplate.getTo());
emailInfo.setCc(emailTemplate.getCc());
emailInfo.setBcc(emailTemplate.getBcc());
emailInfo.setFrom(emailTemplate.getFrom());
emailInfo.setAttachments(templateAttachments);
return emailInfo;
}
use of io.jmix.email.EmailAttachment in project jmix by jmix-framework.
the class EmailTemplatesImpl method createEmailAttachment.
protected EmailAttachment createEmailAttachment(String templateName, ReportWithParams reportWithParams) {
ReportOutputDocument outputDocument = reportRunner.run(new ReportRunContext(reportWithParams.getReport()).setParams(reportWithParams.getParams()));
String fileName = outputDocument.getDocumentName();
if (StringUtils.isNotBlank(templateName)) {
String extension = Files.getFileExtension(templateName);
String docExtension = Files.getFileExtension(fileName);
if (StringUtils.isNotBlank(extension)) {
fileName = templateName;
} else if (StringUtils.isNotBlank(docExtension)) {
fileName = templateName + "." + docExtension;
} else {
fileName = templateName;
}
}
return new EmailAttachment(outputDocument.getContent(), fileName);
}
use of io.jmix.email.EmailAttachment in project jmix-docs by Haulmont.
the class SendByEmailAction method actionPerform.
@Override
public void actionPerform(Component component) {
if (recipientAddress == null || bodyGenerator == null) {
throw new IllegalStateException("Required parameters are not set");
}
E selected = (E) getTarget().getSingleSelected();
if (selected == null) {
return;
}
String caption = "Entity " + metadataTools.getInstanceName(selected) + " info";
// <8>
String body = bodyGenerator.apply(selected);
List<EmailAttachment> attachments = // <9>
attachmentProvider != null ? // <9>
attachmentProvider.apply(selected) : new ArrayList<>();
EmailInfo info = EmailInfoBuilder.create().setAddresses(recipientAddress).setSubject(caption).setBody(body).setBodyContentType(EmailInfo.TEXT_CONTENT_TYPE).setAttachments(attachments.toArray(new EmailAttachment[0])).build();
// <10>
emailer.sendEmailAsync(info);
EmailSentEvent event = new EmailSentEvent(this, info);
// <11>
eventHub.publish(EmailSentEvent.class, event);
}
use of io.jmix.email.EmailAttachment in project jmix by jmix-framework.
the class EmailTemplatesImpl method createReportAttachments.
protected List<EmailAttachment> createReportAttachments(Map<TemplateReport, ReportWithParams> reportsWithParams) {
List<EmailAttachment> attachmentsList = new ArrayList<>();
for (Map.Entry<TemplateReport, ReportWithParams> entry : reportsWithParams.entrySet()) {
TemplateReport templateReport = entry.getKey();
ReportWithParams reportWithParams = entry.getValue();
EmailAttachment emailAttachment = createEmailAttachment(templateReport.getName(), reportWithParams);
attachmentsList.add(emailAttachment);
}
return attachmentsList;
}
Aggregations