n8n-nodes-imap-ai
Version:
Simplified IMAP node for n8n with AI-agent support. Clean and modular email and mailbox management for automation workflows.
155 lines • 5.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmailSearchAI = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const CredentialsSelector_1 = require("../Imap/utils/CredentialsSelector");
const ImapUtils_1 = require("../Imap/utils/ImapUtils");
class EmailSearchAI {
constructor() {
this.description = {
displayName: 'EmailSearchAI',
name: 'emailSearchAi',
icon: 'file:node-imap-icon.svg',
group: ['main'],
version: 1,
subtitle: 'AI-optimized email search',
description: 'Simple email search with minimal parameters - designed for AI agents',
defaults: {
name: 'EmailSearchAI',
},
inputs: ["main"],
outputs: ["main"],
usableAsTool: true,
credentials: [
{
name: 'imapApi',
required: true,
},
],
properties: [
{
displayName: 'From Email',
name: 'from',
type: 'string',
default: '',
description: 'Email address of the sender to search for',
},
{
displayName: 'Subject Contains',
name: 'subject',
type: 'string',
default: '',
description: 'Keywords to search in email subject line',
},
{
displayName: 'Max Results',
name: 'max_results',
type: 'number',
default: 1,
typeOptions: {
minValue: 1,
maxValue: 5,
},
description: 'Maximum number of emails to return (1-5)',
},
],
};
this.methods = {
credentialTest: {
async testImapCredentials(credential) {
try {
const credentials = credential.data;
const client = (0, ImapUtils_1.createImapClient)(credentials, undefined, false);
await client.connect();
await client.logout();
}
catch (error) {
return {
status: 'Error',
message: error.message,
};
}
return {
status: 'OK',
message: 'Connection successful!',
};
},
},
};
}
async execute() {
var _a, _b, _c;
const items = this.getInputData();
const returnData = [];
for (let i = 0; i < items.length; i++) {
try {
const from = this.getNodeParameter('from', i);
const subject = this.getNodeParameter('subject', i);
const maxResults = this.getNodeParameter('max_results', i);
const credentials = await (0, CredentialsSelector_1.getImapCredentials)(this);
const client = (0, ImapUtils_1.createImapClient)(credentials, undefined, false);
await client.connect();
const searchCriteria = {};
if (from) {
searchCriteria.from = from;
}
if (subject) {
searchCriteria.subject = subject;
}
const lock = await client.getMailboxLock('INBOX');
const emails = [];
try {
const messages = client.fetch(searchCriteria, {
envelope: true,
uid: true,
});
let count = 0;
for await (const message of messages) {
if (count >= maxResults)
break;
if (message.envelope) {
emails.push({
uid: message.uid,
date: ((_a = message.envelope.date) === null || _a === void 0 ? void 0 : _a.toISOString().split('T')[0]) || '',
from: ((_c = (_b = message.envelope.from) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.address) || '',
subject: message.envelope.subject || '',
});
}
count++;
}
}
finally {
lock.release();
}
await client.logout();
returnData.push({
json: {
emails,
total: emails.length,
search_criteria: {
from: from || null,
subject: subject || null,
max_results: maxResults,
},
},
});
}
catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: {
error: error.message,
emails: [],
total: 0,
},
});
continue;
}
throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: error.message });
}
}
return [returnData];
}
}
exports.EmailSearchAI = EmailSearchAI;
//# sourceMappingURL=EmailSearchAI.node.js.map