UNPKG

n8n-nodes-imap-ai

Version:

Simplified IMAP node for n8n with AI-agent support. Clean and modular email and mailbox management for automation workflows.

113 lines 5.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CreateDraftOperation = void 0; const n8n_workflow_1 = require("n8n-workflow"); const helpers_1 = require("../utils/helpers"); class CreateDraftOperation { async execute(executeFunctions, client, itemIndex) { const draftFolderParam = executeFunctions.getNodeParameter('draftFolder', itemIndex); const draftFolder = helpers_1.ParameterValidator.extractMailboxName(draftFolderParam); const to = executeFunctions.getNodeParameter('to', itemIndex); const subject = executeFunctions.getNodeParameter('subject', itemIndex, ''); const messageFormat = executeFunctions.getNodeParameter('messageFormat', itemIndex, 'text'); const cc = executeFunctions.getNodeParameter('cc', itemIndex, ''); const bcc = executeFunctions.getNodeParameter('bcc', itemIndex, ''); const fromName = executeFunctions.getNodeParameter('fromName', itemIndex, ''); helpers_1.ParameterValidator.validateMailbox(draftFolderParam); if (!to.trim()) { throw new n8n_workflow_1.NodeApiError(executeFunctions.getNode(), { message: 'To field is required and cannot be empty', }); } let textBody = ''; let htmlBody = ''; if (messageFormat === 'text' || messageFormat === 'both') { textBody = executeFunctions.getNodeParameter('textBody', itemIndex, ''); } if (messageFormat === 'html' || messageFormat === 'both') { htmlBody = executeFunctions.getNodeParameter('htmlBody', itemIndex, ''); } try { await client.mailboxOpen(draftFolder); } catch (error) { try { await client.mailboxCreate(draftFolder); await client.mailboxOpen(draftFolder); } catch (createError) { throw new n8n_workflow_1.NodeApiError(executeFunctions.getNode(), { message: `Failed to access or create draft folder ${draftFolder}: ${createError.message}`, }); } } const messageLines = []; messageLines.push(`Date: ${new Date().toUTCString()}`); if (fromName.trim()) { const credentials = await executeFunctions.getCredentials('imap'); const fromEmail = credentials.user; messageLines.push(`From: "${fromName.trim()}" <${fromEmail}>`); } messageLines.push(`To: ${to.trim()}`); if (cc.trim()) { messageLines.push(`CC: ${cc.trim()}`); } if (bcc.trim()) { messageLines.push(`BCC: ${bcc.trim()}`); } messageLines.push(`Subject: ${subject}`); messageLines.push('MIME-Version: 1.0'); if (messageFormat === 'both' && textBody && htmlBody) { const boundary = `----=_Part_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; messageLines.push(`Content-Type: multipart/alternative; boundary="${boundary}"`); messageLines.push(''); messageLines.push(`--${boundary}`); messageLines.push('Content-Type: text/plain; charset=utf-8'); messageLines.push('Content-Transfer-Encoding: 8bit'); messageLines.push(''); messageLines.push(textBody); messageLines.push(''); messageLines.push(`--${boundary}`); messageLines.push('Content-Type: text/html; charset=utf-8'); messageLines.push('Content-Transfer-Encoding: 8bit'); messageLines.push(''); messageLines.push(htmlBody); messageLines.push(''); messageLines.push(`--${boundary}--`); } else if (messageFormat === 'html' || (messageFormat === 'both' && htmlBody)) { messageLines.push('Content-Type: text/html; charset=utf-8'); messageLines.push('Content-Transfer-Encoding: 8bit'); messageLines.push(''); messageLines.push(htmlBody || textBody); } else { messageLines.push('Content-Type: text/plain; charset=utf-8'); messageLines.push('Content-Transfer-Encoding: 8bit'); messageLines.push(''); messageLines.push(textBody); } const messageSource = messageLines.join('\r\n'); try { const result = await client.append(draftFolder, messageSource, ['\\Draft']); return { success: true, message: `Draft created successfully in ${draftFolder}`, draftFolder: draftFolder, to: to, subject: subject, messageFormat: messageFormat, uid: (result === null || result === void 0 ? void 0 : result.uid) || null, size: messageSource.length, created: new Date().toISOString(), }; } catch (error) { throw new n8n_workflow_1.NodeApiError(executeFunctions.getNode(), { message: `Failed to create draft: ${error.message}`, }); } } } exports.CreateDraftOperation = CreateDraftOperation; //# sourceMappingURL=createDraft.js.map