45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
interface WelcomeEmailInput {
|
|
to: string;
|
|
}
|
|
|
|
interface WelcomeEmailResult {
|
|
sent: boolean;
|
|
reason?: string;
|
|
}
|
|
|
|
/**
|
|
* Sends a welcome email through Scribase Mail (api.scribase.com's
|
|
* Resend-shaped `/emails` endpoint, hosted at mail-api.scribase.com).
|
|
* No SCRIBASE_MAIL_API_KEY set -> skips instead of throwing, so the rest
|
|
* of the app (auth + db) keeps working without Mail configured.
|
|
*/
|
|
export async function sendWelcomeEmail({ to }: WelcomeEmailInput): Promise<WelcomeEmailResult> {
|
|
const apiKey = process.env.SCRIBASE_MAIL_API_KEY;
|
|
const from = process.env.SCRIBASE_MAIL_FROM ?? "onboarding@test.scribase.dev";
|
|
|
|
if (!apiKey) {
|
|
return { sent: false, reason: "SCRIBASE_MAIL_API_KEY not set (see .env.example)" };
|
|
}
|
|
|
|
const response = await fetch("https://mail-api.scribase.com/emails", {
|
|
method: "POST",
|
|
headers: {
|
|
"content-type": "application/json",
|
|
authorization: `Bearer ${apiKey}`,
|
|
},
|
|
body: JSON.stringify({
|
|
from,
|
|
to,
|
|
subject: "Welcome — your first Scribase-backed note is live",
|
|
html: "<p>Your account and first note were created by this starter. Reply to this email if anything looks off.</p>",
|
|
}),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const body = await response.text();
|
|
return { sent: false, reason: `Mail API ${response.status}: ${body.slice(0, 200)}` };
|
|
}
|
|
|
|
return { sent: true };
|
|
}
|