Add email via Resend
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
/** src/lib/server/resend.ts
|
||||
* AUTHOR: Neil Clack
|
||||
* EMAIL: nclack@radiantwavetech.com
|
||||
* DATE: 07/06/2027
|
||||
*
|
||||
* A server side library and client for Resend
|
||||
* Includes the Resend client, as well as functions
|
||||
* for different email scenarios.
|
||||
*
|
||||
*/
|
||||
import { Resend } from 'resend';
|
||||
import { RESEND_API_KEY, EMAIL_FROM, RESEND_TEMPLATE_ID } from '$env/static/private';
|
||||
|
||||
export const resend = new Resend(RESEND_API_KEY);
|
||||
|
||||
/**
|
||||
* Send a transactional email using a Resend-hosted template.
|
||||
*
|
||||
* @example
|
||||
* await sendMail({
|
||||
* to: 'jane@example.com',
|
||||
* templateId: 'tmpl_abc123', // optional — defaults to RESEND_TEMPLATE_ID
|
||||
* subject: 'Your license key is ready', // optional — defaults to 'Template Email'
|
||||
* data: { // optional — template variables
|
||||
* firstName: 'Jane',
|
||||
* licenseKey: 'RWT-7X9K-2M4Q'
|
||||
* }
|
||||
* });
|
||||
*
|
||||
* @returns The Resend API response data on success.
|
||||
* @throws {Error} If the Resend API call fails.
|
||||
*/
|
||||
export async function sendMail(options: {
|
||||
to: string;
|
||||
subject?: string;
|
||||
templateId?: string;
|
||||
data?: Record<string, string | number>;
|
||||
}) {
|
||||
const { to, subject, templateId, data } = options;
|
||||
|
||||
const result = await resend.emails.send({
|
||||
from: EMAIL_FROM,
|
||||
to: [to],
|
||||
subject: subject ?? 'Template Email',
|
||||
template: {
|
||||
id: templateId ?? RESEND_TEMPLATE_ID,
|
||||
variables: data ?? {}
|
||||
}
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
throw new Error(result.error.message);
|
||||
}
|
||||
|
||||
return result.data;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import type { RequestHandler } from "./$types";
|
||||
|
||||
export const POST: RequestHandler = async () => {
|
||||
// Request validation (400)
|
||||
// 1. Validate and extract information
|
||||
|
||||
// Business rule validation (409)
|
||||
// 2. Check for existing entries
|
||||
|
||||
// Resource creation (500 on unexpected failures)
|
||||
// 3. Create new entries
|
||||
// 5. Create license key
|
||||
|
||||
// External operations (502/503/504 or 500, depending on your API)
|
||||
// 4. Send profile setup email
|
||||
// 6. Send license key email
|
||||
|
||||
// Success (200/201)
|
||||
// 7. Send response
|
||||
return new Response(JSON.stringify({ "msg": "success" }))
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/** src/routes/send/+server.ts
|
||||
* AUTHOR: Neil Clack
|
||||
* EMAIL: nclack@radiantwavetech.com
|
||||
* DATE: 07/06/2027
|
||||
*
|
||||
* An example endpoint, temporarily enabled, to describe
|
||||
* how to send an email with the Resend API and our custom
|
||||
* sendMail function.
|
||||
*
|
||||
*/
|
||||
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { sendMail } from '$lib/server/resend';
|
||||
import type { RequestHandler } from './$types';
|
||||
|
||||
export const POST: RequestHandler = async ({ request }) => {
|
||||
const { to, subject, templateId, data } = await request.json();
|
||||
|
||||
try {
|
||||
const result = await sendMail({ to, subject, templateId, data });
|
||||
return json(result);
|
||||
} catch (e) {
|
||||
return json({ error: (e as Error).message }, { status: 400 });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user