Search in sources :

Example 16 with Mail

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

the class SendEmailService method sendCorrelated.

/**
 * 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 correlation correlation information to be attached to the message
 * @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 sendCorrelated(String correlation, 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());
            }
            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 17 with Mail

use of io.quarkus.mailer.Mail in project quarkus-quickstarts by quarkusio.

the class MailerResourceTest method testMail.

@Test
void testMail() {
    given().queryParam("name", "foo").queryParam("email", TO).when().get("/type-safe").then().statusCode(202);
    // verify that it was sent
    List<Mail> sent = mailbox.getMessagesSentTo(TO);
    assertThat(sent).hasSize(1);
    Mail actual = sent.get(0);
    assertThat(actual.getHtml()).contains("Welcome on board foo!");
    assertThat(actual.getSubject()).isEqualTo("Ahoy foo!");
    assertThat(mailbox.getTotalMessagesSent()).isEqualTo(1);
}
Also used : Mail(io.quarkus.mailer.Mail) Test(org.junit.jupiter.api.Test) QuarkusTest(io.quarkus.test.junit.QuarkusTest)

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