Convert Figma logo to code with AI

sendgrid logosendgrid-php

The Official Twilio SendGrid PHP API Library

1,488
624
1,488
35

Top Related Projects

1,484

Helps sending emails

20,995

The classic email sending library for PHP

Comprehensive mailing tools for PHP

Mailgun's Official SDK for PHP

Quick Overview

SendGrid-PHP is the official PHP library for interacting with SendGrid's API. It allows developers to easily integrate email sending capabilities into their PHP applications using SendGrid's robust email delivery service. The library provides a simple and intuitive interface for sending emails, managing contacts, and accessing various SendGrid features.

Pros

  • Easy to use and well-documented API
  • Supports both v2 and v3 of SendGrid's API
  • Comprehensive feature set, including email sending, template management, and statistics
  • Active development and community support

Cons

  • Requires a SendGrid account and API key
  • Some advanced features may require higher-tier SendGrid plans
  • Learning curve for complex email automation scenarios
  • Dependency on external service for email delivery

Code Examples

Sending a simple email:

$email = new \SendGrid\Mail\Mail();
$email->setFrom("test@example.com", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("test@example.com", "Example User");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
    "text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
    $response = $sendgrid->send($email);
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '. $e->getMessage() ."\n";
}

Using a template:

$email = new \SendGrid\Mail\Mail();
$email->setFrom("test@example.com", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("test@example.com", "Example User");
$email->setTemplateId("d-YOUR_TEMPLATE_ID");
$email->addDynamicTemplateData("name", "Example User");
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
    $response = $sendgrid->send($email);
    print $response->statusCode() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '. $e->getMessage() ."\n";
}

Adding attachments:

$email = new \SendGrid\Mail\Mail();
$email->setFrom("test@example.com", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("test@example.com", "Example User");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$file_encoded = base64_encode(file_get_contents('my_file.pdf'));
$email->addAttachment(
    $file_encoded,
    "application/pdf",
    "my_file.pdf",
    "attachment"
);
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
    $response = $sendgrid->send($email);
    print $response->statusCode() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '. $e->getMessage() ."\n";
}

