PHP
This library allows you to quickly and easily send emails through SendGrid using PHP.
How To Install The Libraries
1
| |
SendGrid APIs
SendGrid provides two methods of sending email: the Web API, and SMTP API. SendGrid recommends using the SMTP API for sending emails. For an explanation of the benefits of each, refer to the integration page.
This library implements a common interface to make it very easy to use either API.
Mail Pre-Usage
Before we begin using the library, its important to understand a few things about the library architecture. First, the SendGrid Mail object is the means of setting mail data. In general, data can be set in following three ways for most elements:
- set - reset the data, and initialize it to the given element. This will destroy previous data.
- set (List) - for array based elements, we provide a way of passing the entire array in at once. This will also destroy previous data.
- add - append data to the list of elements.
Sending an email is as simple as:
- Creating a SendGrid Instance
- Creating a SendGrid Mail object, and setting its data
- Sending the mail using either SMTP API or Web API.
Mail Usage
To begin using this library, you must first include it in your code, like so:
1 2 3 | |
Then, initialize the SendGrid object with your SendGrid credentials:
1 2 3 | |
Create a new SendGrid Mail object and add your message details, as follows:
1 2 3 4 5 6 7 8 9 | |
Finally, send it using the API of your choice of SMTP like so:
1 2 3 4 5 | |
Or you can send it using the Web API like so:
1 2 3 4 5 | |
Using Categories
Categories are used to group email statistics provided by SendGrid. To use a category, simply set the category name.
Info There is a maximum of 10 categories per email.
1 2 3 4 5 6 7 8 | |
Using Attachments
Attachments are currently file based only, with future plans for an in memory implementation as well.
Info File attachments are limited to 7 MB per file.
1 2 3 4 5 6 7 | |
Using Substitutions
Substitutions can be used to customize multi-recipient emails, and tailor them for the user.
1 2 3 4 5 6 7 8 9 10 | |
Using Sections
Sections can be used to further customize messages for the end users. A section is only useful in conjunction with a substition value.
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Using Unique Arguments
Unique Arguments can be used for tracking purposes and developing data for unique statistical analysis.
1 2 3 4 5 6 7 8 | |
Using Filter Settings
Filter Settings are used to enable and disable apps, and to pass parameters to those apps.
1 2 3 4 5 6 7 8 9 10 | |
Using Headers
Headers can be used to add existing sendgrid functionality (such as for categories or filters), or custom headers can be added as necessary.
1 2 3 4 5 6 7 | |