EmailService
説明
このサービスは、AWS SES (Simple Email Service)を使用してメールを送信するように設計されています。
使い方
EmailServiceは@mbc-cqrs-serverless/coreからエクスポートされ、フレームワークによってグローバルプロバイダーとして自動登録されます。標準のNestJSコンストラクタインジェクションを使って任意のサービスに注入できます:
import { EmailService, EmailNotification } from '@mbc-cqrs-serverless/core';
import { Injectable } from '@nestjs/common';
@Injectable()
export class NotificationService {
constructor(private readonly emailService: EmailService) {}
async sendWelcomeEmail(email: string, name: string): Promise<void> {
await this.emailService.sendEmail({
toAddrs: [email],
subject: `Welcome, ${name}!`,
body: `<p>Thank you for joining.</p>`,
});
}
}
SES_REGIONと任意でSES_FROM_EMAIL環境変数を設定してSESを構成してください。詳細は環境変数を参照。
メソッド
async sendEmail(msg: EmailNotification): Promise<any>
メールを作成してすぐに送信キューに追加します。Promise<SendEmailCommandOutput>を返します — AWS SESレスポンスには追跡に使用できるMessageId文字列が含まれます:
const result = await this.emailService.sendEmail({
toAddrs: ["recipient@example.com"],
subject: "Hello",
body: "<p>World</p>",
});
console.log(result.MessageId); // Log MessageId for email tracking (メール追跡用にMessageIdをログ出力)
基本的な例
const email = "cat@example.com";
const subject = "Welcome to MBC CQRS Serverless framework!";
const body = "<p>Enjoy</p>";
await this.emailService.sendEmail({
toAddrs: [email],
subject,
body,
});
CCとBCCを使用
await this.emailService.sendEmail({
toAddrs: ["recipient@example.com"],
ccAddrs: ["cc@example.com"],
bccAddrs: ["bcc@example.com"],
subject: "Meeting Invitation",
body: "<p>Please join our meeting.</p>",
});
添付ファイル付き
添付ファイルオブジェクトの配列を指定することで、メールにファイルを添付できます:
await this.emailService.sendEmail({
toAddrs: ["recipient@example.com"],
subject: "Report Attached",
body: "<p>Please find the attached report.</p>",
attachments: [
{
filename: "report.pdf",
content: pdfBuffer,
contentType: "application/pdf",
},
],
});
複数の添付ファイル
await this.emailService.sendEmail({
toAddrs: ["recipient@example.com"],
subject: "Documents",
body: "<p>Please find the attached documents.</p>",
attachments: [
{
filename: "document.pdf",
content: pdfBuffer,
contentType: "application/pdf",
},
{
filename: "image.jpg",
content: imageBuffer,
contentType: "image/jpeg",
},
{
filename: "data.csv",
content: csvBuffer,
contentType: "text/csv",
},
],
});
EmailNotificationインターフェース
| プロパティ | 型 | 必須 | 説明 |
|---|---|---|---|
fromAddr | string | いいえ | 送信者メールアドレス(指定しない場合はデフォルトを使用) |
toAddrs | string[] | はい | 受信者メールアドレスのリスト |
ccAddrs | string[] | いいえ | CC受信者 |
bccAddrs | string[] | いいえ | BCC受信者 |
subject | string | はい | メール件名 |
body | string | はい | HTMLとしてのメール本文 |
replyToAddrs | string[] | いいえ | 返信先アドレス |
attachments | Attachment[] | いいえ | 添付ファイル |
emailTags | EmailTag[] | いいえ | 分類とフィルタリングのためのAWS SESタグ |
Attachmentインターフェース
| プロパティ | 型 | 必須 | 説明 |
|---|---|---|---|
filename | string | はい | 受信者に表示されるファイル名 |
content | Buffer | はい | Bufferとしてのファイル内容 |
contentType | string | いいえ | MIMEタイプ(例:'application/pdf') |
async sendInlineTemplateEmail(msg: TemplatedEmailNotification): Promise<any>
バージョン情報
sendInlineTemplateEmail()はバージョン1.0.23で追加されました。
件名と本文に {{variableName}} プレースホルダーを使ったテンプレートメールを送信します。SES登録テンプレートとは異な り、テンプレートをリクエストにインラインで指定するため、事前登録は不要です。
import { EmailService, TemplatedEmailNotification } from '@mbc-cqrs-serverless/core';
await this.emailService.sendInlineTemplateEmail({
toAddrs: ['user@example.com'],
template: {
subject: '{{orderType}} Confirmation — Order {{orderId}}',
html: '<h1>Hello {{name}}!</h1><p>Your order #{{orderId}} is confirmed.</p>',
text: 'Hello {{name}}, your order #{{orderId}} is confirmed.',
},
data: {
name: 'Jane Doe',
orderId: '12345',
orderType: 'Purchase',
},
});
完全なインターフェース定義と高度なテンプレート機能(空白のトリミング、ネストデータ)については、通知モジュール — インラインテンプレートメールを参照してください。
テスト
実際のSES呼び出しを避けるため、ユニットテストでは EmailService をモックします:
// テストモジュールのセットアップ
const mockEmailService = {
sendEmail: jest.fn().mockResolvedValue({ MessageId: 'test-message-id' }),
};
const module = await Test.createTestingModule({
providers: [
YourService,
{ provide: EmailService, useValue: mockEmailService },
],
}).compile();
// 期待するパラメーターでメールが送信されたことを確認
expect(mockEmailService.sendEmail).toHaveBeenCalledWith(
expect.objectContaining({
toAddrs: ['user@example.com'],
subject: expect.stringContaining('Welcome'),
}),
);