PHPackages                             godpod/sendgrid - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. godpod/sendgrid

ActiveLibrary[Mail &amp; Notifications](/categories/mail)

godpod/sendgrid
===============

This library allows you to quickly and easily send emails or make api calls through SendGrid using PHP.

2.2.0.1(11y ago)05.9k↓35.7%MITPHPPHP &gt;=5.3

Since Apr 3Pushed 10y ago2 watchersCompare

[ Source](https://github.com/godpod/sendgrid-php)[ Packagist](https://packagist.org/packages/godpod/sendgrid)[ Docs](http://github.com/godpod/sendgrid-php)[ RSS](/packages/godpod-sendgrid/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (1)Dependencies (3)Versions (29)Used By (0)

SendGrid-php
============

[](#sendgrid-php)

This library allows you to quickly and easily send emails through SendGrid using PHP.

WARNING: This module was recently upgraded from [1.1.7](https://github.com/sendgrid/sendgrid-php/tree/v1.1.7) to 2.X. There were API breaking changes for various method names. See [usage](https://github.com/sendgrid/sendgrid-php#usage) for up to date method names.

Important: This library requires PHP 5.3 or higher.

[![BuildStatus](https://camo.githubusercontent.com/b3d1fafb543446232b518c518c741ec4e9d4d25f470aa9e2184e3d85236af652/68747470733a2f2f7472617669732d63692e6f72672f73656e64677269642f73656e64677269642d7068702e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/sendgrid/sendgrid-php)[![Latest Stable Version](https://camo.githubusercontent.com/695b93a075eed8b0b0f00b14af899f578c32d7e076afd4eebf21a15d36c3dc6a/68747470733a2f2f706f7365722e707567782e6f72672f73656e64677269642f73656e64677269642f76657273696f6e2e706e67)](https://packagist.org/packages/sendgrid/sendgrid)

```
$sendgrid = new SendGrid('username', 'password');
$email    = new SendGrid\Email();
$email->addTo('foo@bar.com')->
       setFrom('me@bar.com')->
       setSubject('Subject goes here')->
       setText('Hello World!')->
       setHtml('Hello World!');

$sendgrid->send($email);
```

Installation
------------

[](#installation)

Add SendGrid to your `composer.json` file. If you are not using [Composer](http://getcomposer.org), you should be. It's an excellent way to manage dependencies in your PHP application.

```
{
  "require": {
    "godpod/sendgrid": "2.2.0.1"
  }
}
```

Then at the top of your PHP script require the autoloader:

```
require 'vendor/autoload.php';
```

#### Alternative: Install from zip

[](#alternative-install-from-zip)

If you are not using Composer, simply download and install the **[latest packaged release of the library as a zip](https://sendgrid-open-source.s3.amazonaws.com/sendgrid-php/sendgrid-php.zip)**.

[**⬇︎ Download Packaged Library ⬇︎**](https://sendgrid-open-source.s3.amazonaws.com/sendgrid-php/sendgrid-php.zip)

Then require the library from package:

```
require("path/to/sendgrid-php/sendgrid-php.php");
```

Previous versions of the library can be found in the [version index](https://sendgrid-open-source.s3.amazonaws.com/index.html).

Example App
-----------

[](#example-app)

There is a [sendgrid-php-example app](https://github.com/sendgrid/sendgrid-php-example) to help jumpstart your development.

Usage
-----

[](#usage)

To begin using this library, initialize the SendGrid object with your SendGrid credentials.

```
$sendgrid = new SendGrid('username', 'password');
```

Create a new SendGrid Email object and add your message details.

```
$email = new SendGrid\Email();
$email->addTo('foo@bar.com')->
       addTo('bar@foo.com')->
       setFrom('me@bar.com')->
       setSubject('Subject goes here')->
       setText('Hello World!')->
       setHtml('Hello World!');
```

Send it.

```
$sendgrid->send($email);
```

#### addTo

[](#addto)

You can add one or multiple TO addresses using `addTo`.

```
$email = new SendGrid\Email();
$email->addTo('foo@bar.com')->
       addTo('another@another.com');
$sendgrid->send($email);
```

#### setTos

[](#settos)

If you prefer, you can add multiple TO addresses as an array using the `setTos` method. This will unset any previous `addTo`s you appended.

```
$email   = new SendGrid\Email();
$emails = array("foo@bar.com", "another@another.com", "other@other.com");
$email->setTos($emails);
$sendgrid->send($email);
```

#### setFrom

[](#setfrom)

```
$email   = new SendGrid\Email();
$email->setFrom('foo@bar.com');
$sendgrid->send($email);
```

#### setFromName

[](#setfromname)

```
$email   = new SendGrid\Email();
$email->setFrom('foo@bar.com');
$email->setFromName('Foo Bar');
$email->setFrom('other@example.com');
$email->setFromName('Other Guy');
$sendgrid->send($email);
```

#### setReplyTo

[](#setreplyto)

```
$email = new SendGrid\Email();
$email->addTo('foo@bar.com')->
       setReplyTo('someone.else@example.com')->
       setFromName('John Doe')->
       ...
```

### Cc

[](#cc)

#### addCc

[](#addcc)

```
$email   = new SendGrid\Email();
$email->addCc('foo@bar.com');
$sendgrid->send($email);
```

#### setCc

[](#setcc)

```
$email   = new SendGrid\Email();
$email->setCc('foo@bar.com');
$sendgrid->send($email);
```

#### setCcs

[](#setccs)

```
$email   = new SendGrid\Email();
$emails = array("foo@bar.com", "another@another.com", "other@other.com");
$email->setCcs($emails);
$sendgrid->send($email);
```

#### removeCc

[](#removecc)

```
$email->removeCc('foo@bar.com');
```

### Bcc

[](#bcc)

Use multiple `addTo`s as a superior alternative to `setBcc`.

```
$email = new SendGrid\Email();
$email->addTo('foo@bar.com')->
       addTo('someotheraddress@bar.com')->
       addTo('another@another.com')->
       ...
```

But if you do still have a need for Bcc you can do the following:

#### addBcc

[](#addbcc)

```
$email   = new SendGrid\Email();
$email->addBcc('foo@bar.com');
$sendgrid->send($email);
```

#### setBcc

[](#setbcc)

```
$email   = new SendGrid\Email();
$email->setBcc('foo@bar.com');
$sendgrid->send($email);
```

#### setBccs

[](#setbccs)

```
$email   = new SendGrid\Email();
$emails = array("foo@bar.com", "another@another.com", "other@other.com");
$email->setBccs($emails);
$sendgrid->send($email);
```

#### removeBcc

[](#removebcc)

```
$email->removeBcc('foo@bar.com');
```

#### setSubject

[](#setsubject)

```
$email   = new SendGrid\Email();
$email->setSubject('This is a subject');
$sendgrid->send($email);
```

#### setText

[](#settext)

```
$email   = new SendGrid\Email();
$email->setText('This is some text');
$sendgrid->send($email);
```

#### setHtml

[](#sethtml)

```
$email   = new SendGrid\Email();
$email->setHtml('This is an html email');
$sendgrid->send($email);
```

#### setDate

[](#setdate)

```
$email   = new SendGrid\Email();
$email->setDate('Wed, 17 Dec 2014 19:21:16 +0000');
$sendgrid->send($email);
```

#### setSendAt

[](#setsendat)

```
$email   = new SendGrid\Email();
$email->setSendAt(1409348513);
$sendgrid->send($email);
```

#### setSendEachAt

[](#setsendeachat)

```
$email   = new SendGrid\Email();
$email->setSendEachAt(array(1409348513, 1409348514, 1409348515));
$sendgrid->send($email);
```

#### addSendEachAt

[](#addsendeachat)

```
$email   = new SendGrid\Email();
$email->addSendEachAt(1409348513);
$email->addSendEachAt(1409348514);
$email->addSendEachAt(1409348515);
$sendgrid->send($email);
```

### Categories

[](#categories)

Categories are used to group email statistics provided by SendGrid.

To use a category, simply set the category name. Note: there is a maximum of 10 categories per email.

#### addCategory

[](#addcategory)

```
$email = new SendGrid\Email();
$email->addTo('foo@bar.com')->
       ...
       addCategory("Category 1")->
       addCategory("Category 2");
```

#### setCategory

[](#setcategory)

```
$email = new SendGrid\Email();
$email->addTo('foo@bar.com')->
       ...
       setCategory("Category 1");
```

#### setCategories

[](#setcategories)

```
$email = new SendGrid\Email();
$categories = array("Category 1", "Category 2", "Category 3");
$email->setCategories($categories);
```

#### removeCategory

[](#removecategory)

```
$email = new SendGrid\Email();
$email->addTo('foo@bar.com')->
       ...
       removeCategory("Category 1");
```

### Attachments

[](#attachments)

Attachments are currently file based only, with future plans for an in memory implementation as well.

File attachments are limited to 7 MB per file.

#### addAttachment

[](#addattachment)

```
$email = new SendGrid\Email();
$email->addTo('foo@bar.com')->
       ...
       addAttachment("../path/to/file.txt");
```

#### setAttachment

[](#setattachment)

```
$email = new SendGrid\Email();
$email->addTo('foo@bar.com')->
       ...
       setAttachment("../path/to/file.txt");
```

#### setAttachments

[](#setattachments)

```
$email = new SendGrid\Email();
$attachments = array("../path/to/file1.txt", "../path/to/file2.txt");
$email->addTo('foo@bar.com')->
       ...
       setAttachments($attachments);
```

#### removeAttachment

[](#removeattachment)

```
$email = new SendGrid\Email();
$email->addTo('foo@bar.com')->
       ...
       addAttachment("../path/to/file.txt");
$email->removeAttachment("../path/to/file.txt");
```

You can tag files for use as inline HTML content. It will mark the file for inline disposition using the specified "cid".

```
$email = new SendGrid\Email();
$email->addTo('foo@bar.com')
      ->setHtml('Our logo:')
      ->addAttachment("../path/to/file.txt", "super_file.txt", "file-cid");
```

**Important Gotcha**: `setBcc` is not supported with attachments. This is by design. Instead use multiple `addTo`s. Each user will receive their own personalized email with that setup, and only see their own email.

Standard `setBcc` will hide who the email is addressed to. If you use the multiple addTo, each user will receive a personalized email showing \**only* their email. This is more friendly and more personal. Additionally, it is a good idea to use multiple `addTo`s because setBcc is not supported with attachments. This is by design.

So just remember, when thinking 'bcc', instead use multiple `addTo`s.

### Substitutions

[](#substitutions)

Substitutions can be used to customize multi-recipient emails, and tailor them for the user

#### addSubstitution

[](#addsubstitution)

```
$email = new SendGrid\Email();
$email->addTo('john@somewhere.com')->
       addTo('harry@somewhere.com')->
       addTo('Bob@somewhere.com')->
       ...
       setHtml('Hey %name%, we've seen that you've been gone for a while')->
       addSubstitution('%name%', array('John', 'Harry', 'Bob'));
```

Substitutions can also be used to customize multi-recipient subjects.

```
$email = new SendGrid\Email();
$email->addTos(array('john@somewhere.com', 'harry@somewhere.com', 'bob@somewhere.com'))
    ->setSubject('%subject%')
    ->addSubstitution('%subject%', array('Subject to John', 'Subject to Harry', 'Subject to Bob'))
    ...;
```

#### setSubstitutions

[](#setsubstitutions)

```
$email = new SendGrid\Email();
$email->addTos(array('john@somewhere.com', 'harry@somewhere.com', 'bob@somewhere.com'))
    ->setSubject('%subject%')
    ->setSubstitutions(array('%name%' => array('John', 'Harry', 'Bob') , '%subject%' => array('Subject to John', 'Subject to Harry', 'Subject to Bob')))
    ...;
```

### Sections

[](#sections)

Sections can be used to further customize messages for the end users. A section is only useful in conjunction with a substitution value.

#### addSection

[](#addsection)

```
$email = new SendGrid\Email();
$email->addTo('john@somewhere.com')->
       addTo("harry@somewhere.com")->
       addTo("Bob@somewhere.com")->
       ...
       setHtml("Hey %name%, you work at %place%")->
       addSubstitution("%name%", array("John", "Harry", "Bob"))->
       addSubstitution("%place%", array("%office%", "%office%", "%home%"))->
       addSection("%office%", "an office")->
       addSection("%home%", "your house");
```

#### setSections

[](#setsections)

```
$email = new SendGrid\Email();
$email->addTo('john@somewhere.com')->
       addTo("harry@somewhere.com")->
       addTo("Bob@somewhere.com")->
       ...
       setHtml("Hey %name%, you work at %place%")->
       addSubstitution("%name%", array("John", "Harry", "Bob"))->
       addSubstitution("%place%", array("%office%", "%office%", "%home%"))->
       setSections(array("%office%" => "an office", "%home%" => "your house"));
```

### Unique Arguments

[](#unique-arguments)

Unique Arguments are used for tracking purposes

#### addUniqueArg / addUniqueArgument

[](#adduniquearg--adduniqueargument)

```
$email = new SendGrid\Email();
$email->addTo('foo@bar.com')->
       ...
       addUniqueArg("Customer", "Someone")->
       addUniqueArg("location", "Somewhere")->
```

#### setUniqueArgs / setUniqueArguments

[](#setuniqueargs--setuniquearguments)

```
$email = new SendGrid\Email();
$email->addTo('foo@bar.com')->
       ...
       setUniqueArgs(array('cow' => 'chicken'));
```

### Filter Settings

[](#filter-settings)

Filter Settings are used to enable and disable apps, and to pass parameters to those apps.

#### addFilter / addFilterSetting

[](#addfilter--addfiltersetting)

```
$email = new SendGrid\Email();
$email->addTo('foo@bar.com')->
       ...
       addFilter("gravatar", "enable", 1)->
       addFilter("footer", "enable", 1)->
       addFilter("footer", "text/plain", "Here is a plain text footer")->
       addFilter("footer", "text/html", "Here is an HTML footer");
```

#### setFilters / setFilterSettings

[](#setfilters--setfiltersettings)

```
$email = new SendGrid\Email();
$email->addTo('foo@bar.com')->
       ...
       setFilters(array("gravatar" => array("settings" => array("enable" => 1))))
```

### Headers

[](#headers)

You can add standard email message headers as necessary.

#### addHeader

[](#addheader)

```
$email = new SendGrid\Email();
$email->addTo('foo@bar.com')->
       ...
       addHeader('X-Sent-Using', 'SendGrid-API')->
       addHeader('X-Transport', 'web');
```

#### setHeaders

[](#setheaders)

```
$email = new SendGrid\Email();
$email->addTo('foo@bar.com')->
       ...
       setHeaders(array('X-Sent-Using' => 'SendGrid-API', 'X-Transport' => 'web'));
```

#### removeHeader

[](#removeheader)

```
$email = new SendGrid\Email();
$email->addTo('foo@bar.com')->
       ...
       addHeader('X-Sent-Using', 'SendGrid-API')->
       addHeader('X-Transport', 'web');
$email->removeHeader('X-Transport');
```

### Options

[](#options)

Options may be passed to the library when initializing the SendGrid object:

```
$options = array(
  'turn_off_ssl_verification' => false,
  'protocol' => 'https',
  'host' => 'api.sendgrid.com',
  'endpoint' => '/api/mail.send.json',
  'port' => null,
  'url' => null
);
$sendgrid = new SendGrid('username', 'password', $options);
```

#### Changing URL

[](#changing-url)

You may change the URL sendgrid-php uses to send email by supplying various parameters to `options`, all parameters are optional:

```
$sendgrid = new SendGrid('username', 'password', array( 'protocol' => 'http', 'host' => 'sendgrid.org', 'endpoint' => '/send', 'port' => '80' ));
```

A full URL may also be provided:

```
$sendgrid = new SendGrid('username', 'password', array( 'url' => 'http://sendgrid.org:80/send'));
```

#### Ignoring SSL certificate verification

[](#ignoring-ssl-certificate-verification)

You can optionally ignore verification of SSL certificate when using the Web API.

```
$sendgrid   = new SendGrid(SENDGRID_USERNAME, SENDGRID_PASSWORD,  array("turn_off_ssl_verification" => true));
```

### Sending to 1,000s of emails in one batch

[](#sending-to-1000s-of-emails-in-one-batch)

Sometimes you might want to send 1,000s of emails in one request. You can do that. It is recommended you break each batch up in 1,000 increments. So if you need to send to 5,000 emails, then you'd break this into a loop of 1,000 emails at a time.

```
$sendgrid   = new SendGrid(SENDGRID_USERNAME, SENDGRID_PASSWORD);
$email       = new SendGrid\Email();

$recipients = array("alpha@mailinator.com", "beta@mailinator.com", "zeta@mailinator.com");
$names      = array("Alpha", "Beta", "Zeta");

$email->setFrom("from@mailinator.com")->
        setSubject('[sendgrid-php-batch-email]')->
        setTos($recipients)->
        addSubstitution("%name%", $names)->
        setText("Hey %name, we have an email for you")->
        setHtml("Hey %name%, we have an email for you");

$result = $sendgrid->send($email);
```

Contributing
------------

[](#contributing)

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

Running Tests
-------------

[](#running-tests)

The existing tests in the `test` directory can be run using [PHPUnit](https://github.com/sebastianbergmann/phpunit/) with the following command:

```
composer update --dev
cd test
../vendor/bin/phpunit
```

or if you already have PHPUnit installed globally.

```bash
cd test
phpunit
```

## Releasing

To release a new version of this library, update the version in all locations, tag the version, and then push the tag up. Packagist.org takes care of the rest.

#### Testing uploading to Amazon S3

If you want to test uploading the zipped file to Amazon S3 (SendGrid employees only), do the following.

```
export S3_SIGNATURE="secret_signature"
export S3_POLICY="secret_policy"
export S3_BUCKET="sendgrid-open-source"
export S3_ACCESS_KEY="secret_access_key"
./scripts/s3upload.sh
```

```

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 63.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~24 days

Recently: every ~54 days

Total

28

Last Release

4183d ago

Major Versions

1.1.7 → 2.0.12014-02-16

### Community

Maintainers

![](https://www.gravatar.com/avatar/3f3f28776867e658ad61a39008c1a04533e7ec6f0bcb1b5d8035f0face5d0f97?d=identicon)[godpod](/maintainers/godpod)

---

Top Contributors

[![motdotla](https://avatars.githubusercontent.com/u/3848?v=4)](https://github.com/motdotla "motdotla (123 commits)")[![theycallmeswift](https://avatars.githubusercontent.com/u/434373?v=4)](https://github.com/theycallmeswift "theycallmeswift (14 commits)")[![nquinlan](https://avatars.githubusercontent.com/u/829864?v=4)](https://github.com/nquinlan "nquinlan (13 commits)")[![godpod](https://avatars.githubusercontent.com/u/74084?v=4)](https://github.com/godpod "godpod (8 commits)")[![eddiezane](https://avatars.githubusercontent.com/u/959573?v=4)](https://github.com/eddiezane "eddiezane (6 commits)")[![cjbuchmann](https://avatars.githubusercontent.com/u/526429?v=4)](https://github.com/cjbuchmann "cjbuchmann (5 commits)")[![kander-zz](https://avatars.githubusercontent.com/u/521275?v=4)](https://github.com/kander-zz "kander-zz (4 commits)")[![heitortsergent](https://avatars.githubusercontent.com/u/446316?v=4)](https://github.com/heitortsergent "heitortsergent (2 commits)")[![rbin](https://avatars.githubusercontent.com/u/1341049?v=4)](https://github.com/rbin "rbin (2 commits)")[![ravivarma666](https://avatars.githubusercontent.com/u/9799720?v=4)](https://github.com/ravivarma666 "ravivarma666 (2 commits)")[![F21](https://avatars.githubusercontent.com/u/2263040?v=4)](https://github.com/F21 "F21 (2 commits)")[![MattKetmo](https://avatars.githubusercontent.com/u/334996?v=4)](https://github.com/MattKetmo "MattKetmo (1 commits)")[![sander-bol](https://avatars.githubusercontent.com/u/12197682?v=4)](https://github.com/sander-bol "sander-bol (1 commits)")[![Aqua-Ye](https://avatars.githubusercontent.com/u/85234?v=4)](https://github.com/Aqua-Ye "Aqua-Ye (1 commits)")[![travispaul](https://avatars.githubusercontent.com/u/2118993?v=4)](https://github.com/travispaul "travispaul (1 commits)")[![assertivist](https://avatars.githubusercontent.com/u/1486313?v=4)](https://github.com/assertivist "assertivist (1 commits)")[![bpneal](https://avatars.githubusercontent.com/u/438219?v=4)](https://github.com/bpneal "bpneal (1 commits)")[![brandonmwest](https://avatars.githubusercontent.com/u/433682?v=4)](https://github.com/brandonmwest "brandonmwest (1 commits)")[![dmyers](https://avatars.githubusercontent.com/u/207171?v=4)](https://github.com/dmyers "dmyers (1 commits)")[![hellogerard](https://avatars.githubusercontent.com/u/200488?v=4)](https://github.com/hellogerard "hellogerard (1 commits)")

---

Tags

emailsendgridsendgrid

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/godpod-sendgrid/health.svg)

```
[![Health](https://phpackages.com/badges/godpod-sendgrid/health.svg)](https://phpackages.com/packages/godpod-sendgrid)
```

###  Alternatives

[sendgrid/sendgrid

This library allows you to quickly and easily send emails through Twilio SendGrid using PHP.

1.6k50.9M190](/packages/sendgrid-sendgrid)[sendgrid/smtpapi

Build SendGrid X-SMTPAPI headers in PHP.

686.6M2](/packages/sendgrid-smtpapi)[fastglass/sendgrid

This library allows you to send emails through SendGrid using PHP and Guzzle 6.x.

213.9M](/packages/fastglass-sendgrid)[slm/mail

Integration of various email service providers in the Laminas\\Mail

109745.7k1](/packages/slm-mail)[omnimail/omnimail

PHP Library to send email across all platforms using one interface.

33235.4k](/packages/omnimail-omnimail)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
