imap-fetch.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @node imap-fetch
  3. * @name IMAP Fetch Email
  4. * @category email
  5. * @version 1.0.1
  6. * @description Fetch the full content of an email from an IMAP server.
  7. * @icon download
  8. */
  9. const configSchema = {
  10. type: 'object',
  11. properties: {
  12. credentialId: {
  13. type: 'string',
  14. title: 'IMAP Credential',
  15. description: 'Select the IMAP credential to use for authentication',
  16. dynamicOptions: {
  17. source: 'credentials',
  18. filter: { type: 'imap' }
  19. }
  20. },
  21. mailbox: {
  22. type: 'string',
  23. title: 'Mailbox',
  24. description: 'The mailbox containing the email',
  25. default: 'INBOX'
  26. },
  27. uid: {
  28. type: 'string',
  29. title: 'Email UID',
  30. description: 'The unique identifier of the email to fetch (can be provided from input)'
  31. },
  32. includeRaw: {
  33. type: 'boolean',
  34. title: 'Include Raw',
  35. description: 'Include the raw email content in the output',
  36. default: false
  37. }
  38. },
  39. required: ['credentialId']
  40. };
  41. const inputSchema = {
  42. type: 'object',
  43. properties: {
  44. uid: {
  45. type: 'string',
  46. description: 'Email UID to fetch'
  47. },
  48. mailbox: {
  49. type: 'string',
  50. description: 'Override mailbox from input'
  51. }
  52. }
  53. };
  54. const outputSchema = {
  55. type: 'object',
  56. properties: {
  57. uid: { type: 'string' },
  58. subject: { type: 'string' },
  59. from: { type: 'string' },
  60. to: { type: 'string' },
  61. date: { type: 'string' },
  62. body: { type: 'string' },
  63. raw: { type: 'string' }
  64. }
  65. };
  66. const outputs = [
  67. { name: 'main', displayName: 'Email Content', type: 'object', color: '#8b5cf6' }
  68. ];
  69. module.exports = {
  70. configSchema,
  71. inputSchema,
  72. outputSchema,
  73. outputs,
  74. async execute(config, input, context) {
  75. const credentialId = config.credentialId;
  76. const mailbox = input.mailbox || config.mailbox || 'INBOX';
  77. const uid = input.uid || config.uid;
  78. const includeRaw = config.includeRaw || false;
  79. if (!credentialId) {
  80. throw new Error('IMAP credential is required');
  81. }
  82. if (!uid) {
  83. throw new Error('Email UID is required');
  84. }
  85. // Fetch the email
  86. const fetchResult = smartbotic.imap.fetch({
  87. credentialId,
  88. mailbox,
  89. uid
  90. });
  91. if (!fetchResult.success) {
  92. throw new Error(`IMAP fetch failed: ${fetchResult.error}`);
  93. }
  94. const result = {
  95. uid: fetchResult.uid,
  96. subject: fetchResult.subject,
  97. from: fetchResult.from,
  98. to: fetchResult.to,
  99. date: fetchResult.date,
  100. body: fetchResult.body,
  101. mailbox
  102. };
  103. if (includeRaw) {
  104. result.raw = fetchResult.raw;
  105. }
  106. return result;
  107. }
  108. };