SendGrid PHP Library
repository·main·Indexed 23 days ago
https://github.com/sendgrid/sendgrid-phpA PHP library providing a wrapper for the Twilio SendGrid Web API v3. It enables developers to send emails and manage SendGrid resources using a fluent interface or direct endpoint access. The library includes helpers for building mail objects, managing attachments, configuring Google Analytics tracking, and using the Recipients Helper for subscription forms. Requires PHP 7.3 or higher.
What's inside sendgrid-php
- The Recipients Helper is a utility designed to simplify the process of building subscription forms that add contacts to your SendGrid contact database. It abstracts the complexity of constructing the necessary data structures for the Contacts API.
Manage Marketing Campaigns
mainThe Marketing Campaigns API allows you to perform the following operations:
- Retrieve all Campaigns: Get a list of all campaigns (newest first) using
get()with optionallimitandoffsetquery parameters. - Retrieve a single campaign: Get details for a specific campaign using its
campaign_id. - Update a Campaign: Modify existing campaign parameters (like
subject,title, orhtml_content) usingpatch(). - Delete a Campaign: Remove a specific campaign using
delete(). - Schedule a Campaign: Set a specific date and time for a campaign to be sent using
post()on theschedules()endpoint. - Update a Scheduled Campaign: Change the scheduled time and date for an existing campaign using
patch()on theschedules()endpoint.
// Example: Retrieve all campaigns $query_params = json_decode('{"limit": 1, "offset": 1}'); $response = $sg->client->campaigns()->get(null, $query_params); // Example: Update a campaign $campaign_id = "test_url_param"; $request_body = json_decode('{ "subject": "New Products for Summer!" }'); $response = $sg->client->campaigns()->_($campaign_id)->patch($request_body); // Example: Schedule a campaign $campaign_id = "test_url_param"; $request_body = json_decode('{ "send_at": 1489771528 }'); $response = $sg->client->campaigns()->_($campaign_id)->schedules()->post($request_body);- Retrieve all Campaigns: Get a list of all campaigns (newest first) using
Manage IP pools
mainIP Pools allow you to group dedicated Twilio SendGrid IP addresses together (e.g., separating transactional and marketing traffic) to maintain separate reputations. Each user can create up to 10 different IP pools. Note that IP pools can only be used with authenticated IP addresses.
Create an IP pool
Use
POST /ips/poolswith a JSON body containing thename.Retrieve all IP pools
Use
GET /ips/poolsto list all existing pools.Update an IP pool name
Use
PUT /ips/pools/{pool_name}with a JSON body containing the newname.Delete an IP pool
Use
DELETE /ips/pools/{pool_name}.// Create an IP pool $request_body = json_decode('{ "name": "marketing" }'); $response = $sg->client->ips()->pools()->post($request_body); // Retrieve all IP pools $response = $sg->client->ips()->pools()->get(); // Update an IP pool name $request_body = json_decode('{ "name": "new_pool_name" }'); $pool_name = "test_url_param"; $response = $sg->client->ips()->pools()->_($pool_name)->put($request_body); // Delete an IP pool $pool_name = "test_url_param"; $response = $sg->client->ips()->pools()->_($pool_name)->delete();Manage tracking settings
mainTracking settings allow you to monitor recipient interactions such as email opens, link clicks, and subscriptions. You can retrieve all available tracking settings, manage click tracking, and configure Google Analytics integration.
// Retrieve all tracking settings $query_params = json_decode('{"limit": 1, "offset": 1}'); $response = $sg->client->tracking_settings()->get(null, $query_params);Manage Alerts
mainAlerts allow you to receive notifications regarding email usage or statistics.
- Usage alerts: Set thresholds for specific usage levels.
- Stats notifications: Set frequency (e.g.,
daily,weekly,monthly) for receiving email statistics reports.
Manage IP Access Settings
mainIP Access Management allows you to control which IP addresses can access your account via the UI or API. You can whitelist specific IPs, CIDR ranges, or wildcards.
Warning: Removing your own IP address from the whitelist may prevent you from accessing your account.
Manage Twilio SendGrid API Key via environment variables
mainIt is highly recommended to use environment variables to store your Twilio SendGrid API key rather than hardcoding it.
Recommended approach:
$apiKey = getenv('SENDGRID_API_KEY');(whereSENDGRID_API_KEYis the name of the environment variable).Discouraged approach (hardcoded):
$apiKey = 'YOUR_ACTUAL_API_KEY_STRING';Manage Suppression Groups (ASM)
mainSuppression groups (or unsubscribe groups) allow you to categorize email content (e.g., 'Newsletters' vs 'Invoices') so recipients can opt out of specific categories without unsubscribing from everything. You can create up to 25 groups. You can manage these groups and the email addresses (suppressions) associated with them using theasm()client method.Migrate from 7.x.x to 8.x.x
mainWhen upgrading from version 7.x.x to 8.x.x, note that support for older, End-of-Life (EOL) PHP versions has been dropped. Ensure your environment is running a supported version of PHP.
Breaking Change:
- Support for PHP versions 5.6, 7.0, 7.1, and 7.2 is no longer available in version 8.x.x.
Configure the SENDGRID_API_KEY environment variable
mainThe library requires a
SENDGRID_API_KEYto authenticate requests. You can set this up in your development environment using an.envfile:- Copy the sample env file:
cp .env.sample .env - Edit the
.envfile to include yourSENDGRID_API_KEY. - Source the file:
source ./.env
cp .env.sample .env source ./.env- Copy the sample env file:
Install the SendGrid PHP library from a ZIP file
mainIf you are not using Composer, you can download the latest packaged release of the library as a zip file and include it manually in your project.
Continue using Twilio SendGrid API v2
mainIf your application requires the older Twilio SendGrid API v2, you must use version
~4.0.4of thesendgrid-phplibrary, as it is the last version providing v2 support.Composer requirement:
{ "require": { "sendgrid/sendgrid": "~4.0.4" } }