Transactional Email API Guide: Verification, Password Reset, and Notifications

Transactional emails are personalized emails triggered by user actions, such as registration verification, password reset, and order confirmations. Unlike marketing emails, transactional emails are "expected" by users, resulting in 70-90% open rates.

1. Transactional vs Marketing Email

Feature Transactional Marketing
Trigger User action Scheduled batch
Personalization High Medium
Open rate 70-90% 20-30%
Frequency On demand Scheduled
Unsubscribe Should not include Must include
Delivery requirement Must deliver Best effort

2. Common Email Types

2.1 Registration Verification

const crypto = require('crypto');
function generateVerificationToken(userId) {
  const token = crypto.randomBytes(32).toString('hex');
  const expiresAt = Date.now() + 24 * 60 * 60 * 1000;
  saveToken({ userId, token, expiresAt, type: 'email_verification' });
  return token;
}

2.2 Password Reset

async function requestPasswordReset(email) {
  const user = await findUserByEmail(email);
  if (!user) return { success: true }; // Don't reveal user existence
  const token = crypto.randomBytes(32).toString('hex');
  const resetUrl = `https://yourdomain.com/reset-password?token=${token}`;
  await emailService.send({
    to: email,
    template: 'password-reset',
    data: { username: user.name, resetUrl },
  });
}

2.3 Order Notifications

Send order confirmation, shipping notification, delivery confirmation.

3. Template Management

  • One template per scenario
  • Parameterized with placeholders
  • Multi-language support
  • Responsive design
  • Plain text version alongside HTML

4. System Design

4.1 Queue Architecture

App Service → Message Queue (Redis/RabbitMQ) → Email Worker → Email API → Logging & Monitoring

Benefits: Async processing, retry mechanism, rate limiting, horizontal scaling

5. Security

5.1 Rate Limiting

function checkRateLimit(email, type) {
  const key = `${email}:${type}`;
  const windowMs = 60 * 1000;
  const maxAttempts = 3;
  // Check and enforce limits
}

5.2 Token Security

  • Use crypto.randomBytes for token generation
  • Set reasonable expiration
  • Invalidate tokens after use
  • Track failed attempts

6. Monitoring

Metric Alert Threshold Description
Send failure rate > 3% Rejected or timed out
Latency > 5 minutes Send to delivery delay
Bounce rate > 2% Invalid addresses
Queue backlog > 1000 Queue堆积

7. Summary

Transactional emails are critical for user communication. Use mature third-party services, implement queues and retry mechanisms, and don't forget monitoring.