Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ResendService } from "./services/resend";
import { SendGridService } from "./services/sendgrid";
import { MailgunService } from "./services/mailgun";
import { ZeptoMailService } from "./services/zeptomail";
import { BrevoService } from "./services/brevo";
import type { EmailProvider } from "./types/email-options";
import type { EmailService } from "./types/email-service";

Expand Down Expand Up @@ -33,6 +34,9 @@ export function useEmail(provider: EmailProvider): EmailService {
case "zeptomail": {
return new ZeptoMailService();
}
case "brevo": {
return BrevoService();
}
default: {
throw new Error(`Unsupported email provider: ${provider}`);
}
Expand Down
49 changes: 49 additions & 0 deletions src/services/brevo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { ofetch as $fetch } from "ofetch";
import type { EmailOptions } from "../types/email-options";
import type { EmailService } from "../types/email-service";

/**
* Email service implementation for Mailgun
*/
export const BrevoService = (): EmailService => {
const BREVO_API_KEY = process.env.BREVO_API_KEY;
const BREVO_API_URL = "https://api.brevo.com/v3/smtp/email";

const send = async (emailOptions: EmailOptions): Promise<void> => {
if (!BREVO_API_KEY) {
throw new Error("Brevo API key is missing");
}

const { to, from, subject, text, html } = emailOptions;
if (!to || !from || (!text && !html)) {
throw new Error("Required email fields are missing");
}

const payload = {
sender: { email: from },
to: Array.isArray(to)
? to.map((email) => ({ email }))
: [{ email: to }],
subject: subject,
textContent: text,
htmlContent: html,
};

try {
await $fetch(BREVO_API_URL, {
method: "POST",
headers: {
"api-key": BREVO_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
console.log("Email sent via Brevo");
} catch (error) {
console.error("Failed to send email with Brevo:", error);
throw new Error("Email sending failed with Brevo");
}
};

return { send };
};
3 changes: 2 additions & 1 deletion src/types/email-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export type EmailProvider =
| "sendgrid"
| "postmark"
| "mailgun"
| "zeptomail";
| "zeptomail"
| "brevo";