Search in sources :

Example 1 with Mail

use of io.quarkus.mailer.Mail in project automatiko-engine by automatiko-io.

the class HumanTaskLifeCycleWithEmail method sendEmail.

private void sendEmail(WorkItem workItem) {
    HumanTaskWorkItem humanTask = (HumanTaskWorkItem) workItem;
    List<String> users = new ArrayList<>();
    if (humanTask.getActualOwner() != null) {
        users.add(humanTask.getActualOwner());
    }
    if (humanTask.getPotentialUsers() != null) {
        users.addAll(humanTask.getPotentialUsers());
    }
    Map<String, String> addresses = emailAddressResolver.resolve(users, humanTask.getPotentialGroups());
    if (addresses.isEmpty()) {
        return;
    }
    String subject = "New task has been assigned to you (" + humanTask.getTaskName() + ")";
    Template template = getTemplate(humanTask.getProcessInstance().getProcess(), humanTask);
    if (template == null) {
        template = engine.getTemplate(DEFAULT_TEMPLATE);
    }
    Mail[] emails = new Mail[addresses.size()];
    Map<String, Object> templateData = new HashMap<>();
    templateData.put("name", humanTask.getTaskName());
    templateData.put("description", humanTask.getTaskDescription());
    templateData.put("taskId", humanTask.getId());
    templateData.put("instanceId", humanTask.getProcessInstanceId());
    templateData.put("processId", humanTask.getProcessInstance().getProcessId());
    templateData.put("inputs", humanTask.getParameters());
    int count = 0;
    for (Entry<String, String> address : addresses.entrySet()) {
        String dedicatedTo = "";
        if (users.contains(address.getKey())) {
            dedicatedTo = address.getKey();
        }
        String parentProcessInstanceId = humanTask.getProcessInstance().getParentProcessInstanceId();
        if (parentProcessInstanceId != null && !parentProcessInstanceId.isEmpty()) {
            parentProcessInstanceId += ":";
        } else {
            parentProcessInstanceId = "";
        }
        String version = version(humanTask.getProcessInstance().getProcess().getVersion());
        String encoded = Base64.getEncoder().encodeToString((humanTask.getProcessInstance().getProcessId() + version + "|" + parentProcessInstanceId + humanTask.getProcessInstance().getId() + "|" + humanTask.getId() + "|" + dedicatedTo).getBytes(StandardCharsets.UTF_8));
        String link = serviceUrl + "/management/tasks/link/" + encoded;
        templateData.put("link", link);
        String body = template.instance().data(templateData).render();
        emails[count] = Mail.withHtml(address.getValue(), subject, body);
        count++;
    }
    // send emails asynchronously
    CompletableFuture.runAsync(() -> {
        for (String to : addresses.values()) {
            mailer.send(emails);
            LOGGER.debug("Email sent to {} with new assigned task {}", to, humanTask.getName());
        }
    });
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Template(io.quarkus.qute.Template) HumanTaskWorkItem(io.automatiko.engine.api.runtime.process.HumanTaskWorkItem) Mail(io.quarkus.mailer.Mail)

Example 2 with Mail

use of io.quarkus.mailer.Mail in project automatiko-engine by automatiko-io.

the class SendEmailService method sendSimple.

/**
 * Sends email to the given list of recipients with given subject and body.
 * Optionally given attachments will be put on the message as well
 *
 * In case of error a <code>ServiceExecutionError</code> will be thrown with error code set to <code>sendEmailFailure</code>
 * so it can be used within workflow definition to handle it
 *
 * @param tos comma separated list of recipients
 * @param subject subject of the email
 * @param body body of the email
 * @param attachments optional attachments
 */
public void sendSimple(String tos, String subject, String body, File<byte[]>... attachments) {
    try {
        for (String to : tos.split(",")) {
            Mail mail = Mail.withHtml(to, subject, body);
            for (File<byte[]> attachment : attachments) {
                if (attachment == null) {
                    continue;
                }
                mail.addAttachment(attachment.name(), attachment.content(), attachment.type());
            }
            mailer.send(mail);
        }
    } catch (Exception e) {
        throw new ServiceExecutionError("sendEmailFailure", e.getMessage(), e);
    }
}
Also used : ServiceExecutionError(io.automatiko.engine.api.workflow.ServiceExecutionError) Mail(io.quarkus.mailer.Mail)

Example 3 with Mail

use of io.quarkus.mailer.Mail in project automatiko-engine by automatiko-io.

the class SendEmailService method sendCorrelatedWithCC.

/**
 * Sends email to the given list of recipients and cc recipients with given subject and body that is created based on the
 * given template.
 * Optionally given attachments will be put on the message as well
 *
 * In case of error a <code>ServiceExecutionError</code> will be thrown with error code set to <code>sendEmailFailure</code>
 * so it can be used within workflow definition to handle it
 *
 * In case template cannot be found with given name a <code>ServiceExecutionError</code> will
 * be thrown with error code set to <code>emailTemplateNotFound</code>
 *
 * @param correlation correlation information to be attached to the message
 * @param tos comma separated list of recipients
 * @param ccs comma separated list of CC recipients
 * @param subject subject of the email
 * @param templateName name of the email template to be used to create the body of the email message
 * @param body body of the email
 * @param attachments optional attachments
 */
public void sendCorrelatedWithCC(String correlation, String tos, String ccs, String subject, String templateName, Object body, File<byte[]>... attachments) {
    Template template = getTemplate(templateName);
    try {
        Map<String, Object> templateData = new HashMap<>();
        templateData.put("body", body);
        String content = template.instance().data(templateData).render();
        for (String to : tos.split(",")) {
            Mail mail = Mail.withHtml(to, subject, content);
            for (String cc : ccs.split(",")) {
                mail.addCc(cc);
            }
            for (File<byte[]> attachment : attachments) {
                if (attachment == null) {
                    continue;
                }
                mail.addAttachment(attachment.name(), attachment.content(), attachment.type());
            }
            mail.addHeader("Message-ID", EmailUtils.messageIdWithCorrelation(correlation, host.orElse("localhost")));
            mailer.send(mail);
        }
    } catch (Exception e) {
        throw new ServiceExecutionError("sendEmailFailure", e.getMessage(), e);
    }
}
Also used : ServiceExecutionError(io.automatiko.engine.api.workflow.ServiceExecutionError) Mail(io.quarkus.mailer.Mail) HashMap(java.util.HashMap) Template(io.quarkus.qute.Template)

Example 4 with Mail

use of io.quarkus.mailer.Mail in project automatiko-engine by automatiko-io.

the class SendEmailService method send.

/**
 * Sends email to the given list of recipients with given subject and body that is created based on the given template.
 * Optionally given attachments will be put on the message as well
 *
 * In case of error a <code>ServiceExecutionError</code> will be thrown with error code set to <code>sendEmailFailure</code>
 * so it can be used within workflow definition to handle it
 *
 * In case template cannot be found with given name a <code>ServiceExecutionError</code> will
 * be thrown with error code set to <code>emailTemplateNotFound</code>
 *
 * @param tos comma separated list of recipients
 * @param subject subject of the email
 * @param templateName name of the email template to be used to create the body of the email message
 * @param body body of the email
 * @param attachments optional attachments
 */
public void send(String tos, String subject, String templateName, Object body, File<byte[]>... attachments) {
    Template template = getTemplate(templateName);
    try {
        Map<String, Object> templateData = new HashMap<>();
        templateData.put("body", body);
        String content = template.instance().data(templateData).render();
        for (String to : tos.split(",")) {
            Mail mail = Mail.withHtml(to, subject, content);
            for (File<byte[]> attachment : attachments) {
                if (attachment == null) {
                    continue;
                }
                mail.addAttachment(attachment.name(), attachment.content(), attachment.type());
            }
            mailer.send(mail);
        }
    } catch (Exception e) {
        throw new ServiceExecutionError("sendEmailFailure", e.getMessage(), e);
    }
}
Also used : ServiceExecutionError(io.automatiko.engine.api.workflow.ServiceExecutionError) Mail(io.quarkus.mailer.Mail) HashMap(java.util.HashMap) Template(io.quarkus.qute.Template)

Example 5 with Mail

use of io.quarkus.mailer.Mail in project kogito-runtimes by kiegroup.

the class QuarkusMailSenderTest method testMail.

@Test
void testMail() {
    UserTaskDeadlineDataEvent event = Mockito.mock(UserTaskDeadlineDataEvent.class);
    Map<String, Object> notification = new HashMap<>();
    notification.put(MailInfo.SUBJECT_PROPERTY, SUBJECT);
    notification.put(MailInfo.BODY_PROPERTY, TEXT);
    notification.put(MailInfo.FROM_PROPERTY, "realbetisbalompie@gmail.com");
    notification.put(MailInfo.TO_PROPERTY, TO + ",fulanito@doesnotexist.com");
    UserTaskDeadlineEventBody eventData = UserTaskDeadlineEventBody.create("1", notification).build();
    Mockito.when(event.getData()).thenReturn(eventData);
    sender.onDeadline(event);
    List<Mail> messages = mailBox.getMessagesSentTo(TO);
    assertEquals(1, messages.size());
    Mail message = messages.get(0);
    assertEquals(TEXT, message.getText());
    assertEquals(SUBJECT, message.getSubject());
}
Also used : Mail(io.quarkus.mailer.Mail) HashMap(java.util.HashMap) UserTaskDeadlineDataEvent(org.kie.kogito.services.event.UserTaskDeadlineDataEvent) UserTaskDeadlineEventBody(org.kie.kogito.services.event.impl.UserTaskDeadlineEventBody) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Aggregations

Mail (io.quarkus.mailer.Mail)17 ServiceExecutionError (io.automatiko.engine.api.workflow.ServiceExecutionError)12 HashMap (java.util.HashMap)8 Template (io.quarkus.qute.Template)7 QuarkusTest (io.quarkus.test.junit.QuarkusTest)3 Test (org.junit.jupiter.api.Test)3 HumanTaskWorkItem (io.automatiko.engine.api.runtime.process.HumanTaskWorkItem)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Incoming (org.eclipse.microprofile.reactive.messaging.Incoming)1 UserTaskDeadlineDataEvent (org.kie.kogito.services.event.UserTaskDeadlineDataEvent)1 UserTaskDeadlineEventBody (org.kie.kogito.services.event.impl.UserTaskDeadlineEventBody)1