Send Transactional Email with SendGrid in PHP

Posted on June 13, 2018 at 2:52 pm

SendGrid is the most popular transactional email service with a FREE plan:
Email Delivery Service | SendGrid

You can download the official SendGrid PHP library here:
https://github.com/sendgrid/sendgrid-php

Here is a simple function used to send an email:

function sendgrid_send($from_name, $from_email, $to_email, $subject, $content_html)
{
    require "path/to/sendgrid-php/sendgrid-php.php"; 
    $from = new SendGrid\Email($from_name, $from_email);
    $to = new SendGrid\Email("", $to_email);
    $content = new SendGrid\Content("text/html", $content_html);
    $mail = new SendGrid\Mail($from, $subject, $to, $content);
 
    // Put below your SendGrid API Key
    $sg = new \SendGrid("YOUR_SENDGRID_API_KEY_HERE");
 
    $response = $sg->client->mail()->send()->post($mail);
    return $response->statusCode();
}

Usage is very simple:

$ret = sendgrid_send( "From Name", "from@email.com", "to@email.com". "Subject", "HTML Message" );
echo ( $ret == "202" ) ? "SENT" : "FAILED";

Receive updates via email

Other Posts

Updated Posts