Skip to main content

Frameworks

Astro

Send email from an Astro API route with the top-level mail().


Postboi is framework-agnostic — read the request’s FormData in an API route and hand it straight to mail(). Postboi extracts the special fields and renders the rest into a tidy HTML table.

// src/pages/api/contact.ts
import type { APIRoute } from 'astro'
import { mail } from 'postboi'

export const POST: APIRoute = async ({ request, redirect }) => {
	await mail({ body: request.formData() })
	return redirect('/?sent=1', 303)
}
// src/pages/api/contact.ts
import type { APIRoute } from 'astro'
import { mail } from 'postboi'

export const POST: APIRoute = async ({ request, redirect }) => {
	await mail({ body: request.formData() })
	return redirect('/?sent=1', 303)
}

Point a multipart/form-data form at the route. Include hidden _subject and _reply_to fields, and mirror the email into _reply_to with a one-line oninput so replying reaches the sender:

<form method="post" action="/api/contact" enctype="multipart/form-data">
	<input type="hidden" name="_subject" value="Contact Form" />
	<input type="hidden" name="_reply_to" />
	<input name="contact→name" placeholder="Name" required />
	<input name="contact→email" type="email" required
		oninput="this.form._reply_to.value = this.value" />
	<textarea name="details→message" placeholder="Message"></textarea>
	<button type="submit">Send</button>
</form>
<form method="post" action="/api/contact" enctype="multipart/form-data">
	<input type="hidden" name="_subject" value="Contact Form" />
	<input type="hidden" name="_reply_to" />
	<input name="contact→name" placeholder="Name" required />
	<input name="contact→email" type="email" required
		oninput="this.form._reply_to.value = this.value" />
	<textarea name="details→message" placeholder="Message"></textarea>
	<button type="submit">Send</button>
</form>

Field names use the fieldset→field syntax. API routes need SSR, so set output: 'server' with an adapter in astro.config.mjs. The provider and default recipient come from postboi.config.ts — a POSTBOI_TOKEN routes to Postboi Cloud, or pick any provider.

Runnable example: examples/astro-provider-postboi.