n8n-nodes-imap-ai
Version:
Simplified IMAP node for n8n with AI-agent support. Clean and modular email and mailbox management for automation workflows.
82 lines • 3.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DownloadAttachmentOperation = void 0;
const mailparser_1 = require("mailparser");
const n8n_workflow_1 = require("n8n-workflow");
const helpers_1 = require("../utils/helpers");
class DownloadAttachmentOperation {
async execute(executeFunctions, client, itemIndex) {
const mailboxParam = executeFunctions.getNodeParameter('mailbox', itemIndex);
const mailbox = helpers_1.ParameterValidator.extractMailboxName(mailboxParam);
const emailUid = executeFunctions.getNodeParameter('emailUid', itemIndex);
const attachmentIndex = executeFunctions.getNodeParameter('attachmentIndex', itemIndex);
helpers_1.ParameterValidator.validateMailbox(mailboxParam);
helpers_1.ParameterValidator.validateUid(emailUid);
try {
await client.mailboxOpen(mailbox);
}
catch (error) {
throw new n8n_workflow_1.NodeApiError(executeFunctions.getNode(), {
message: `Failed to open mailbox ${mailbox}: ${error.message}`,
});
}
let message;
try {
const searchResults = await client.search({ uid: emailUid.toString() });
if (searchResults.length === 0) {
throw new n8n_workflow_1.NodeApiError(executeFunctions.getNode(), {
message: `Email with UID ${emailUid} not found in ${mailbox}`,
});
}
const messageGenerator = client.fetch(emailUid.toString(), {
source: true,
uid: true,
}, { uid: true });
for await (const msg of messageGenerator) {
message = msg;
break;
}
if (!message) {
throw new Error(`No message data received for UID ${emailUid}`);
}
}
catch (error) {
throw new n8n_workflow_1.NodeApiError(executeFunctions.getNode(), {
message: `Failed to fetch email with UID ${emailUid}: ${error.message}`,
});
}
if (!message || !message.source) {
throw new n8n_workflow_1.NodeApiError(executeFunctions.getNode(), {
message: `Email with UID ${emailUid} not found`,
});
}
let parsed;
try {
parsed = await (0, mailparser_1.simpleParser)(message.source);
}
catch (error) {
throw new n8n_workflow_1.NodeApiError(executeFunctions.getNode(), {
message: `Failed to parse email: ${error.message}`,
});
}
if (!parsed.attachments || parsed.attachments.length === 0) {
throw new n8n_workflow_1.NodeApiError(executeFunctions.getNode(), {
message: 'No attachments found in this email',
});
}
if (attachmentIndex >= parsed.attachments.length) {
throw new n8n_workflow_1.NodeApiError(executeFunctions.getNode(), {
message: `Attachment index ${attachmentIndex} not found. Email has ${parsed.attachments.length} attachments.`,
});
}
const attachment = parsed.attachments[attachmentIndex];
return {
filename: attachment.filename,
contentType: attachment.contentType,
size: attachment.size,
content: attachment.content.toString('base64'),
};
}
}
exports.DownloadAttachmentOperation = DownloadAttachmentOperation;
//# sourceMappingURL=downloadAttachment.js.map