DEV Community

Avi Goldman for SparkPost

Posted on • Originally published at on

PHP Frameworks In The Wild: A Closer Look at the PHP Ecosystem

SparkPost and PHP Frameworks: Scratching the Surface

PHP is one of the older languages used on the Web, created in the mid-nineties. Some environments are still stuck running PHP 3 while others are running the latest version. There’s been a lot of development around amazing PHP frameworks and tools in the last few years, and most of them come with easy ways to send mail. We recently broke down how we simplified our PHP library, but going with a native solution can still be easier sometimes. Here’s a quick breakdown of how to use SparkPost to send mail with some of the more popular PHP frameworks in the wild.

Plain ol’ PHP

Sometimes you just need to fire off a quick API call. All you need is a little cURL magic to do what you need. Check out this snippet for shooting off SparkPost requests.


 * Use this method to access the SparkPost API
function sparkpost($method, $uri, $payload = [], $headers = [])
    $defaultHeaders = ['Content-Type: application/json'];

    $curl = curl_init();
    $method = strtoupper($method);

    $finalHeaders = array_merge($defaultHeaders, $headers);

    $url = ''.$uri;

    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    if ($method !== 'GET') {
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($payload));

    curl_setopt($curl, CURLOPT_HTTPHEADER, $finalHeaders);

    $result = curl_exec($curl);


    return $result;

echo "Sending email...\n";
$email_results = sparkpost('POST', 'transmissions', [
    'content' => [
        'from' => [
            'name' => 'SparkPost Team',
            'email' => '',
        'subject' => 'First Mailing From PHP+cURL',
        'text' => 'Congratulations!! You just sent an email with PHP+cURL!',
    'recipients' => [
        ['address' => '',],
], ['Authorization: YOUR_API_KEY']);

🐘 + 🎶 = ❤️

Here at SparkPost we created a composer package to simplify using our API in all modern PHP environments. It’s quick to install and provides some “sugar to smooth out some of the more confusing parts of the API. To keep our library as flexible as possible, we use PHP HTTP to let you bring your own request library. To use it we can require guzzlehttp/guzzle, the php-http/guzzle6-adapter, and the sparkpost/sparkpost library. Then we are good to go with the code below.

require 'vendor/autoload.php';

use SparkPost\SparkPost;
use GuzzleHttp\Client;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;

$httpClient = new GuzzleAdapter(new Client());
$sparky = new SparkPost($httpClient, ['key'=>'YOUR_API_KEY', 'async' => false]);

$promise = $sparky->transmissions->post([
    'content' => [
        'from' => 'name' => 'SparkPost Team <>',
        'subject' => 'First Mailing From PHP Library',
        'text' => 'Congratulations!! You just sent an email with the PHP library!',
    'recipients' => [[ 'address' => 'John Doe <>'], ],

World of WordPress

Our very own Raju wrote a SparkPost plugin to use SparkPost as your email provider. It exposes key features like using templates, sending via SMTP or the API, and allows you to hook in at a ton of points to modify the email and settings on the fly. Plus, it’s easy to get started 😎


PHPMailer is probably the most popular way to send mail in PHP! It’s got a great API that makes it easy to plug any SMTP provider. Check out this simple example to get started with SparkPost quickly.


// This example uses the PHPMailer library:

require 'vendor/autoload.php';

$mail = new PHPMailer;

// configure for sparkpost
$mail->Host = '';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = 'SMTP_Injection';
$mail->Password = 'YOUR_API_KEY';

$mail->Subject = 'First Mailing From PHPMailer';
$mail->Body = 'Congratulations!! You just sent an email from PHPMailer!';

if (!$mail->send()) {
  echo "Message could not be sent\n";
  echo "Mailer Error: " . $mail->ErrorInfo . "\n";
} else {
  echo "Message has been sent\n";


Laravel is a whole beast unto itself so I’ll just hit the main points. You can follow the official Laravel docs to get set up with the SparkPost mail driver. Once you’ve done that you can generate and send your mailables. Matt Stauffer did a solid write up on using SparkPost with Laravel which is worth checking out.


CodeIgniter provides a simple API to send mail. You can add the SMTP configuration in code or through config/email.php. Check out the example below to fire off a quick email.


// inside a controller

$config = [
  'protocol' => 'smtp',
  'smtp_host' => '',
  'smtp_user' => 'SMTP_Injection',
  'smtp_pass' => 'YOUR_API_KEY',
  'smtp_crypto' => 'tls',
  'smtp_port' => '587',
  'newline' => "\r\n",


$this->email->from('', 'Your Name');
$this->email->subject('Testing SparkPost SMTP from CodeIgniter');
$this->email->message('Congratulations!! You just sent an email from CodeIgniter!');


Be sure to comment below or tweet us if you want us to go into more depth on any particular tool or if we missed your favorite framework 👋

This post was originally published on

Top comments (0)