SendGrid PHP Library

repository·main·Indexed 23 days ago

https://github.com/sendgrid/sendgrid-php

A 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.

Tokens
52.4K
Snippets
157
Records
217
Agent score
79%

What's inside sendgrid-php

  1. Use the Recipients Helper to build subscription forms

    main
    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.
  2. Manage Marketing Campaigns

    main

    The Marketing Campaigns API allows you to perform the following operations:

    • Retrieve all Campaigns: Get a list of all campaigns (newest first) using get() with optional limit and offset query 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, or html_content) using patch().
    • 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 the schedules() endpoint.
    • Update a Scheduled Campaign: Change the scheduled time and date for an existing campaign using patch() on the schedules() 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);
  3. Manage IP pools

    main

    IP 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/pools with a JSON body containing the name.

    Retrieve all IP pools

    Use GET /ips/pools to list all existing pools.

    Update an IP pool name

    Use PUT /ips/pools/{pool_name} with a JSON body containing the new name.

    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();
  4. Manage tracking settings

    main

    Tracking 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);
  5. Manage Alerts

    main

    Alerts 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.
  6. Manage IP Access Settings

    main

    IP 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.

  7. Manage Twilio SendGrid API Key via environment variables

    main

    It 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'); (where SENDGRID_API_KEY is the name of the environment variable).

    Discouraged approach (hardcoded): $apiKey = 'YOUR_ACTUAL_API_KEY_STRING';

  8. Manage Suppression Groups (ASM)

    main
    Suppression 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 the asm() client method.
  9. Migrate from 7.x.x to 8.x.x

    main

    When 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.
  10. Configure the SENDGRID_API_KEY environment variable

    main

    The library requires a SENDGRID_API_KEY to authenticate requests. You can set this up in your development environment using an .env file:

    1. Copy the sample env file: cp .env.sample .env
    2. Edit the .env file to include your SENDGRID_API_KEY.
    3. Source the file: source ./.env
    cp .env.sample .env
    source ./.env
  11. Continue using Twilio SendGrid API v2

    main

    If your application requires the older Twilio SendGrid API v2, you must use version ~4.0.4 of the sendgrid-php library, as it is the last version providing v2 support.

    Composer requirement:

    {
      "require": {
        "sendgrid/sendgrid": "~4.0.4"
      }
    }