xedi/sendgrid

SendGrid PHP Library

2.0.3 2022-03-24 13:18 UTC

README

Dependabot Status Packagist

Table of Contents

Installation

Prerequisites

  • PHP 7.1+
  • A SendGrid account and API Key

Install Package

Add XEDI SendGrid to your composer.json file either manually or using composer require.

{
    "require": {
        "xedi/sendgrid": "^1"
    }
}
composer require xedi/sendgrid

We recommend the later option, let composer do it's thing

Quick Start

<?php
    require 'vendor/autoload.php';

    use Xedi\SendGrid\Contracts\Exception as SendGridException;
    use Xedi\SendGrid\SendGrid;

    SendGrid::setClient(
        SendGrid::getApiClient($api_key)
    );

    ($mailable = SendGrid::prepareMail())
        ->setSender('you@email.com', 'Joe Blogs')
        ->setSubject('Checkout XEDI\'s SendGrid library!')
        ->addTextContent('Hey,\n\rI found this great SendGrid library!')
        ->addHtmlContent('<body>Hey,<br/>I found this great SendGrid library!</body>')
        ->addRecipient('john.smith@email.com', 'John Smith');

    try {
        SendGrid::send($mailable);
    } catch (SendGridException $exception) {
        echo 'Caught exception: ' . (string) $exception;
    }

About

This library uses SendGrids v3 HTTP API. It builts on some of the limitations of the "official" library such as:

  • Interacts with cURL directly limiting your ability to intercept requests during tests.
  • Exceptions aren't specific to the returned error.

We employ GuzzleHttp to act as our cURL wrapper off-loading responsibility of processing HTTP requests. Guzzle 6 provides a great utility for intercepting HTTP requests made through it's client; for more details click here.

SendGrid only returns one error code if there is a problem with your payload! This is because SendGrid's API is not RESTful. Our library will attempt to provide contextual exceptions such as Xedi\SendGrid\Exceptions\Domain\SubjectException, which would be thrown if SendGrid reports an issue with your subject field.

All exceptions can be identified using our ExceptionContract which all exceptions implement.

Feature Requests

This library was developed for an in-house project, and it's features will grow as our requirements change. However, if there is something we don't support please raise a GitHub issue.

Security Vulnerabilities

We take security very seriously. If you spot something, please let us know by emailing use at development@xedi.com.