import { emails } from "gadget-server";
import type { Server } from "gadget-server";
/**
* Boot plugin to configure SendGrid as the email transport provider
* This plugin runs on app startup and configures the Gadget email system
* to use SendGrid for sending emails.
*/
export default async function plugin(server: Server) {
try {
// Get SendGrid API key from environment variables
const sendgridApiKey = process.env.SENDGRID_API_KEY;
if (!sendgridApiKey) {
throw new Error("SENDGRID_API_KEY environment variable is not set");
}
// Configure SendGrid transport
const transport = {
host: "smtp.sendgrid.net",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: "apikey", // SendGrid requires this value to be "apikey"
pass: sendgridApiKey,
},
};
// Set the transport for Gadget emails
emails.setTransport(transport);
server.log.info("SendGrid email transport configured successfully");
} catch (error) {
// Log the error but don't crash the app
server.log.error(error, "Failed to configure SendGrid email transport");
// Depending on requirements, you might want to rethrow the error to prevent app startup
// if email functionality is critical
// throw error;
}
}