imap-trigger.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * @node imap-trigger
  3. * @name IMAP Email Trigger
  4. * @category email
  5. * @version 1.0.1
  6. * @description Triggers when new emails are received. Polls the IMAP server for new messages.
  7. * @trigger
  8. * @icon mail
  9. */
  10. const configSchema = {
  11. type: 'object',
  12. properties: {
  13. credentialId: {
  14. type: 'string',
  15. title: 'IMAP Credential',
  16. description: 'Select the IMAP credential to use for authentication',
  17. dynamicOptions: {
  18. source: 'credentials',
  19. filter: { type: 'imap' }
  20. }
  21. },
  22. mailbox: {
  23. type: 'string',
  24. title: 'Mailbox',
  25. description: 'The mailbox to monitor (e.g., INBOX)',
  26. default: 'INBOX'
  27. },
  28. filterUnread: {
  29. type: 'boolean',
  30. title: 'Only Unread',
  31. description: 'Only trigger on unread (unseen) emails',
  32. default: true
  33. },
  34. filterFrom: {
  35. type: 'string',
  36. title: 'Filter by From',
  37. description: 'Only trigger on emails from this address (optional)'
  38. },
  39. filterSubject: {
  40. type: 'string',
  41. title: 'Filter by Subject',
  42. description: 'Only trigger on emails containing this text in subject (optional)'
  43. },
  44. markAsRead: {
  45. type: 'boolean',
  46. title: 'Mark as Read',
  47. description: 'Mark emails as read after processing',
  48. default: true
  49. },
  50. limit: {
  51. type: 'integer',
  52. title: 'Max Emails',
  53. description: 'Maximum number of emails to process per trigger',
  54. default: 10,
  55. minimum: 1,
  56. maximum: 100
  57. }
  58. },
  59. required: ['credentialId']
  60. };
  61. const outputs = [
  62. { name: 'main', displayName: 'Email Received', type: 'object', color: '#22c55e' }
  63. ];
  64. module.exports = {
  65. configSchema,
  66. outputs,
  67. async execute(config, input, context) {
  68. const {
  69. credentialId,
  70. mailbox = 'INBOX',
  71. filterUnread = true,
  72. filterFrom,
  73. filterSubject,
  74. markAsRead = true,
  75. limit = 10
  76. } = config;
  77. if (!credentialId) {
  78. throw new Error('IMAP credential is required');
  79. }
  80. // Build search criteria
  81. let criteria = filterUnread ? 'UNSEEN' : 'ALL';
  82. if (filterFrom) {
  83. criteria += ` FROM "${filterFrom}"`;
  84. }
  85. if (filterSubject) {
  86. criteria += ` SUBJECT "${filterSubject}"`;
  87. }
  88. // Search for emails
  89. const searchResult = smartbotic.imap.search({
  90. credentialId,
  91. mailbox,
  92. criteria,
  93. limit
  94. });
  95. if (!searchResult.success) {
  96. throw new Error(`IMAP search failed: ${searchResult.error}`);
  97. }
  98. const emails = [];
  99. // Fetch each email
  100. for (const uid of searchResult.uids) {
  101. const fetchResult = smartbotic.imap.fetch({
  102. credentialId,
  103. mailbox,
  104. uid
  105. });
  106. if (fetchResult.success) {
  107. emails.push({
  108. uid: fetchResult.uid,
  109. subject: fetchResult.subject,
  110. from: fetchResult.from,
  111. to: fetchResult.to,
  112. date: fetchResult.date,
  113. body: fetchResult.body
  114. });
  115. // Mark as read if configured
  116. if (markAsRead) {
  117. smartbotic.imap.modify({
  118. credentialId,
  119. mailbox,
  120. uid,
  121. action: 'markRead'
  122. });
  123. }
  124. }
  125. }
  126. return {
  127. emails,
  128. count: emails.length,
  129. mailbox,
  130. timestamp: Date.now()
  131. };
  132. }
  133. };