IMAP vs Webhooks

Two approaches to receiving inbound email. Which one is right for your application?

The core difference

When your application needs to receive inbound email, you typically choose between polling a mailbox (IMAP) or receiving webhooks (HTTP callbacks). The difference comes down to push vs pull.

IMAP (Pull)

Your application connects to a mail server and asks "are there new emails?" at regular intervals. You pull the data when you want it.

Webhooks (Push)

The mail server sends an HTTP request to your endpoint the moment an email arrives. The data is pushed to you immediately.

Detailed comparison

FactorIMAPWebhooks
LatencyDepends on polling interval (seconds to minutes)Real-time (milliseconds)
Connection managementMust maintain persistent connections, handle reconnectsNo connections to manage
Data formatRaw MIME format, requires parsingPre-parsed JSON payload
AttachmentsBase64 encoded within MIME, must extractDownloadable URLs
ScalingOne connection per mailboxStandard HTTP scaling
Server requirementsLong-running process for pollingServerless compatible
Implementation complexityHighLow

How IMAP works

IMAP (Internet Message Access Protocol) is the standard protocol for reading email from a mail server. To receive emails via IMAP, your application needs to:

1.Open a TCP connection to the mail server (typically port 993 with TLS)
2.Authenticate with credentials
3.Select a mailbox (e.g., INBOX)
4.Poll for new messages using IDLE or regular intervals
5.Fetch and parse MIME-encoded message content
6.Handle connection drops and reconnect

Example: IMAP with Node.js

const Imap = require('imap');
const { simpleParser } = require('mailparser');

const imap = new Imap({
  user: '[email protected]',
  password: 'your-password',
  host: 'imap.example.com',
  port: 993,
  tls: true
});

imap.on('mail', (numNewMsgs) => {
  // Fetch new messages
  const fetch = imap.seq.fetch(
    `${imap.seq.lastSync}:*`,
    { bodies: '' }
  );

  fetch.on('message', (msg) => {
    msg.on('body', (stream) => {
      simpleParser(stream, (err, parsed) => {
        // Finally, your email data
        console.log(parsed.subject);
        console.log(parsed.from);
        console.log(parsed.text);
      });
    });
  });
});

imap.connect();
// Plus error handling, reconnection logic...

How email webhooks work

With webhooks, an email service receives inbound email on your behalf and forwards it to your application as an HTTP POST request. Your application just needs an endpoint:

1.Email arrives at the service (e.g., Mailhooks)
2.Service parses the email into structured JSON
3.Service POSTs the JSON to your webhook URL
4.Your endpoint processes the email data

Example: Webhook endpoint

// That's it. No connections, no parsing.
app.post('/webhook/email', (req, res) => {
  const { from, to, subject, text, html, attachments } = req.body;

  console.log(`Email from ${from}: ${subject}`);

  // Attachments are already URLs
  for (const attachment of attachments) {
    console.log(`Download: ${attachment.url}`);
  }

  // Process the email
  await processEmail(req.body);

  res.status(200).send('OK');
});

When to use each

Consider IMAP when:

  • You need to read existing mailboxes you don't control
  • You're building an email client application
  • You need offline access to historical emails

Choose webhooks when:

  • You need to process incoming emails in real-time
  • You want to trigger workflows from email events
  • You prefer serverless or event-driven architecture
  • You don't want to manage email infrastructure

Try the webhook approach

Get your first email webhook in minutes with Mailhooks.