Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(mail-outbound-connector): cherry pick mail content from rc and … #3755

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"id" : "io.camunda.connectors.email.v1",
"description" : "Execute email requests",
"documentationRef" : "https://docs.camunda.io/docs/components/connectors/out-of-the-box-connectors/email/",
"version" : 1,
"version" : 2,
"category" : {
"id" : "connectors",
"name" : "Connectors"
Expand Down Expand Up @@ -538,6 +538,29 @@
},
"tooltip" : "Comma-separated list of email, e.g., '[email protected],[email protected]' or '=[ \"[email protected]\", \"[email protected]\"]'",
"type" : "String"
}, {
"id" : "smtpHeaders",
"label" : "Headers",
"optional" : true,
"feel" : "required",
"group" : "sendEmailSmtp",
"binding" : {
"name" : "data.smtpAction.headers",
"type" : "zeebe:input"
},
"condition" : {
"allMatch" : [ {
"property" : "data.smtpActionDiscriminator",
"equals" : "sendEmailSmtp",
"type" : "simple"
}, {
"property" : "protocol",
"equals" : "smtp",
"type" : "simple"
} ]
},
"tooltip" : "Additional email headers",
"type" : "String"
}, {
"id" : "smtpSubject",
"label" : "Subject",
Expand Down Expand Up @@ -565,12 +588,42 @@
"tooltip" : "Email's subject",
"type" : "String"
}, {
"id" : "smtpBody",
"label" : "Email Content",
"id" : "contentType",
"label" : "ContentType",
"optional" : false,
"constraints" : {
"notEmpty" : true
"value" : "PLAIN",
"group" : "sendEmailSmtp",
"binding" : {
"name" : "data.smtpAction.contentType",
"type" : "zeebe:input"
},
"condition" : {
"allMatch" : [ {
"property" : "data.smtpActionDiscriminator",
"equals" : "sendEmailSmtp",
"type" : "simple"
}, {
"property" : "protocol",
"equals" : "smtp",
"type" : "simple"
} ]
},
"tooltip" : "Email's contentType",
"type" : "Dropdown",
"choices" : [ {
"name" : "PLAIN",
"value" : "PLAIN"
}, {
"name" : "HTML",
"value" : "HTML"
}, {
"name" : "HTML & Plaintext",
"value" : "MULTIPART"
} ]
}, {
"id" : "smtpBody",
"label" : "Email Text Content",
"optional" : false,
"feel" : "optional",
"group" : "sendEmailSmtp",
"binding" : {
Expand All @@ -579,6 +632,10 @@
},
"condition" : {
"allMatch" : [ {
"property" : "contentType",
"oneOf" : [ "PLAIN", "MULTIPART" ],
"type" : "simple"
}, {
"property" : "data.smtpActionDiscriminator",
"equals" : "sendEmailSmtp",
"type" : "simple"
Expand All @@ -590,6 +647,33 @@
},
"tooltip" : "Email's content",
"type" : "Text"
}, {
"id" : "smtpHtmlBody",
"label" : "Email Html Content",
"optional" : false,
"feel" : "optional",
"group" : "sendEmailSmtp",
"binding" : {
"name" : "data.smtpAction.htmlBody",
"type" : "zeebe:input"
},
"condition" : {
"allMatch" : [ {
"property" : "contentType",
"oneOf" : [ "HTML", "MULTIPART" ],
"type" : "simple"
}, {
"property" : "data.smtpActionDiscriminator",
"equals" : "sendEmailSmtp",
"type" : "simple"
}, {
"property" : "protocol",
"equals" : "smtp",
"type" : "simple"
} ]
},
"tooltip" : "Email's Html content",
"type" : "Text"
}, {
"id" : "pop3maxToBeRead",
"label" : "Maximum number of emails to be read",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"id" : "io.camunda.connectors.email.v1-hybrid",
"description" : "Execute email requests",
"documentationRef" : "https://docs.camunda.io/docs/components/connectors/out-of-the-box-connectors/email/",
"version" : 1,
"version" : 2,
"category" : {
"id" : "connectors",
"name" : "Connectors"
Expand Down Expand Up @@ -543,6 +543,29 @@
},
"tooltip" : "Comma-separated list of email, e.g., '[email protected],[email protected]' or '=[ \"[email protected]\", \"[email protected]\"]'",
"type" : "String"
}, {
"id" : "smtpHeaders",
"label" : "Headers",
"optional" : true,
"feel" : "required",
"group" : "sendEmailSmtp",
"binding" : {
"name" : "data.smtpAction.headers",
"type" : "zeebe:input"
},
"condition" : {
"allMatch" : [ {
"property" : "data.smtpActionDiscriminator",
"equals" : "sendEmailSmtp",
"type" : "simple"
}, {
"property" : "protocol",
"equals" : "smtp",
"type" : "simple"
} ]
},
"tooltip" : "Additional email headers",
"type" : "String"
}, {
"id" : "smtpSubject",
"label" : "Subject",
Expand Down Expand Up @@ -570,12 +593,42 @@
"tooltip" : "Email's subject",
"type" : "String"
}, {
"id" : "smtpBody",
"label" : "Email Content",
"id" : "contentType",
"label" : "ContentType",
"optional" : false,
"constraints" : {
"notEmpty" : true
"value" : "PLAIN",
"group" : "sendEmailSmtp",
"binding" : {
"name" : "data.smtpAction.contentType",
"type" : "zeebe:input"
},
"condition" : {
"allMatch" : [ {
"property" : "data.smtpActionDiscriminator",
"equals" : "sendEmailSmtp",
"type" : "simple"
}, {
"property" : "protocol",
"equals" : "smtp",
"type" : "simple"
} ]
},
"tooltip" : "Email's contentType",
"type" : "Dropdown",
"choices" : [ {
"name" : "PLAIN",
"value" : "PLAIN"
}, {
"name" : "HTML",
"value" : "HTML"
}, {
"name" : "HTML & Plaintext",
"value" : "MULTIPART"
} ]
}, {
"id" : "smtpBody",
"label" : "Email Text Content",
"optional" : false,
"feel" : "optional",
"group" : "sendEmailSmtp",
"binding" : {
Expand All @@ -584,6 +637,10 @@
},
"condition" : {
"allMatch" : [ {
"property" : "contentType",
"oneOf" : [ "PLAIN", "MULTIPART" ],
"type" : "simple"
}, {
"property" : "data.smtpActionDiscriminator",
"equals" : "sendEmailSmtp",
"type" : "simple"
Expand All @@ -595,6 +652,33 @@
},
"tooltip" : "Email's content",
"type" : "Text"
}, {
"id" : "smtpHtmlBody",
"label" : "Email Html Content",
"optional" : false,
"feel" : "optional",
"group" : "sendEmailSmtp",
"binding" : {
"name" : "data.smtpAction.htmlBody",
"type" : "zeebe:input"
},
"condition" : {
"allMatch" : [ {
"property" : "contentType",
"oneOf" : [ "HTML", "MULTIPART" ],
"type" : "simple"
}, {
"property" : "data.smtpActionDiscriminator",
"equals" : "sendEmailSmtp",
"type" : "simple"
}, {
"property" : "protocol",
"equals" : "smtp",
"type" : "simple"
} ]
},
"tooltip" : "Email's Html content",
"type" : "Text"
}, {
"id" : "pop3maxToBeRead",
"label" : "Maximum number of emails to be read",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package io.camunda.connector.email.client.jakarta.outbound;

import static io.camunda.connector.email.outbound.protocols.actions.ContentType.PLAIN;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.camunda.connector.email.authentication.Authentication;
Expand All @@ -14,12 +16,12 @@
import io.camunda.connector.email.outbound.model.EmailRequest;
import io.camunda.connector.email.outbound.protocols.Protocol;
import io.camunda.connector.email.outbound.protocols.actions.*;
import io.camunda.connector.email.outbound.protocols.actions.ContentType;
import io.camunda.connector.email.response.*;
import jakarta.mail.*;
import jakarta.mail.internet.AddressException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.internet.*;
import jakarta.mail.search.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class JakartaEmailActionExecutor implements EmailActionExecutor {
Expand Down Expand Up @@ -249,13 +251,17 @@ private SendEmailResponse smtpSendEmail(
Optional<InternetAddress[]> to = createParsedInternetAddresses(smtpSendEmail.to());
Optional<InternetAddress[]> cc = createParsedInternetAddresses(smtpSendEmail.cc());
Optional<InternetAddress[]> bcc = createParsedInternetAddresses(smtpSendEmail.bcc());
Optional<Map<String, String>> headers = Optional.ofNullable(smtpSendEmail.headers());
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(smtpSendEmail.from()));
if (to.isPresent()) message.setRecipients(Message.RecipientType.TO, to.get());
if (cc.isPresent()) message.setRecipients(Message.RecipientType.CC, cc.get());
if (bcc.isPresent()) message.setRecipients(Message.RecipientType.BCC, bcc.get());
headers.ifPresent(stringObjectMap -> setMessageHeaders(stringObjectMap, message));
message.setSubject(smtpSendEmail.subject());
message.setText(smtpSendEmail.body());
Multipart multipart = getMultipart(smtpSendEmail);

message.setContent(multipart);
try (Transport transport = session.getTransport()) {
this.jakartaUtils.connectTransport(transport, authentication);
transport.sendMessage(message, message.getAllRecipients());
Expand All @@ -266,6 +272,44 @@ private SendEmailResponse smtpSendEmail(
}
}

private void setMessageHeaders(Map<String, String> stringObjectMap, Message message) {
stringObjectMap.forEach(
(key, value) -> {
try {
message.setHeader(key, value);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
});
}

private Multipart getMultipart(SmtpSendEmail smtpSendEmail) throws MessagingException {
Multipart multipart = new MimeMultipart();
ContentType contentType =
smtpSendEmail.contentType() == null ? PLAIN : smtpSendEmail.contentType();
switch (contentType) {
case PLAIN -> {
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(smtpSendEmail.body(), StandardCharsets.UTF_8.name());
multipart.addBodyPart(textPart);
}
case HTML -> {
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(smtpSendEmail.htmlBody(), JakartaUtils.HTML_CHARSET);
multipart.addBodyPart(htmlPart);
}
case MULTIPART -> {
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(smtpSendEmail.body(), StandardCharsets.UTF_8.name());
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(smtpSendEmail.htmlBody(), JakartaUtils.HTML_CHARSET);
multipart.addBodyPart(textPart);
multipart.addBodyPart(htmlPart);
}
}
return multipart;
}

private SearchTerm createSearchTerms(JsonNode jsonNode) throws AddressException {
List<SearchTerm> searchTerms = new ArrayList<>();
if (jsonNode.has("operator")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class JakartaUtils {

private static final Logger LOGGER = LoggerFactory.getLogger(JakartaUtils.class);
private static final String REGEX_PATH_SPLITTER = "[./]";
public static final String HTML_CHARSET = "text/html; charset=utf-8";

public Session createSession(Configuration configuration) {
return Session.getInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
name = "Email Connector",
description = "Execute email requests",
inputDataClass = EmailRequest.class,
version = 1,
version = 2,
propertyGroups = {
@ElementTemplate.PropertyGroup(id = "authentication", label = "Authentication"),
@ElementTemplate.PropertyGroup(id = "protocol", label = "Protocol"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. Licensed under a proprietary license.
* See the License.txt file for more information. You may not use this file
* except in compliance with the proprietary license.
*/
package io.camunda.connector.email.outbound.protocols.actions;

public enum ContentType {
PLAIN("text/plain; charset=utf-8"),
HTML("text/html; charset=utf-8"),
MULTIPART("multipart/mixed; charset=utf-8");

private final String value;

ContentType(String value) {
this.value = value;
}

public String type() {
return value;
}
}
Loading
Loading