Getting Started

  1. Install the library using Composer:

    composer require sendgrid/sendgrid
    
  2. Set up your SendGrid API key as an environment variable:

    export SENDGRID_API_KEY='YOUR_API_KEY'
    
  3. Create a new PHP file and include the following code to send a test email:

    require 'vendor/autoload.php';
    $email = new \SendGrid\Mail\Mail();
    $email->setFrom("test@example.com", "Example User");
    $email->setSubject("SendGrid Test");
    $email->addTo("recipient@example
    

Competitor Comparisons

1,484

Helps sending emails

Pros of symfony/mailer

  • More versatile, supporting multiple email providers and protocols
  • Integrated seamlessly with the Symfony framework ecosystem
  • Offers a more robust and feature-rich API for complex email tasks

Cons of symfony/mailer

  • Steeper learning curve, especially for developers not familiar with Symfony
  • May be overkill for simple email sending tasks
  • Requires additional configuration for specific providers like SendGrid

Code Comparison

sendgrid-php:

$email = new \SendGrid\Mail\Mail();
$email->setFrom("test@example.com", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("test@example.com", "Example User");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");

symfony/mailer:

$email = (new Email())
    ->from('test@example.com')
    ->to('you@example.com')
    ->subject('Time for Symfony Mailer!')
    ->text('Sending emails is fun again!')
    ->html('<p>See Twig integration for better HTML</p>');

Both libraries offer straightforward ways to create and send emails, but symfony/mailer provides a more object-oriented approach with method chaining. sendgrid-php is more focused on SendGrid-specific features, while symfony/mailer is designed to work with various email providers and offers more flexibility in terms of email composition and delivery options.

20,995

The classic email sending library for PHP

Pros of PHPMailer

  • Open-source and free to use, with no external service dependencies
  • Supports a wide range of email protocols (SMTP, mail(), sendmail, qmail)
  • Extensive documentation and large community support

Cons of PHPMailer

  • Requires more setup and configuration compared to SendGrid
  • May have lower deliverability rates without proper server configuration
  • Limited built-in analytics and tracking features

Code Comparison

PHPMailer:

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'user@example.com';
$mail->Password = 'secret';
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('recipient@example.com');
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->send();

SendGrid:

$email = new \SendGrid\Mail\Mail();
$email->setFrom("from@example.com", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("to@example.com", "Example User");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent("text/html", "<strong>and easy to do anywhere, even with PHP</strong>");
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
$response = $sendgrid->send($email);

Both libraries offer straightforward ways to send emails, but PHPMailer requires more configuration for SMTP settings, while SendGrid simplifies the process by using an API key.

Comprehensive mailing tools for PHP

Pros of SwiftMailer

  • More flexible and feature-rich for complex email scenarios
  • Supports a wider range of email protocols (SMTP, sendmail, mail())
  • Easier to integrate with various PHP frameworks and CMSs

Cons of SwiftMailer

  • Requires more setup and configuration compared to SendGrid-PHP
  • May have higher resource usage for large-scale email sending
  • Lacks built-in advanced features like email analytics and tracking

Code Comparison

SwiftMailer:

$transport = new Swift_SmtpTransport('smtp.example.org', 25);
$mailer = new Swift_Mailer($transport);
$message = (new Swift_Message('Subject'))
  ->setFrom(['john@doe.com' => 'John Doe'])
  ->setTo(['receiver@domain.org'])
  ->setBody('Here is the message itself');
$result = $mailer->send($message);

SendGrid-PHP:

$email = new \SendGrid\Mail\Mail();
$email->setFrom("test@example.com", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("test@example.com", "Example User");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
$response = $sendgrid->send($email);

Both libraries offer straightforward ways to send emails, but SwiftMailer provides more low-level control over the email creation process, while SendGrid-PHP simplifies the process with a more streamlined API.

Mailgun's Official SDK for PHP

Pros of mailgun-php

  • More comprehensive documentation with detailed examples
  • Supports a wider range of Mailgun API features
  • Better error handling and exception management

Cons of mailgun-php

  • Slightly more complex setup process
  • Less frequent updates and releases
  • Smaller community and fewer third-party integrations

Code Comparison

mailgun-php:

$mg = Mailgun::create('key-example');
$mg->messages()->send('example.com', [
  'from'    => 'bob@example.com',
  'to'      => 'alice@example.com',
  'subject' => 'Hello',
  'text'    => 'Testing some Mailgun awesomeness!'
]);

sendgrid-php:

$email = new \SendGrid\Mail\Mail();
$email->setFrom("test@example.com", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("test@example.com", "Example User");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
$response = $sendgrid->send($email);

Both libraries offer straightforward methods for sending emails, but mailgun-php provides a more concise syntax. sendgrid-php requires separate method calls for each email component, while mailgun-php allows setting multiple parameters in a single array. However, sendgrid-php's approach might be more intuitive for some developers, especially those new to email APIs.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

SendGrid Logo

BuildStatus Packagist Downloads MIT licensed Twitter Follow GitHub contributors Open Source Helpers

NEW:

  • Send SMS messages with Twilio.

This library allows you to quickly and easily use the Twilio SendGrid Web API v3 via PHP.

Version 7.X.X of this library provides full support for all Twilio SendGrid Web API v3 endpoints, including the new v3 /mail/send.

If you need support using SendGrid, please check the Twilio SendGrid Support Help Center.

Please browse the rest of this README for further details.

We appreciate your continued support, thank you!

Table of Contents

Installation

Prerequisites

  • PHP version 7.3, 7.4, 8.0, or 8.1
  • The Twilio SendGrid service, starting at the free level to send up to 40,000 emails for the first 30 days, then send 100 emails/day free forever or check out our pricing.
  • For SMS messages, you will need a free Twilio account.

Setup Environment Variables

Update the development environment with your SENDGRID_API_KEY, for example:

  1. Copy the sample env file to a new file named .env
cp .env.sample .env
  1. Edit the .env file to include your SENDGRID_API_KEY
  2. Source the .env file
source ./.env

Install Package

Add Twilio SendGrid to your composer.json file. If you are not using Composer, we highly recommend it. It's an excellent way to manage dependencies in your PHP application.

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

Alternative: Install package from zip

If you are not using Composer, simply download and install the latest packaged release of the library as a zip.

⬇︎ Download Packaged Library ⬇︎

Previous versions of the library can be downloaded directly from GitHub.

Dependencies

Quick Start

Include the proper lines from below at the top of each example based on your installation method:

<?php
// Uncomment the next line if you're using a dependency loader (such as Composer) (recommended)
// require 'vendor/autoload.php';

// Uncomment the next line if you're not using a dependency loader (such as Composer), replacing <PATH TO> with the path to the sendgrid-php.php file
// require_once '<PATH TO>/sendgrid-php.php';

Hello Email

The following is the minimum needed code to send an email. You may find more examples in our USE_CASES file:

$email = new \SendGrid\Mail\Mail();
$email->setFrom("test@example.com", "Example User");
$email->setSubject("Sending with Twilio SendGrid is Fun");
$email->addTo("test@example.com", "Example User");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
    "text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
    $response = $sendgrid->send($email);
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '. $e->getMessage() ."\n";
}

The SendGrid\Mail constructor creates a personalization object for you. Here is an example of how to add to it.

General v3 Web API Usage (With Fluent Interface)

$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);

try {
    $response = $sg->client->suppression()->bounces()->get();
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '.  $e->getMessage(). "\n";
}

General v3 Web API Usage (Without Fluent Interface)

$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);

try {
    $response = $sg->client->_("suppression/bounces")->get();
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '.  $e->getMessage(). "\n";
}

Use Cases

Examples of common API use cases, such as how to send an email with a transactional template.

Usage

Announcements

v7 has been released! Please see the release notes for details.

All updates to this library are documented in our CHANGELOG and releases.

How to Contribute

We encourage contribution to our libraries (you might even score some nifty swag), please see our CONTRIBUTING guide for details.

Quick links:

Troubleshooting

Please see our troubleshooting guide for common library issues.

About

sendgrid-php is maintained and funded by Twilio SendGrid, Inc. The names and logos for sendgrid-php are trademarks of Twilio SendGrid, Inc.

Support

For product support, please check the Twilio SendGrid Support Help Center.

License

The MIT License (MIT)