← 返回博客
批量发送群发速率限制开发者

批量邮件发送:分批、限流与大规模投递

Rovela 批量接口、游标分页处理大列表、指数退避、发送时机和进度跟踪。

更新于 2026年10月12日
批量邮件发送:分批、限流与大规模投递

批量邮件发送:分批、限流与大规模投递

群发一万封邮件和发一封邮件,完全是两件不同的事。批量发送需要考虑速率限制、内存占用、失败重试和声誉影响。

Rovela 批量发送接口

Rovela 支持批量发送接口,一次请求发多封邮件:

await rovela.emails.sendBatch([
  { from: 'news@yourdomain.com', to: 'user1@example.com', subject: '周报', html: '...' },
  { from: 'news@yourdomain.com', to: 'user2@example.com', subject: '周报', html: '...' },
  // 最多 100 封/次
]);

处理大列表

10 万收件人不能一次性加载到内存里。用游标分页从数据库读:

async function* streamContacts(batchSize = 1000) {
  let cursor = 0;
  while (true) {
    const rows = await db.query(
      'SELECT email, name FROM contacts WHERE status = $1 AND id > $2 ORDER BY id LIMIT $3',
      ['active', cursor, batchSize]
    );
    if (rows.length === 0) break;
    yield rows;
    cursor = rows[rows.length - 1].id;
  }
}

for await (const batch of streamContacts()) {
  const messages = batch.map(contact => ({
    from: 'news@yourdomain.com',
    to: contact.email,
    subject: '本周更新',
    html: renderNewsletter({ name: contact.name }),
  }));
  await rovela.emails.sendBatch(messages);
  await sleep(100); // 控制发送速率
}

速率控制

Rovela 的 API 有速率限制,超过限制会返回 429。用指数退避重试:

async function sendWithRetry(messages, attempt = 0) {
  try {
    return await rovela.emails.sendBatch(messages);
  } catch (err) {
    if (err.status === 429 && attempt < 5) {
      await sleep(Math.pow(2, attempt) * 1000); // 1s, 2s, 4s, 8s, 16s
      return sendWithRetry(messages, attempt + 1);
    }
    throw err;
  }
}

发送时机

批量邮件发送时机影响到达率:

  • 工作日上午 9-11 点或下午 2-4 点打开率最高(B2B)
  • 周二、周三通常表现优于周一和周五
  • 避免深夜发送,特别是首次接触新 IP 时

进度跟踪

大批量发送要记录进度,方便暂停和恢复:

await db.query(
  'UPDATE campaign_sends SET sent_count = $1, last_contact_id = $2 WHERE campaign_id = $3',
  [sentCount, lastId, campaignId]
);