xpavp03 / sendgrid-nette

SendGrid API integration for Nette. Allows sending ANY request via SendGrid API, not just sending e-mails.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sendgrid-nette

Sendgrid integration for Nette.

Github badge Build Status Coverage Status

Install

composer require price2performance/sendgrid-nette

Configuration

In config add:

extension:
    sendgrid: Price2Performance\SendGrid\DI\SendGridExtension

sendgrid:
    key: 'SECRET_KEY'

Usage

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.

Versions

Version Nette SendGrid API PHP
master ^3.0 ^7.4 7.1 - 7.4
2.0 ^3.0 ^7.4 7.1 - 7.4

About

SendGrid API integration for Nette. Allows sending ANY request via SendGrid API, not just sending e-mails.

License:GNU General Public License v3.0


Languages

Language:PHP 100.0%