n8n-nodes-imap-ai
Version:
Simplified IMAP node for n8n with AI-agent support. Clean and modular email and mailbox management for automation workflows.
133 lines • 5.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.setEmailFlagsOperation = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const SearchFieldParameters_1 = require("../../../utils/SearchFieldParameters");
var ImapFlags;
(function (ImapFlags) {
ImapFlags["Answered"] = "\\Answered";
ImapFlags["Flagged"] = "\\Flagged";
ImapFlags["Deleted"] = "\\Deleted";
ImapFlags["Seen"] = "\\Seen";
ImapFlags["Draft"] = "\\Draft";
})(ImapFlags || (ImapFlags = {}));
exports.setEmailFlagsOperation = {
operation: {
name: 'Set Flags',
value: 'setEmailFlags',
description: 'Set or remove email flags like "Read/Unread", "Flagged", "Answered" etc. Perfect for AI agents to organize emails, mark as read, flag important messages, or manage email status automatically.',
},
parameters: [
{
...SearchFieldParameters_1.parameterSelectMailbox,
description: 'Select the mailbox containing the emails to modify. AI agents can specify: INBOX, Sent, Drafts, or custom folder names.',
},
{
displayName: 'Email UID',
name: 'emailUid',
type: 'string',
default: "={{ $fromAI('email_uid', 'UID of the email or comma-separated list of email UIDs to modify flags for') }}",
description: 'UID of the email to set flags on. AI can specify single UID or comma-separated list for bulk operations.',
placeholder: '123 or 123,456,789',
hint: 'You can use comma separated list of UIDs to modify multiple emails at once',
},
{
displayName: 'Email Flags to Modify',
name: 'flags',
type: 'collection',
default: {},
required: true,
placeholder: 'Add Flag to Modify',
description: 'Choose which email flags to set or remove. AI agents can intelligently set these based on email processing needs.',
options: [
{
displayName: 'Answered',
name: ImapFlags.Answered,
type: 'boolean',
default: false,
description: 'Whether email is marked as answered/replied to',
},
{
displayName: 'Deleted',
name: ImapFlags.Deleted,
type: 'boolean',
default: false,
description: 'Whether email is marked for deletion',
},
{
displayName: 'Draft',
name: ImapFlags.Draft,
type: 'boolean',
default: false,
description: 'Whether email is marked as draft',
},
{
displayName: 'Flagged',
name: ImapFlags.Flagged,
type: 'boolean',
default: false,
description: 'Whether email is flagged as important',
},
{
displayName: 'Seen',
name: ImapFlags.Seen,
type: 'boolean',
default: false,
description: 'Whether email is marked as read/seen',
},
],
},
],
async executeImapAction(context, itemIndex, client) {
var _a;
const returnData = [];
const mailboxPath = (0, SearchFieldParameters_1.getMailboxPathFromNodeParameter)(context, itemIndex);
const emailUid = context.getNodeParameter('emailUid', itemIndex);
const flags = context.getNodeParameter('flags', itemIndex);
const flagsToSet = [];
const flagsToRemove = [];
for (const flagName in flags) {
if (flags[flagName]) {
flagsToSet.push(flagName);
}
else {
flagsToRemove.push(flagName);
}
}
(_a = context.logger) === null || _a === void 0 ? void 0 : _a.info(`Setting flags "${flagsToSet.join(',')}" and removing flags "${flagsToRemove.join(',')}" on email "${emailUid}" in mailbox "${mailboxPath}"`);
await client.mailboxOpen(mailboxPath, { readOnly: false });
if (flagsToSet.length > 0) {
const isSuccess = await client.messageFlagsAdd(emailUid, flagsToSet, {
uid: true,
});
if (!isSuccess) {
throw new n8n_workflow_1.NodeApiError(context.getNode(), {}, {
message: `Unable to set flags ${flagsToSet.join(', ')} on email UID ${emailUid}`,
});
}
}
if (flagsToRemove.length > 0) {
const isSuccess = await client.messageFlagsRemove(emailUid, flagsToRemove, {
uid: true,
});
if (!isSuccess) {
throw new n8n_workflow_1.NodeApiError(context.getNode(), {}, {
message: `Unable to remove flags ${flagsToRemove.join(', ')} from email UID ${emailUid}`,
});
}
}
returnData.push({
json: {
success: true,
emailUid: emailUid,
mailboxPath: mailboxPath,
flagsSet: flagsToSet,
flagsRemoved: flagsToRemove,
totalFlags: flagsToSet.length + flagsToRemove.length,
operation: 'setEmailFlags',
},
});
return returnData;
},
};
//# sourceMappingURL=EmailSetFlags.js.map