Invia email da Next.js
Aggiungi un Route Handler lato server che chiama l'API delle email transazionali di Brevo con la tua chiave presa da una variabile d'ambiente, così la chiave resta fuori dal codice client.

Come funziona
import { NextResponse } from "next/server";
export async function POST(request: Request) {
const { to, subject, html } = await request.json();
const res = await fetch("https://api.brevo.com/v3/smtp/email", {
method: "POST",
headers: {
"api-key": process.env.BREVO_API_KEY!,
"Content-Type": "application/json",
},
body: JSON.stringify({
sender: { name: "Your App", email: "[email protected]" },
to: [{ email: to }],
subject,
htmlContent: html,
}),
});
if (!res.ok) {
return NextResponse.json({ error: await res.text() }, { status: res.status });
}
return NextResponse.json({ ok: true });
}
await fetch("/api/send-email", {
method: "POST",
body: JSON.stringify({ to: "[email protected]", subject: "Welcome!", html: "<p>Hi there</p>" }),
});
npm install @getbrevo/brevo
import { BrevoClient } from '@getbrevo/brevo';
const brevo = new BrevoClient({ apiKey: 'your-api-key' });
const result = await brevo.transactionalEmails.sendTransacEmail({
subject: 'Hello from Brevo!',
htmlContent: '<html><body><p>Hello,</p><p>This is my first transactional email.</p></body></html>',
sender: { name: 'Alex from Brevo', email: '[email protected]' },
to: [{ email: '[email protected]', name: 'John Doe' }],
});
console.log('Email sent. Message ID:', result.messageId);
Sostituisci chiave API, mittente e destinatario con i tuoi. Tieni le chiavi nelle variabili d'ambiente.
Chiedi al tuo agente IA
Prompt per il server Brevo MCP.
Prima di inviare email dalla mia app Next.js, verifica che io abbia un mittente verificato e un dominio di invio autenticato. Elenca cosa manca.
Sola lettura: legge il tuo account Brevo tramite il server Brevo MCP e non modifica nulla.
Configurazione in tre passaggi
Crea una chiave e salvala come secret
Crea una chiave API in SMTP & API, poi API keys & MCP. Aggiungila come BREVO_API_KEY nelle variabili d'ambiente del tuo hosting, con scope su Production e su ogni altro ambiente che ne ha bisogno, poi rifai il deploy.
Aggiungi il Route Handler
Crea app/api/send-email/route.ts. Legge to, subject e html dal corpo della richiesta. Li invia a Brevo con l'header api-key. Fai l'await della chiamata fetch.
Chiamalo e controlla il log
Fai una POST a /api/send-email dalla tua app, poi conferma la consegna in Transactional, poi Email logs. Verifica prima il mittente: i mittenti non verificati vengono rifiutati o fortemente rallentati.


