Invia email da PHP, in tre modi
Invia da PHP con l'SDK PHP di Brevo su PHP 8.1 o successivo, con PHPMailer e il relay SMTP di Brevo, oppure con cURL.

Come funziona
composer require getbrevo/brevo-php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Brevo\Brevo;
use Brevo\TransactionalEmails\Requests\SendTransacEmailRequest;
use Brevo\TransactionalEmails\Types\SendTransacEmailRequestSender;
use Brevo\TransactionalEmails\Types\SendTransacEmailRequestToItem;
$client = new Brevo(apiKey: 'your-api-key');
$result = $client->transactionalEmails->sendTransacEmail(
new SendTransacEmailRequest([
'subject' => 'Hello from Brevo!',
'htmlContent' => '<html><body><p>Hello,</p><p>This is my first transactional email.</p></body></html>',
'sender' => new SendTransacEmailRequestSender([
'name' => 'Alex from Brevo',
'email' => '[email protected]',
]),
'to' => [
new SendTransacEmailRequestToItem([
'email' => '[email protected]',
'name' => 'John Doe',
]),
],
])
);
echo 'Email sent. Message ID: ' . $result->messageId . PHP_EOL;
$client = new Brevo(apiKey: $_ENV['BREVO_API_KEY'] ?? getenv('BREVO_API_KEY'));
use Brevo\Exceptions\BrevoApiException;
use Brevo\Exceptions\BrevoException;
try {
$client->transactionalEmails->sendTransacEmail($request);
} catch (BrevoApiException $e) {
$statusCode = $e->getCode();
$body = $e->getBody();
if ($statusCode === 401) {
echo 'Invalid API key';
} elseif ($statusCode === 429) {
echo 'Rate limited. Check Retry-After header.';
} else {
echo "API error {$statusCode}: " . $e->getMessage();
}
} catch (BrevoException $e) {
echo 'SDK error: ' . $e->getMessage();
}
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'smtp-relay.brevo.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'YOUR_SMTP_KEY';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->setFrom('[email protected]', 'My App');
$mail->addAddress('[email protected]', 'John Doe');
$mail->addReplyTo('[email protected]', 'Support');
$mail->Subject = 'Your order is confirmed';
$mail->Body = 'Hello John, your order #12345 has been received.';
$mail->send();
echo 'Email sent successfully';
} catch (Exception $e) {
echo "Error: {$mail->ErrorInfo}";
}
<?php
$apiKey = $_ENV['BREVO_API_KEY'];
$payload = json_encode([
'sender' => ['email' => '[email protected]', 'name' => 'My App'],
'to' => [['email' => '[email protected]', 'name' => 'User']],
'subject' => 'Welcome to My App!',
'htmlContent' => '<html><body><h1>Welcome!</h1><p>Thanks for signing up.</p></body></html>',
]);
$ch = curl_init('https://api.brevo.com/v3/smtp/email');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => [
'api-key: ' . $apiKey,
'Content-Type: application/json',
],
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 201) {
$data = json_decode($response, true);
echo 'Sent! Message ID: ' . $data['messageId'];
} elseif ($httpCode === 429) {
echo 'Rate limit exceeded. Try again shortly.';
} else {
echo "Error {$httpCode}: {$response}";
}
Sostituisci chiave API, mittente e destinatario con i tuoi. Leggi la chiave da una variabile d'ambiente.
Chiedi al tuo agente IA
Prompt per il server Brevo MCP.
Prima di inviare email dalla mia app PHP, 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
Scegli la credenziale giusta
Autentica prima il tuo dominio di invio e il mittente. L'SDK e cURL usano una chiave API, creata in SMTP & API, poi API keys & MCP. PHPMailer usa una chiave SMTP dalla scheda SMTP, mai una chiave API.
Scegli un metodo e invia
PHP 8.1 o successivo: installa getbrevo/brevo-php con Composer, crea un client con la tua chiave API e chiama transactionalEmails->sendTransacEmail. WordPress, hosting condiviso o PHP 7.x: PHPMailer con il relay SMTP. Senza Composer: cURL su /v3/smtp/email.
Gestisci gli errori e controlla il log
L'SDK lancia BrevoApiException per qualsiasi risposta non 2xx. Conferma la consegna in Transactional, poi Email logs. Per gli invii in batch, usa messageVersions su POST /v3/smtp/email. Il relay SMTP non supporta l'invio in batch.


