Search in sources :

Example 1 with EmailAttachment

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;
}
Also used : ReportWithParams(io.jmix.emailtemplates.dto.ReportWithParams) EmailAttachment(io.jmix.email.EmailAttachment) EmailTemplates(io.jmix.emailtemplates.EmailTemplates) TemplateNotFoundException(io.jmix.emailtemplates.exception.TemplateNotFoundException) EmailInfo(io.jmix.email.EmailInfo)

Example 2 with EmailAttachment

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);
}
Also used : ReportOutputDocument(com.haulmont.yarg.reporting.ReportOutputDocument) ReportRunContext(io.jmix.reports.runner.ReportRunContext) EmailAttachment(io.jmix.email.EmailAttachment)

Example 3 with EmailAttachment

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);
}
Also used : EmailAttachment(io.jmix.email.EmailAttachment) EmailInfo(io.jmix.email.EmailInfo)

Example 4 with EmailAttachment

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;
}
Also used : ReportWithParams(io.jmix.emailtemplates.dto.ReportWithParams) EmailAttachment(io.jmix.email.EmailAttachment)

Aggregations

EmailAttachment (io.jmix.email.EmailAttachment)4 EmailInfo (io.jmix.email.EmailInfo)2 ReportWithParams (io.jmix.emailtemplates.dto.ReportWithParams)2 ReportOutputDocument (com.haulmont.yarg.reporting.ReportOutputDocument)1 EmailTemplates (io.jmix.emailtemplates.EmailTemplates)1 TemplateNotFoundException (io.jmix.emailtemplates.exception.TemplateNotFoundException)1 ReportRunContext (io.jmix.reports.runner.ReportRunContext)1