n8n-nodes-imap-ai
Version:
Simplified IMAP node for n8n with AI-agent support. Clean and modular email and mailbox management for automation workflows.
237 lines • 9.75 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDraftOperation = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const nodemailer = __importStar(require("nodemailer"));
const SearchFieldParameters_1 = require("../../../utils/SearchFieldParameters");
const PARAM_NAME_DESTINATION_MAILBOX = 'destinationMailbox';
exports.createDraftOperation = {
operation: {
name: 'Create Draft',
value: 'createDraft',
description: 'Create draft emails for later sending or review. Perfect for AI agents to compose automated responses, prepare emails for approval, or create template drafts based on data processing.',
},
parameters: [
{
...SearchFieldParameters_1.parameterSelectMailbox,
description: 'Select the mailbox where the draft will be stored. AI agents can specify: Drafts, INBOX, or custom folder names.',
name: PARAM_NAME_DESTINATION_MAILBOX,
},
{
displayName: 'Input Format',
name: 'inputFormat',
type: 'options',
options: [
{
name: 'Fields',
value: 'fields',
},
{
name: 'RFC822 Formatted Email',
value: 'rfc822',
},
],
default: 'fields',
description: 'Select the input format of the email content. AI can choose based on complexity needs.',
},
{
displayName: 'Use <a href="https://github.com/umanamente/n8n-nodes-eml" target="_blank"><pre>n8n-nodes-eml</pre></a> to compose complex emails. ' +
'It supports attachments and other features. ' +
'Then use RFC822 input format provided by that node.',
name: 'noticeSlowResponse',
type: 'notice',
default: '',
displayOptions: {
show: {
inputFormat: ['fields'],
},
},
},
{
displayName: 'Subject',
name: 'subject',
type: 'string',
default: "={{ $fromAI('email_subject', 'Subject line for the draft email') }}",
description: 'The subject of the email. AI can generate contextual subjects based on content or purpose.',
placeholder: 'Meeting Summary | Invoice Request | Follow-up',
displayOptions: {
show: {
inputFormat: ['fields'],
},
},
},
{
displayName: 'From',
name: 'from',
type: 'string',
default: "={{ $fromAI('sender_email', 'Email address of the sender') }}",
description: 'The email address of the sender. AI can use configured sender addresses.',
placeholder: 'sender@company.com',
displayOptions: {
show: {
inputFormat: ['fields'],
},
},
},
{
displayName: 'To',
name: 'to',
type: 'string',
default: "={{ $fromAI('recipient_email', 'Email address of the recipient') }}",
description: 'The email address of the recipient. AI can extract from previous email threads.',
placeholder: 'recipient@company.com',
displayOptions: {
show: {
inputFormat: ['fields'],
},
},
},
{
displayName: 'Text',
name: 'text',
type: 'string',
default: "={{ $fromAI('email_content', 'Text content of the email message') }}",
description: 'The text content of the email. AI can generate personalized content based on data and context.',
placeholder: 'Dear [Name], Thank you for...',
typeOptions: {
rows: 5,
},
displayOptions: {
show: {
inputFormat: ['fields'],
},
},
},
{
displayName: 'RFC822 Formatted Email',
name: 'rfc822',
type: 'string',
default: '',
required: true,
typeOptions: {
rows: 10,
},
displayOptions: {
show: {
inputFormat: ['rfc822'],
},
},
},
],
async executeImapAction(context, itemIndex, client) {
const returnData = [];
const destinationMailboxPath = (0, SearchFieldParameters_1.getMailboxPathFromNodeParameter)(context, itemIndex, PARAM_NAME_DESTINATION_MAILBOX);
const inputFormat = context.getNodeParameter('inputFormat', itemIndex);
let rfc822Content = '';
if (inputFormat === 'rfc822') {
rfc822Content = context.getNodeParameter('rfc822', itemIndex);
}
else {
const transporter = nodemailer.createTransport({
streamTransport: true,
buffer: true,
newline: 'unix',
});
const subject = context.getNodeParameter('subject', itemIndex);
const from = context.getNodeParameter('from', itemIndex);
const to = context.getNodeParameter('to', itemIndex);
const text = context.getNodeParameter('text', itemIndex);
if (!to || to.trim() === '') {
throw new n8n_workflow_1.NodeApiError(context.getNode(), {}, {
message: 'To field is required for creating email drafts. Please provide a recipient email address.',
description: 'AI agents must specify a valid recipient email address when creating drafts.',
});
}
if (!from || from.trim() === '') {
throw new n8n_workflow_1.NodeApiError(context.getNode(), {}, {
message: 'From field is required for creating email drafts. Please provide a sender email address.',
description: 'AI agents must specify a valid sender email address when creating drafts.',
});
}
const json_data = {
from: from.trim(),
to: to.trim(),
subject: subject || 'Draft Email',
text: text || '',
};
const promise_compose_rfc822 = new Promise((resolve, reject) => {
transporter.sendMail(json_data, (err, info) => {
if (err) {
resolve({
error: err,
info: null,
});
return;
}
if (!info.envelope || !info.messageId || !info.message) {
resolve({
error: new Error(`Invalid info object returned by nodemailer. Expected fields: envelope, messageId, message. Found: ${JSON.stringify(info)}`),
info: null,
});
return;
}
const result = {
error: null,
info: info,
};
resolve(result);
});
});
const result = await promise_compose_rfc822;
if (result.error) {
throw new n8n_workflow_1.NodeApiError(context.getNode(), {}, {
message: `Error composing the email: ${result.error}`,
});
}
rfc822Content = result.info.message.toString('utf8');
}
await client.mailboxOpen(destinationMailboxPath, { readOnly: false });
const resp = await client.append(destinationMailboxPath, rfc822Content, [
'\\Draft',
]);
if (!resp) {
throw new n8n_workflow_1.NodeApiError(context.getNode(), {}, {
message: 'Unable to create draft, unknown error',
});
}
const item_json = JSON.parse(JSON.stringify(resp));
returnData.push({
json: item_json,
});
return returnData;
},
};
//# sourceMappingURL=EmailCreateDraft.js.map