@aws-blocks/bb-email-client

中文文档
0.2.0发布于 2026/11/02Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 6.3万

Transactional email sending via Amazon SES.

原始使用文档
文档目录10 节
  1. Usage
  2. API
  3. new EmailClient(scope, id, options)
  4. emailClient.send(message): Promise<SendResult>
  5. emailClient.sendBatch(messages): Promise<SendBatchResult>
  6. Input validation
  7. Error Handling
  8. Local Development
  9. Package Export Conditions
  10. Limits

@aws-blocks/bb-email-client

Transactional email sending via Amazon SES.

Design & mock parity details: DESIGN.md

Usage

import { EmailClient } from '@aws-blocks/bb-email-client';

const emailClient = new EmailClient(scope, 'notifications', {
  fromAddress: '[email protected]',
  configurationSet: 'my-tracking-set', // optional
});

// Send a single email
const { messageId } = await emailClient.send({
  to: '[email protected]',
  subject: 'Welcome!',
  body: 'Thanks for signing up.',
  html: '<h1>Welcome!</h1><p>Thanks for signing up.</p>',
});
console.log('Sent:', messageId);

// Send a batch of emails (uses SES SendBulkEmail API with inline templates — no setup required)
const result = await emailClient.sendBatch([
  { to: '[email protected]', subject: 'Hi Alice', body: 'Hello!' },
  { to: '[email protected]', subject: 'Hi Bob', body: 'Hello!' },
]);
const sent = result.results.filter(r => r.status === 'success').length;
const failed = result.results.filter(r => r.status === 'failed').length;
console.log(`Sent: ${sent}, Failed: ${failed}`);

API

new EmailClient(scope, id, options)

OptionTypeRequiredDescription
fromAddressstring✅Verified sender email address
replyTostring[]❌Reply-to addresses
configurationSetstring❌SES configuration set name

emailClient.send(message): Promise<SendResult>

Send to one or more recipients. Returns { messageId: string }.

FieldTypeDescription
message.tostring | string[]Recipient address(es)
message.subjectstringSubject line
message.bodystringPlain text body
message.htmlstring?HTML body (optional)
message.ccstring[]?CC addresses (optional)
message.bccstring[]?BCC addresses (optional)

emailClient.sendBatch(messages): Promise<SendBatchResult>

Send multiple messages using the SES SendBulkEmail API with inline templates. No stored SES template is required — each message's subject, body, and HTML are passed directly via TemplateContent.

Returns { results: Array<{ status, messageId?, error? }> }.

Never throws on send failures. Both partial and total failures are reported per-entry in the results array (with status: 'failed').

A message without html is sent as plain text only, as with send(): its body is never used as an HTML part. Messages that fail validation (see below) are marked failed and not sent. Messages with html are sent in their own SendBulkEmail calls, before the text-only ones, so the order messages go out (and use up the SES sending rate) can differ from the input order; results stays in input order.

Each message in the array is an EmailMessage (same type as send()):

FieldTypeDescription
tostring | string[]Recipient address(es)
subjectstringSubject line
bodystringPlain text body
htmlstring?HTML body (optional)
ccstring[]?CC addresses (optional)
bccstring[]?BCC addresses (optional)

Input validation

send() and sendBatch() validate each message the same way locally and when deployed, before calling SES:

  • every address (To, CC, BCC, fromAddress and replyTo) must be a valid email address, either bare ([email protected]) or with a display name made of plain words or one quoted string (Jane Doe <[email protected]>, "Doe, Jane" <[email protected]>). The address is checked as given: surrounding whitespace and control characters are rejected;
  • at most 50 recipients per message (To + CC + BCC combined);
  • at most 40 MB per message.

A malformed address or too many recipients throws InvalidInputException; an oversized message throws EmailSendFailedException. In sendBatch() the message is marked failed instead.

Error Handling

import { EmailErrors } from '@aws-blocks/bb-email-client';
import { isBlocksError } from '@aws-blocks/core';

try {
  await emailClient.send({ to: '[email protected]', subject: 'Hi', body: 'Hello' });
} catch (e: unknown) {
  if (isBlocksError(e, EmailErrors.SendFailed)) {
    // General send failure — check error message for details
  }
  if (isBlocksError(e, EmailErrors.InvalidInput)) {
    // Malformed input (e.g. invalid email address)
  }
  if (isBlocksError(e, EmailErrors.DomainNotVerified)) {
    // Sending domain not verified in SES
  }
  if (isBlocksError(e, EmailErrors.AccountPaused)) {
    // Account sending is paused by AWS
  }
  if (isBlocksError(e, EmailErrors.RateLimited)) {
    // Transient — safe to retry after backoff
  }
}

Local Development

In local dev mode (npm run dev), emails are:

  • Logged to the console as a block (body and HTML truncated):
    [Email:{id}]
      Recipient: [email protected], [email protected]
      Subject:   ...
      Body:      ...
      HTML:      ...   (only when html is set)
    
  • Persisted to .bb-data/{fullId}/emails.json for inspection
  • Validated with the same checks as the deployed client, so an address or message rejected locally is rejected the same way when deployed

Package Export Conditions

This package uses a custom "cdk" export condition:

{
  "exports": {
    ".": {
      "cdk": "./dist/index.cdk.js",
      "aws-runtime": "./dist/index.aws.js",
      "default": "./dist/index.mock.js"
    }
  }
}

Note: The "cdk" condition is a Blocks framework convention, resolved by the Blocks build toolchain (tsx with --conditions=cdk). It is not a built-in Node.js condition and will not resolve in standard node or ts-node unless you pass --conditions=cdk explicitly.

Limits

  • Per-message recipients: 50 recipients max per message (To + CC + BCC combined)
  • Batch API destinations: 50 destinations per SendBulkEmail API call (handled automatically via chunking)
  • Message size: 40 MB per message
  • SES sandbox rate: 1 email/sec (request production access for higher throughput)

Note: Both the mock and the AWS runtime enforce the per-message recipient and size limits locally before calling SES (see Input validation): send() throws, and sendBatch() records the violation as a failed entry in results.

版本与发布记录

@aws-blocks/bb-email-client 共发布 14 个版本,首次发布于 2026/06/16;过去 12 个月发布了 14 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.0latest2026/11/025 天前
0.1.92026/10/1622 天前
0.0.0-canary.esql-canary.c93c81b.1791235863canary-esql-canary2026/10/061 个月前
0.1.82026/10/051 个月前
0.0.0-canary.esql-canary.f7f39ac.17909891272026/10/031 个月前
0.0.0-canary.ci-publish-canary-npm.f53dfdc.1790988617canary-ci-publish-canary-npm2026/10/031 个月前

关于 @aws-blocks/bb-email-client 的常见问题

@aws-blocks/bb-email-client 是什么?

Transactional email sending via Amazon SES.

如何安装 @aws-blocks/bb-email-client?

在项目目录运行 npm install @aws-blocks/bb-email-client,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:constructs(^10.6.0)、aws-cdk-lib(^2.257.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aws-blocks/bb-email-client 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aws-blocks/bb-email-client 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@aws-blocks/bb-email-client 可以商用吗?

@aws-blocks/bb-email-client 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@aws-blocks/bb-email-client 的下载量有多少?

根据 npm 官方下载统计,@aws-blocks/bb-email-client 在 2026/10/07 – 2026/11/05 这 30 天内被下载 63,083 次,日均约 2,103 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aws-blocks/bb-email-client 还在维护吗?

最新版本 0.2.0 发布于 2026/11/02(5 天前);至今共发布 14 个版本,其中过去 12 个月发布了 14 个;首次发布于 2026/06/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。