MCPcopy
hub / github.com/simstudioai/sim / processEmails

Function processEmails

apps/sim/lib/webhooks/polling/imap.ts:478–608  ·  view source on GitHub ↗
(
  emails: Array<{
    uid: number
    mailboxPath: string
    envelope: FetchMessageObject['envelope']
    bodyStructure: FetchMessageObject['bodyStructure']
    source?: Buffer
  }>,
  webhookData: PollWebhookContext['webhookData'],
  workflowData: PollWebhookContext['workflowData'],
  config: ImapWebhookConfig,
  client: ImapFlow,
  requestId: string,
  logger: Logger
)

Source from the content-addressed store, hash-verified

476}
477
478async function processEmails(
479 emails: Array<{
480 uid: number
481 mailboxPath: string
482 envelope: FetchMessageObject['envelope']
483 bodyStructure: FetchMessageObject['bodyStructure']
484 source?: Buffer
485 }>,
486 webhookData: PollWebhookContext['webhookData'],
487 workflowData: PollWebhookContext['workflowData'],
488 config: ImapWebhookConfig,
489 client: ImapFlow,
490 requestId: string,
491 logger: Logger
492) {
493 let processedCount = 0
494 let failedCount = 0
495
496 let currentOpenMailbox: string | null = null
497 const lockState: { lock: MailboxLockObject | null } = { lock: null }
498
499 try {
500 for (const email of emails) {
501 try {
502 await pollingIdempotency.executeWithIdempotency(
503 'imap',
504 `${webhookData.id}:${email.mailboxPath}:${email.uid}`,
505 async () => {
506 const envelope = email.envelope
507
508 const { text: bodyText, html: bodyHtml } = email.source
509 ? extractTextFromSource(email.source)
510 : { text: '', html: '' }
511
512 let attachments: ImapAttachment[] = []
513 const hasAttachments = hasAttachmentsInBodyStructure(email.bodyStructure)
514
515 if (config.includeAttachments && hasAttachments && email.source) {
516 attachments = extractAttachmentsFromSource(email.source, email.bodyStructure)
517 }
518
519 const simplifiedEmail: SimplifiedImapEmail = {
520 uid: String(email.uid),
521 messageId: envelope?.messageId || '',
522 subject: envelope?.subject || '[No Subject]',
523 from: parseEmailAddress(envelope?.from),
524 to: parseEmailAddress(envelope?.to),
525 cc: parseEmailAddress(envelope?.cc),
526 date: envelope?.date ? new Date(envelope.date).toISOString() : null,
527 bodyText,
528 bodyHtml,
529 mailbox: email.mailboxPath,
530 hasAttachments,
531 attachments,
532 }
533
534 const payload: ImapWebhookPayload = {
535 messageId: simplifiedEmail.messageId,

Callers 1

pollWebhookFunction · 0.70

Calls 11

getErrorMessageFunction · 0.90
extractTextFromSourceFunction · 0.85
parseEmailAddressFunction · 0.85
errorMethod · 0.80
releaseMethod · 0.80
infoMethod · 0.80
warnMethod · 0.65

Tested by

no test coverage detected