Skip to content

Commit

Permalink
other(email-connector): single from address, add fields to readRespon…
Browse files Browse the repository at this point in the history
…se (#3365)

* others(email-connector): add last minute feature requested by the team

* others(email-connector): add last minute feature requested by the team

* others(email-connector): add last minute feature requested by the team 2

* others(email-connector): add last minute feature requested by the team 3

* others(email-connector): add last minute feature requested by the team 3

* feat(email connector): label refinment and fix DoNothing handle feature 4

* chore(email): fix import issues

- remove use of wildcard import
- remove unnecessary fully qualified class name

* chore(email): fix spotless issues

---------

Co-authored-by: Ev Bilske <[email protected]>
  • Loading branch information
mathias-vandaele and ev-codes authored Sep 28, 2024
1 parent 052e2ab commit 3865e40
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@
"type" : "simple"
} ]
},
"tooltip" : "Comma-separated list of email, e.g., '[email protected],[email protected]' or '=[ \"[email protected]\", \"[email protected]\"]'",
"tooltip" : "Address the email will be sent from",
"type" : "String"
}, {
"id" : "smtpTo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@
"type" : "simple"
} ]
},
"tooltip" : "Comma-separated list of email, e.g., '[email protected],[email protected]' or '=[ \"[email protected]\", \"[email protected]\"]'",
"tooltip" : "Address the email will be sent from",
"type" : "String"
}, {
"id" : "smtpTo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public record Email(
EmailBody body,
String messageId,
String subject,
List<String> from,
List<Header> headers,
String from,
List<String> to,
List<String> cc,
OffsetDateTime sentAt,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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.client.jakarta;

public record Header(String key, String value) {}
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ private ReadEmailResponse imapReadEmail(
new ReadEmailResponse(
email.messageId(),
email.from(),
email.headers(),
email.subject(),
email.size(),
email.body().bodyAsPlainText(),
email.body().bodyAsHtml()))
email.body().bodyAsHtml(),
email.receivedAt()))
.orElseThrow(() -> new MessagingException("Could not find an email ID"));
}
} catch (MessagingException e) {
Expand Down Expand Up @@ -204,10 +206,12 @@ private ReadEmailResponse pop3ReadEmail(
new ReadEmailResponse(
email.messageId(),
email.from(),
email.headers(),
email.subject(),
email.size(),
email.body().bodyAsPlainText(),
email.body().bodyAsHtml()))
email.body().bodyAsHtml(),
email.receivedAt()))
.orElseThrow(() -> new MessagingException("No emails have been found with this ID"));
}
}
Expand Down Expand Up @@ -256,12 +260,11 @@ private List<SearchEmailsResponse> pop3SearchEmails(
private SendEmailResponse smtpSendEmail(
SmtpSendEmail smtpSendEmail, Authentication authentication, Session session) {
try {
Optional<InternetAddress[]> from = createParsedInternetAddresses(smtpSendEmail.from());
Optional<InternetAddress[]> to = createParsedInternetAddresses(smtpSendEmail.to());
Optional<InternetAddress[]> cc = createParsedInternetAddresses(smtpSendEmail.cc());
Optional<InternetAddress[]> bcc = createParsedInternetAddresses(smtpSendEmail.bcc());
Message message = new MimeMessage(session);
if (from.isPresent()) message.addFrom(from.get());
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.eclipse.angus.mail.imap.IMAPFolder;
import org.eclipse.angus.mail.imap.IMAPMessage;
import org.eclipse.angus.mail.imap.IdleManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -97,7 +98,7 @@ private void processChangedEvent(
InboundConnectorContext connectorContext,
EmailListenerConfig emailListenerConfig) {
IMAPFolder imapFolder = (IMAPFolder) event.getSource();
Message message = event.getMessage();
IMAPMessage message = (IMAPMessage) event.getMessage();
try {
if (!message.isSet(Flags.Flag.SEEN))
processMail(message, connectorContext, emailListenerConfig);
Expand All @@ -112,7 +113,7 @@ private void pollAllAndProcess(
try {
Message[] messages = folder.getMessages();
Arrays.stream(messages)
.forEach(message -> processMail(message, context, emailListenerConfig));
.forEach(message -> processMail((IMAPMessage) message, context, emailListenerConfig));
} catch (MessagingException e) {
throw new RuntimeException(e);
}
Expand All @@ -124,7 +125,7 @@ private void pollUnseenAndProcess(
FlagTerm unseenFlagTerm = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
Message[] unseenMessages = folder.search(unseenFlagTerm);
Arrays.stream(unseenMessages)
.forEach(message -> processMail(message, context, emailListenerConfig));
.forEach(message -> processMail((IMAPMessage) message, context, emailListenerConfig));
} catch (MessagingException e) {
throw new RuntimeException(e);
}
Expand All @@ -136,7 +137,7 @@ private void processNewEvent(
EmailListenerConfig emailListenerConfig) {
IMAPFolder imapFolder = (IMAPFolder) event.getSource();
for (Message message : event.getMessages()) {
processMail(message, connectorContext, emailListenerConfig);
processMail((IMAPMessage) message, connectorContext, emailListenerConfig);
}
try {
idleManager.watch(imapFolder);
Expand All @@ -146,12 +147,13 @@ private void processNewEvent(
}

private void processMail(
Message message,
IMAPMessage message,
InboundConnectorContext connectorContext,
EmailListenerConfig emailListenerConfig) {

message.setPeek(true);
CorrelationResult correlationResult = this.correlateEmail(message, connectorContext);

message.setPeek(false);
switch (correlationResult) {
case CorrelationResult.Failure failure -> {
switch (failure.handlingStrategy()) {
Expand Down Expand Up @@ -181,10 +183,12 @@ private CorrelationResult correlateEmail(
new ReadEmailResponse(
email.messageId(),
email.from(),
email.headers(),
email.subject(),
email.size(),
email.body().bodyAsPlainText(),
email.body().bodyAsHtml()));
email.body().bodyAsHtml(),
email.receivedAt()));
}

private List<String> createInboxList(Object folderToListen) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void markAsSeen(Message message) {
private Properties createProperties(SmtpConfig smtp) {
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.host", smtp.smtpHost());
properties.put("mail.smtp.host", smtp.smtpHost().trim());
properties.put("mail.smtp.port", smtp.smtpPort().toString());
properties.put("mail.smtp.auth", true);
switch (smtp.smtpCryptographicProtocol()) {
Expand All @@ -90,20 +90,20 @@ private Properties createProperties(Pop3Config pop3) {
switch (pop3.pop3CryptographicProtocol()) {
case NONE -> {
properties.put("mail.store.protocol", "pop3");
properties.put("mail.pop3.host", pop3.pop3Host());
properties.put("mail.pop3.host", pop3.pop3Host().trim());
properties.put("mail.pop3.port", pop3.pop3Port().toString());
properties.put("mail.pop3.auth", true);
}
case TLS -> {
properties.put("mail.store.protocol", "pop3s");
properties.put("mail.pop3s.host", pop3.pop3Host());
properties.put("mail.pop3s.host", pop3.pop3Host().trim());
properties.put("mail.pop3s.port", pop3.pop3Port().toString());
properties.put("mail.pop3s.auth", true);
properties.put("mail.pop3s.starttls.enable", true);
}
case SSL -> {
properties.put("mail.store.protocol", "pop3s");
properties.put("mail.pop3s.host", pop3.pop3Host());
properties.put("mail.pop3s.host", pop3.pop3Host().trim());
properties.put("mail.pop3s.port", pop3.pop3Port().toString());
properties.put("mail.pop3s.auth", true);
properties.put("mail.pop3s.ssl.enable", true);
Expand All @@ -118,21 +118,21 @@ private Properties createProperties(ImapConfig imap) {
switch (imap.imapCryptographicProtocol()) {
case NONE -> {
properties.put("mail.store.protocol", "imap");
properties.put("mail.imap.host", imap.imapHost());
properties.put("mail.imap.host", imap.imapHost().trim());
properties.put("mail.imap.port", imap.imapPort().toString());
properties.put("mail.imap.auth", true);
}
case TLS -> {
properties.put("mail.store.protocol", "imaps");
properties.put("mail.imaps.host", imap.imapHost());
properties.put("mail.imaps.host", imap.imapHost().trim());
properties.put("mail.imaps.port", imap.imapPort().toString());
properties.put("mail.imaps.auth", true);
properties.put("mail.imaps.starttls.enable", true);
properties.put("mail.imaps.usesocketchannels", true);
}
case SSL -> {
properties.put("mail.store.protocol", "imaps");
properties.put("mail.imaps.host", imap.imapHost());
properties.put("mail.imaps.host", imap.imapHost().trim());
properties.put("mail.imaps.port", imap.imapPort().toString());
properties.put("mail.imaps.auth", true);
properties.put("mail.imaps.ssl.enable", true);
Expand Down Expand Up @@ -190,11 +190,12 @@ public Folder findImapFolder(Folder rootFolder, String targetFolder) throws Mess

public Email createBodylessEmail(Message message) {
try {
List<String> from =
String from =
Arrays.stream(Optional.ofNullable(message.getFrom()).orElse(new Address[0]))
.map(Address::toString)
.map(address -> address.replaceAll(".*<|>.*", ""))
.toList();
.toList()
.getFirst();
List<String> to =
Arrays.stream(
Optional.ofNullable(message.getRecipients(Message.RecipientType.TO))
Expand All @@ -219,11 +220,16 @@ public Email createBodylessEmail(Message message) {
.map(Date::toInstant)
.map(instant -> instant.atOffset(ZoneOffset.UTC))
.orElse(null);
List<Header> headers =
Collections.list(message.getAllHeaders()).stream()
.map(header -> new Header(header.getName(), header.getValue()))
.toList();
String messageId = stripMessageId(message.getHeader("Message-ID")[0]);
return new Email(
null,
messageId,
message.getSubject(),
headers,
from,
to,
cc,
Expand Down Expand Up @@ -253,6 +259,7 @@ public Email createEmail(Message message) {
emailBody,
email.messageId(),
email.subject(),
email.headers(),
email.from(),
email.to(),
email.cc(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public record EmailListenerConfig(
defaultValue = "UNSEEN",
choices = {
@TemplateProperty.DropdownPropertyChoice(
label = "Unseen emails will be sync",
label = "Unseen emails will be synced",
value = "UNSEEN"),
@TemplateProperty.DropdownPropertyChoice(
label = "No initial sync. Only new emails",
value = "NONE"),
@TemplateProperty.DropdownPropertyChoice(
label = "All emails will be sync",
label = "All emails will be synced",
value = "ALL")
},
binding = @TemplateProperty.PropertyBinding(name = "data.initialPollingConfig"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public record ImapSearchEmails(
group = "searchEmailsImap",
id = "searchStringEmailImap",
tooltip =
"Define the search criteria using supported keywords and syntax to filter emails. Refer to our detailed documentation for full search syntax and examples: [Email Documentation](https://docs.camunda.io/docs/components/connectors/out-of-the-box-connectors/email/).",
"Define the search criteria using supported keywords and syntax to filter emails.",
description =
"Refer to our detailed documentation for full search syntax and examples: [Email Documentation](https://docs.camunda.io/docs/components/connectors/out-of-the-box-connectors/email/).",
type = TemplateProperty.PropertyType.Text,
feel = Property.FeelMode.required,
optional = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public record Pop3SearchEmails(
group = "searchEmailsPop3",
id = "searchStringEmailPop3",
tooltip =
"Define the search criteria using supported keywords and syntax to filter emails. Refer to our detailed documentation for full search syntax and examples: [Email Documentation](https://docs.camunda.io/docs/components/connectors/out-of-the-box-connectors/email/).",
"Define the search criteria using supported keywords and syntax to filter emails.",
description =
"Refer to our detailed documentation for full search syntax and examples: [Email Documentation](https://docs.camunda.io/docs/components/connectors/out-of-the-box-connectors/email/).",
type = TemplateProperty.PropertyType.Text,
feel = Property.FeelMode.required,
optional = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ public record SmtpSendEmail(
label = "From",
group = "sendEmailSmtp",
id = "smtpFrom",
tooltip =
"Comma-separated list of email, e.g., '[email protected],[email protected]' or '=[ \"[email protected]\", \"[email protected]\"]'",
tooltip = "Address the email will be sent from",
feel = Property.FeelMode.optional,
binding = @TemplateProperty.PropertyBinding(name = "data.smtpAction.from"))
@Valid
@NotNull
Object from,
String from,
@TemplateProperty(
label = "To",
group = "sendEmailSmtp",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@
*/
package io.camunda.connector.email.response;

import java.util.List;

public record ListEmailsResponse(
String messageId, List<String> fromAddresses, String subject, Integer size) {}
String messageId, String fromAddress, String subject, Integer size) {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
*/
package io.camunda.connector.email.response;

import io.camunda.connector.email.client.jakarta.Header;
import java.time.OffsetDateTime;
import java.util.List;

public record ReadEmailResponse(
String messageId,
List<String> fromAddresses,
String fromAddress,
List<Header> headers,
String subject,
Integer size,
String plainTextBody,
String htmlBody) {}
String htmlBody,
OffsetDateTime receivedDateTime) {}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ void executePop3ListEmails() throws MessagingException {
null,
"1",
"",
List.of(""),
List.of(),
"",
List.of(""),
List.of(""),
OffsetDateTime.now(),
Expand Down Expand Up @@ -187,7 +188,8 @@ void executeImapListEmails() throws MessagingException {
null,
"1",
"",
List.of(""),
List.of(),
"",
List.of(""),
List.of(""),
OffsetDateTime.now(),
Expand Down Expand Up @@ -242,7 +244,8 @@ void executePop3ReadEmail() throws MessagingException, IOException {
new EmailBody("", "", List.of()),
"1",
"",
List.of(""),
List.of(),
"",
List.of(""),
List.of(""),
OffsetDateTime.now(),
Expand Down Expand Up @@ -296,7 +299,8 @@ void executeImapReadEmail() throws MessagingException, IOException {
EmailBody.createBuilder().build(),
"1",
"",
List.of(""),
List.of(),
"",
List.of(""),
List.of(""),
OffsetDateTime.now(),
Expand Down Expand Up @@ -435,7 +439,8 @@ void executePop3SearchEmails() throws MessagingException, IOException {
null,
"1",
"",
List.of(""),
List.of(),
"",
List.of(""),
List.of(""),
OffsetDateTime.now(),
Expand Down Expand Up @@ -490,7 +495,8 @@ void executeImapSearchEmails() throws MessagingException, IOException {
null,
"1",
"",
List.of(""),
List.of(),
"",
List.of(""),
List.of(""),
OffsetDateTime.now(),
Expand Down
Loading

0 comments on commit 3865e40

Please sign in to comment.