Generate and send emails with AI (LLM)
Use an LLM (OpenAI, Anthropic, etc.) to draft the content and send it with Publiq — great for summaries, follow-ups and personalized replies.
Generate the HTML with the LLM and pass it inline in the send. Always validate/sanitize the model output before sending:
import OpenAI from 'openai';
import { Publiq } from 'publiq';
const openai = new OpenAI();
const publiq = new Publiq(process.env.PUBLIQ_API_KEY);
const res = await openai.chat.completions.create({
model: 'gpt-4o-mini',
messages: [{ role: 'user', content: 'Write a friendly welcome email in HTML for Acme.' }],
});
const html = res.choices[0].message.content ?? '';
await publiq.emails.send({
from: 'you@yourdomain.com',
to: 'user@example.com',
subject: 'Welcome to Acme',
html,
});Prefer not to build HTML by hand? Publiq has an AI template builder in the dashboard — generate the template visually and send it by templateKey.
Best practices
- Always call Publiq from the server — never expose the API key on the client.
- Create the client instance once and reuse it across requests.
- Prefer
templateKeyover inline HTML to keep content versioned. - Handle
PubliqError(status/code); the SDK already retries transient errors with backoff. - Send from a verified domain — see Domains.