| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- /**
- * @node imap-trigger
- * @name IMAP Email Trigger
- * @category email
- * @version 1.0.1
- * @description Triggers when new emails are received. Polls the IMAP server for new messages.
- * @trigger
- * @icon mail
- */
- 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 to monitor (e.g., INBOX)',
- default: 'INBOX'
- },
- filterUnread: {
- type: 'boolean',
- title: 'Only Unread',
- description: 'Only trigger on unread (unseen) emails',
- default: true
- },
- filterFrom: {
- type: 'string',
- title: 'Filter by From',
- description: 'Only trigger on emails from this address (optional)'
- },
- filterSubject: {
- type: 'string',
- title: 'Filter by Subject',
- description: 'Only trigger on emails containing this text in subject (optional)'
- },
- markAsRead: {
- type: 'boolean',
- title: 'Mark as Read',
- description: 'Mark emails as read after processing',
- default: true
- },
- limit: {
- type: 'integer',
- title: 'Max Emails',
- description: 'Maximum number of emails to process per trigger',
- default: 10,
- minimum: 1,
- maximum: 100
- }
- },
- required: ['credentialId']
- };
- const outputs = [
- { name: 'main', displayName: 'Email Received', type: 'object', color: '#22c55e' }
- ];
- module.exports = {
- configSchema,
- outputs,
- async execute(config, input, context) {
- const {
- credentialId,
- mailbox = 'INBOX',
- filterUnread = true,
- filterFrom,
- filterSubject,
- markAsRead = true,
- limit = 10
- } = config;
- if (!credentialId) {
- throw new Error('IMAP credential is required');
- }
- // Build search criteria
- let criteria = filterUnread ? 'UNSEEN' : 'ALL';
- if (filterFrom) {
- criteria += ` FROM "${filterFrom}"`;
- }
- if (filterSubject) {
- criteria += ` SUBJECT "${filterSubject}"`;
- }
- // Search for emails
- const searchResult = smartbotic.imap.search({
- credentialId,
- mailbox,
- criteria,
- limit
- });
- if (!searchResult.success) {
- throw new Error(`IMAP search failed: ${searchResult.error}`);
- }
- const emails = [];
- // Fetch each email
- for (const uid of searchResult.uids) {
- const fetchResult = smartbotic.imap.fetch({
- credentialId,
- mailbox,
- uid
- });
- if (fetchResult.success) {
- emails.push({
- uid: fetchResult.uid,
- subject: fetchResult.subject,
- from: fetchResult.from,
- to: fetchResult.to,
- date: fetchResult.date,
- body: fetchResult.body
- });
- // Mark as read if configured
- if (markAsRead) {
- smartbotic.imap.modify({
- credentialId,
- mailbox,
- uid,
- action: 'markRead'
- });
- }
- }
- }
- return {
- emails,
- count: emails.length,
- mailbox,
- timestamp: Date.now()
- };
- }
- };
|