Sendgrid integration for Nette.
composer require price2performance/sendgrid-nette
In config add:
extension:
sendgrid: Price2Performance\SendGrid\DI\SendGridExtension
sendgrid:
key: 'SECRET_KEY'
To make any API call to SendGrid, just inject the SendGrid class to your presenter:
/** @var SendGrid @inject */
public $sendgrid;
public function actionDefault()
{
// CALL suppression/bounces
try {
$response = $this->sendgrid->client->suppression()->bounces()->get();
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage(). "\n";
}
}
To send an email via SendGrid
, just inject Price2Performance\SendGrid\SendGridMailer
to your presenter:
/** @var \Price2Performance\SendGrid\SendGridMailer @inject */
public $mailer;
protected function sendMail() {
$message = new \Nette\Mail\Message();
$message->addFrom('sender@example.com', 'Sender Name');
$message->addTo('example@example.com');
$message->setSubject('TEST SUBJECT');
$message->setBody('TEST BODY');
$this->mailer->send($message);
/** @var \SendGrid\Response $response */
$response = $this->mailer->getLastResponse(); // optional, for error logging
}
Calling getLastResponse()
on SendGridMailer
gets you SendGrid\Response
of the last send()
call. You can use it to log errors.
Version | Nette | SendGrid API | PHP |
---|---|---|---|
master | ^3.0 | ^7.4 | 7.1 - 7.4 |
2.0 | ^3.0 | ^7.4 | 7.1 - 7.4 |