Skip to content

The open alternative to Salesforce, designed for AI.

fix(messaging): enforce CRLF line endings on raw outbound messages and surface IMAP append error responseText (#26011) (#26017)

Simon Enerstrand9a8a193d6210
diff --git a/packages/twenty-server/src/modules/messaging/message-outbound-manager/drivers/imap/services/imap-smtp-message-outbound.service.ts b/packages/twenty-server/src/modules/messaging/message-outbound-manager/drivers/imap/services/imap-smtp-message-outbound.service.ts
index 7c168aa8c70..74173e46533 100644
--- a/packages/twenty-server/src/modules/messaging/message-outbound-manager/drivers/imap/services/imap-smtp-message-outbound.service.ts
+++ b/packages/twenty-server/src/modules/messaging/message-outbound-manager/drivers/imap/services/imap-smtp-message-outbound.service.ts
@@ -14,6 +14,7 @@ import { type ConnectedAccountEntity } from 'src/engine/metadata-modules/connect
import { ImapClientProvider } from 'src/modules/messaging/message-import-manager/drivers/imap/providers/imap-client.provider';
import { ImapFindDraftsFolderService } from 'src/modules/messaging/message-import-manager/drivers/imap/services/imap-find-drafts-folder.service';
import { getImapFolderPath } from 'src/modules/messaging/message-import-manager/drivers/imap/utils/get-imap-folder-path.util';
+import { isImapFlowError } from 'src/modules/messaging/message-import-manager/drivers/imap/utils/is-imap-flow-error.util';
import { parseMessageId } from 'src/modules/messaging/message-import-manager/drivers/imap/utils/parse-message-id.util';
import { SmtpClientProvider } from 'src/modules/messaging/message-import-manager/drivers/smtp/providers/smtp-client.provider';
import { type SendMessageInput } from 'src/modules/messaging/message-outbound-manager/types/send-message-input.type';
@@ -149,6 +150,12 @@ export class ImapSmtpMessageOutboundService implements MessageOutboundDriver {
const DRAFT_FLAG = '\\Draft';
await imapClient.append(draftsFolder.path, messageBuffer, [DRAFT_FLAG]);
+ } catch (error) {
+ if (isImapFlowError(error) && isNonEmptyString(error.responseText)) {
+ throw new Error(`Failed to create draft: ${error.responseText}`);
+ }
+
+ throw error;
} finally {
await this.imapClientProvider.closeClient(imapClient);
}
diff --git a/packages/twenty-server/src/modules/messaging/message-outbound-manager/utils/to-mail-composer-options.util.ts b/packages/twenty-server/src/modules/messaging/message-outbound-manager/utils/to-mail-composer-options.util.ts
index 73e40ae0a98..c9d264d63a2 100644
--- a/packages/twenty-server/src/modules/messaging/message-outbound-manager/utils/to-mail-composer-options.util.ts
+++ b/packages/twenty-server/src/modules/messaging/message-outbound-manager/utils/to-mail-composer-options.util.ts
@@ -13,6 +13,7 @@ export const toMailComposerOptions = (
subject: sendMessageInput.subject,
text: sendMessageInput.body,
html: sendMessageInput.html,
+ newline: '\r\n',
...(sendMessageInput.attachments && sendMessageInput.attachments.length > 0
? {
attachments: sendMessageInput.attachments.map((attachment) => ({
diff --git a/packages/twenty-server/test/integration/workflow-actions/draft-email/drivers/imap-smtp.integration-spec.ts b/packages/twenty-server/test/integration/workflow-actions/draft-email/drivers/imap-smtp.integration-spec.ts
index 08df14f1f85..c96c25756da 100644
--- a/packages/twenty-server/test/integration/workflow-actions/draft-email/drivers/imap-smtp.integration-spec.ts
+++ b/packages/twenty-server/test/integration/workflow-actions/draft-email/drivers/imap-smtp.integration-spec.ts
@@ -26,7 +26,9 @@ describe('DRAFT_EMAIL workflow action on IMAP (integration)', () => {
let connectedAccountId: string;
let messageChannelId: string;
- const findDraftSubjects = async (): Promise<string[]> => {
+ const findDrafts = async (): Promise<
+ { subject: string; source: string }[]
+ > => {
const client = new ImapFlow({
host: greenmail.host,
port: greenmail.imapPort,
@@ -44,17 +46,23 @@ describe('DRAFT_EMAIL workflow action on IMAP (integration)', () => {
return [];
}
- const subjects: string[] = [];
+ const drafts: { subject: string; source: string }[] = [];
- for await (const message of client.fetch('1:*', { envelope: true })) {
+ for await (const message of client.fetch('1:*', {
+ envelope: true,
+ source: true,
+ })) {
const subject = message.envelope?.subject;
if (isNonEmptyString(subject)) {
- subjects.push(subject);
+ drafts.push({
+ subject,
+ source: message.source?.toString('latin1') ?? '',
+ });
}
}
- return subjects;
+ return drafts;
} finally {
await client.logout();
}
@@ -142,7 +150,9 @@ describe('DRAFT_EMAIL workflow action on IMAP (integration)', () => {
subject,
connectedAccountId,
});
- expect(await findDraftSubjects()).toContain(subject);
+ expect(await findDrafts()).toContainEqual(
+ expect.objectContaining({ subject }),
+ );
expect(
await findRecordNodesByFilter<{ id: string }>(
'message',
@@ -152,4 +162,32 @@ describe('DRAFT_EMAIL workflow action on IMAP (integration)', () => {
),
).toEqual([]);
}, 300000);
+
+ it('appends a multi-paragraph draft with CRLF line endings only', async () => {
+ const subject = `IMAP/SMTP multi-paragraph draft ${randomUUID()}`;
+
+ const workflowRun = await runWorkflowActionStep({
+ name: 'IMAP/SMTP multi-paragraph draft email workflow',
+ stepType: 'DRAFT_EMAIL',
+ input: {
+ connectedAccountId,
+ recipients: { to: '{{trigger.to}}' },
+ subject: '{{trigger.subject}}',
+ body: '<p>First paragraph</p><p>Second paragraph</p>',
+ },
+ payload: { to: HANDLE, subject },
+ });
+
+ expect(workflowRun).toMatchObject({
+ status: 'COMPLETED',
+ stepStatus: 'SUCCESS',
+ });
+
+ const draft = (await findDrafts()).find(
+ (candidate) => candidate.subject === subject,
+ );
+
+ expect(draft?.source).toContain('First paragraph\r\n\r\nSecond paragraph');
+ expect(draft?.source).not.toMatch(/[^\r]\n/);
+ }, 300000);
});