Erreur 401 en Sendgrid

Erreur 401 en Sendgrid

Si vous avez une réponse de type 401 lors de l’envoi d’un mail avec PHP en Sendgrid

Dans votre code il faut remplacer

$apiKey = getenv(...);

Par

$apiKey = 'add here your api key';

Exemple:

<?php
require 'vendor/autoload.php';
$email = new \SendGrid\Mail\Mail();
$email->setFrom("EMAIL", "NOM_PRENOM");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("EMAIL", "NOM_PRENOM");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
   "text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid('KEY');
/* wKExm12rT_6dLsD0sRq_lA */
try {
   $response = $sendgrid->send($email);
   print $response->statusCode() . "\n";
   print_r($response->headers());
   print $response->body() . "\n";
} catch (Exception $e) {
   echo 'Caught exception: '. $e->getMessage() ."\n";
}