PHP Frameworks In The Wild: A Closer Look at the PHP Ecosystem
Tags: php, phpframeworks, sdk
Published: 2018-02-10 22:32:34

---
title: PHP Frameworks In The Wild: A Closer Look at the PHP Ecosystem
published: true
tags: PHP,php frameworks,sdk
canonical_url: https://www.sparkpost.com/blog/php-frameworks/
---

### 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](https://www.sparkpost.com/blog/php-client-library-2-0/) 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](https://github.com/SparkPost/php-sparkpost/issues/164#issuecomment-289888237) for shooting off SparkPost requests.

```php
<?php 

/**
 * 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 = 'https://api.sparkpost.com:443/api/v1/'.$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);

    curl_close($curl);

    return $result;
}

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

### ðŸ˜ + ðŸŽ¶ = â¤ï¸

Here at SparkPost we created a [composer package](https://packagist.org/packages/sparkpost/sparkpost) 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](http://docs.php-http.org/en/latest/) 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.

```php
<?php
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 <phplibrary@yourdomain.com>',
        'subject' => 'First Mailing From PHP Library',
        'text' => 'Congratulations!! You just sent an email with the PHP library!',
    ],
    'recipients' => [[ 'address' => 'John Doe <john.doe@example.com>'], ],
]);
```

### World of WordPress

Our very own [Raju](https://thehungrycoder.com/) wrote a [SparkPost plugin](https://github.com/SparkPost/wordpress-sparkpost/) 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](https://github.com/SparkPost/wordpress-sparkpost/blob/master/docs/hooks.md) in at a ton of points to modify the email and settings on the fly. Plus, it’s easy to [get started](https://www.sparkpost.com/blog/sparkpost-wordpress-plugin/) ðŸ˜Ž

### PHPMailer

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](https://github.com/SparkPost/code-snippets/blob/master/snippets/SMTP/smtp_example.php) to get started with SparkPost quickly.

```php
<?php

// This example uses the PHPMailer library:
// https://github.com/PHPMailer/PHPMailer

require 'vendor/autoload.php';

$mail = new PHPMailer;

// configure for sparkpost
$mail->isSMTP();
$mail->Host = 'smtp.sparkpostmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = 'SMTP_Injection';
$mail->Password = 'YOUR_API_KEY';

$mail->setFrom('phpmailer@yourdomain.com');
$mail->addAddress('john.doe@example.com');
$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

Laravel is a whole beast unto itself so I’ll just hit the main points. You can follow the official [Laravel docs](https://laravel.com/docs/5.4/mail#driver-prerequisites) to get set up with the SparkPost mail driver. Once you’ve done that you can [generate](https://laravel.com/docs/5.4/mail#generating-mailables) and [send](https://laravel.com/docs/5.4/mail#sending-mail) your mailables. [Matt Stauffer](https://mattstauffer.co/blog/using-sparkpost-for-transactional-emails-with-laravel) did a solid write up on using SparkPost with Laravel which is worth checking out.

### CodeIgniter

CodeIgniter provides a simple API to send mail. You can add the SMTP configuration in code or through [config/email.php](https://www.codeigniter.com/user_guide/libraries/email.html#setting-email-preferences-in-a-config-file). Check out the example below to fire off a quick email.

```php
<?php

// inside a controller

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

$this->load->library('email');
$this->email->initialize($config);

$this->email->from('codeigniter@yourdomain.com', 'Your Name');
$this->email->to('john.doe@example.com');
$this->email->subject('Testing SparkPost SMTP from CodeIgniter');
$this->email->message('Congratulations!! You just sent an email from CodeIgniter!');

$this->email->send();
```

Be sure to comment below or [tweet us](http://twitter.com/sparkpost) 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 [sparkpost.com](https://www.sparkpost.com/blog/php-frameworks/)_