| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- /**
- * @node imap-fetch
- * @name IMAP Fetch Email
- * @category email
- * @version 1.2.0
- * @description Fetch the full content of an email from an IMAP server.
- * @icon download
- */
- const configSchema = {
- type: 'object',
- properties: {
- credentialId: {
- type: 'string',
- title: 'IMAP Credential',
- description: 'Select the IMAP credential to use for authentication',
- dynamicOptions: {
- source: 'credentials',
- filter: { type: 'imap' }
- }
- },
- mailbox: {
- type: 'string',
- title: 'Mailbox',
- description: 'The mailbox containing the email',
- default: 'INBOX'
- },
- uid: {
- type: 'string',
- title: 'Email UID',
- description: 'The unique identifier of the email to fetch (can be provided from input)'
- },
- peek: {
- type: 'boolean',
- title: 'Peek (Don\'t Mark as Read)',
- description: 'Fetch without marking the email as read',
- default: true
- }
- },
- required: ['credentialId']
- };
- const inputSchema = {
- type: 'object',
- properties: {
- uid: {
- type: 'string',
- description: 'Email UID to fetch'
- },
- mailbox: {
- type: 'string',
- description: 'Override mailbox from input'
- }
- }
- };
- const outputSchema = {
- type: 'object',
- properties: {
- uid: { type: 'string', description: 'Unique email identifier' },
- subject: { type: 'string' },
- from: { type: 'string' },
- to: { type: 'string' },
- date: { type: 'string' },
- body: { type: 'string', description: 'Email body text' },
- raw: { type: 'string', description: 'Raw email content (for attachment extraction)' },
- hasAttachments: { type: 'boolean', description: 'Whether email has attachments' },
- mailbox: { type: 'string' }
- }
- };
- const outputs = [
- { name: 'main', displayName: 'Email Content', type: 'object', color: '#8b5cf6' }
- ];
- /**
- * Check if email has attachments by looking for Content-Disposition: attachment
- * or multipart content types
- */
- function detectAttachments(rawEmail) {
- if (!rawEmail) return false;
- const lower = rawEmail.toLowerCase();
- return lower.includes('content-disposition: attachment') ||
- lower.includes('content-disposition:attachment') ||
- (lower.includes('multipart/mixed') && lower.includes('boundary='));
- }
- module.exports = {
- configSchema,
- inputSchema,
- outputSchema,
- outputs,
- async execute(config, input, context) {
- const credentialId = config.credentialId;
- const mailbox = input.mailbox || config.mailbox || 'INBOX';
- const uid = input.uid || config.uid;
- const peek = config.peek !== false; // Default to true (don't mark as read)
- if (!credentialId) {
- throw new Error('IMAP credential is required');
- }
- if (!uid) {
- throw new Error('Email UID is required');
- }
- // Fetch the email
- const fetchResult = smartbotic.imap.fetch({
- credentialId,
- mailbox,
- uid,
- peek
- });
- if (!fetchResult.success) {
- throw new Error(`IMAP fetch failed: ${fetchResult.error}`);
- }
- return {
- uid: fetchResult.uid,
- subject: fetchResult.subject,
- from: fetchResult.from,
- to: fetchResult.to,
- date: fetchResult.date,
- body: fetchResult.body,
- raw: fetchResult.raw,
- hasAttachments: detectAttachments(fetchResult.raw),
- mailbox
- };
- }
- };
|