フレームワーク連携
Nuxt
Sendery の JavaScript SDK を使い、Nuxt のサーバーからトランザクションメールを送信します。
インストール
npm install @sendery/[email protected]動作環境
Node.js 22.12 以上のサーバー環境で JavaScript SDK を使います。プロジェクトの API キーを作成してください。
非公開の設定を追加
senderyApiKey を runtimeConfig に追加します。runtimeConfig.public には含めないでください。
nuxt.config.ts
// nuxt.config.ts
export default defineNuxtConfig({
runtimeConfig: { senderyApiKey: '' },
});環境変数を設定
サーバーの環境変数に NUXT_SENDERY_API_KEY を設定します。Nuxt が runtimeConfig.senderyApiKey に反映します。
環境変数
NUXT_SENDERY_API_KEY=your_project_api_keyサーバーから送信
サーバー側で sendEmail(event, input, emailKey) を呼び出します。input に宛先、公開済みテンプレートのキー、変数を渡してください。再試行では同じ emailKey と入力を使います。
TypeScript
// server/utils/sendEmail.ts
import { Sendery, type SendEmailInput } from '@sendery/sdk';
import type { H3Event } from 'h3';
export async function sendEmail(event: H3Event, input: SendEmailInput, emailKey: string) {
const config = useRuntimeConfig(event);
const sendery = new Sendery(config.senderyApiKey);
return sendery.prepare(input, emailKey).retry().send();
}特定のバージョンを送信
公開履歴のバージョンを指定すると、テンプレートの更新後もその版を送信できます。指定しない場合は最新バージョンを使います。
TypeScript
// server/utils/sendEmail.ts
import { Sendery, type SendEmailInput } from '@sendery/sdk';
import type { H3Event } from 'h3';
export async function sendEmail(event: H3Event, input: SendEmailInput, emailKey: string) {
const config = useRuntimeConfig(event);
const sendery = new Sendery(config.senderyApiKey);
return sendery.prepare(input, emailKey).version(3).send();
}添付ファイル
サーバールートで attachment() を使ってファイルを添付します。base64への変換はヘルパーが行います。
最大10ファイル、合計5 MBまで送信できます。対応形式と上限は 添付ファイルのリファレンス を参照してください。
使用例
import { Sendery, attachment } from '@sendery/sdk';
import { readFile } from 'node:fs/promises';
const sendery = new Sendery(process.env.SENDERY_API_KEY);
const file = await readFile('/path/document.pdf');
await sendery.prepare({
to: '[email protected]',
template: 'your-template',
data: { name: 'Alex', action_url: 'https://example.com/start' },
attachments: [attachment('document.pdf', file, 'application/pdf')],
}, 'your-idempotency-key').retry().send();