Introduction

Get instant Slack notifications when important emails arrive. This tutorial shows you how to connect MailWebhook to Slack.

Prerequisites

  • A MailWebhook account with a connected mailbox
  • A Slack workspace with permission to create webhooks

Step 1: Create a Slack Webhook

  1. Go to your Slack App settings
  2. Enable Incoming Webhooks
  3. Create a new webhook for your channel
  4. Copy the webhook URL

Step 2: Build the Integration

Create a webhook handler that forwards to Slack:

const SLACK_WEBHOOK = process.env.SLACK_WEBHOOK_URL

app.post('/webhooks/email', async (req, res) => {
  const { from, subject, body } = req.body
  
  await fetch(SLACK_WEBHOOK, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      blocks: [
        {
          type: 'header',
          text: {
            type: 'plain_text',
            text: 'New Email Received'
          }
        },
        {
          type: 'section',
          fields: [
            {
              type: 'mrkdwn',
              text: `*From:*\n${from.email}`
            },
            {
              type: 'mrkdwn',
              text: `*Subject:*\n${subject}`
            }
          ]
        },
        {
          type: 'section',
          text: {
            type: 'mrkdwn',
            text: body.text.substring(0, 300) + '...'
          }
        }
      ]
    })
  })
  
  res.status(200).json({ sent: true })
})

Customizing Notifications

You can filter which emails trigger Slack notifications:

function shouldNotify(email) {
  const importantSenders = ['ceo@company.com', 'vip@client.com']
  return importantSenders.includes(email.from.email)
}

Conclusion

Now you’ll get instant Slack notifications for your important emails. Customize the formatting to match your team’s needs.