2025-08-23 17:24:37 +09:00
|
|
|
import { Injectable, Inject } from "@nestjs/common";
|
|
|
|
|
import { ConfigService } from "@nestjs/config";
|
|
|
|
|
import { Logger } from "nestjs-pino";
|
2025-08-27 10:54:05 +09:00
|
|
|
import sgMail, { MailDataRequired } from "@sendgrid/mail";
|
|
|
|
|
|
|
|
|
|
export interface ProviderSendOptions {
|
|
|
|
|
to: string | string[];
|
2025-09-10 16:33:24 +09:00
|
|
|
from?: string;
|
2025-08-27 10:54:05 +09:00
|
|
|
subject: string;
|
|
|
|
|
text?: string;
|
|
|
|
|
html?: string;
|
|
|
|
|
templateId?: string;
|
|
|
|
|
dynamicTemplateData?: Record<string, unknown>;
|
|
|
|
|
}
|
2025-08-23 17:24:37 +09:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class SendGridEmailProvider {
|
|
|
|
|
constructor(
|
|
|
|
|
private readonly config: ConfigService,
|
|
|
|
|
@Inject(Logger) private readonly logger: Logger
|
|
|
|
|
) {
|
|
|
|
|
const apiKey = this.config.get<string>("SENDGRID_API_KEY");
|
|
|
|
|
if (apiKey) {
|
|
|
|
|
sgMail.setApiKey(apiKey);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-27 10:54:05 +09:00
|
|
|
async send(options: ProviderSendOptions): Promise<void> {
|
2025-09-10 16:33:24 +09:00
|
|
|
const from = options.from || this.config.get<string>("EMAIL_FROM");
|
2025-08-27 10:54:05 +09:00
|
|
|
if (!from) {
|
|
|
|
|
this.logger.warn("EMAIL_FROM is not configured; email not sent");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-08-23 17:24:37 +09:00
|
|
|
|
2025-08-27 10:54:05 +09:00
|
|
|
const message: MailDataRequired = {
|
|
|
|
|
to: options.to,
|
|
|
|
|
from,
|
2025-08-23 17:24:37 +09:00
|
|
|
subject: options.subject,
|
|
|
|
|
text: options.text,
|
|
|
|
|
html: options.html,
|
|
|
|
|
templateId: options.templateId,
|
|
|
|
|
dynamicTemplateData: options.dynamicTemplateData,
|
2025-08-27 10:54:05 +09:00
|
|
|
} as MailDataRequired;
|
2025-08-23 17:24:37 +09:00
|
|
|
|
|
|
|
|
try {
|
2025-08-27 10:54:05 +09:00
|
|
|
await sgMail.send(message);
|
2025-08-23 17:24:37 +09:00
|
|
|
} catch (error) {
|
2025-08-27 10:54:05 +09:00
|
|
|
this.logger.error("Failed to send email via SendGrid", {
|
|
|
|
|
error: error instanceof Error ? error.message : String(error),
|
2025-08-23 17:24:37 +09:00
|
|
|
});
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|