SparkPost Coding: Send Email Easier

Email has been around for a pretty long time, 45 years to be exact. But even as old as it is, email is still a critical part of our lives. So it’s inevitable that whether you are writing a server script or a website, you need to be able to send out an email. Most modern programming frameworks have the ability to send email, but they usually rely on the local computer being able to relay email. Sometimes that is fine, but other times it becomes a bit of a pain to depend on the local system being configured correctly. That’s where SparkPost comes in. SparkPost is an email delivery service. They provide an API and your code can generate emails that SparkPost will send. Now that’s a 30 second summary of what they do, but there’s a lot more you can do with SparkPost and emails. Of course, one thing that makes SparkPost attractive to developers is that you can send 100,000 emails a month for free. So if you’re just learning about their service, or just writing a small application that needs to send email, SparkPost is a free service for you.

Getting Started with SparkPost

sparkpostTo get started using Sparkpost is pretty simple. First, you should have the ability to manage your domain’s email and DNS. Now head over to sparkpost.com. Click Try for Free, provide your email address and a password.

 

sparkpost-1Next, you’ll be prompted to confirm control of the sending domain. The easiest way is to select the postmaster@domain.com option. I went into my Email System and created the postmaster@domain.com address, forwarding it to my email. SparkPost will then send a confirmation email to that address. Click the link and you’re done.

 

 

sparkpost apiThen you’ll be asked if you want to create emails via SMTP or REST API. I think most people will want to go with API. Choose Get a Key and then an API Key will be generated for you. Make sure you save the API Key because you won’t be able to access it later. You’ll need this key in your applications.

sparkpostAt this point your ready to start sending email. Now SparkPost has a number other features which are pretty powerful and you may want to check. For this post, I’m just to focus on basic email sending. But some of the other available features include Email List management,¬†including people who have opted-out. Email templating is also available, as well as real-time tracking of email delivery and message, opens.

sparkpost message trackingOne more thing I want to highlight is the SparkPost message tracking features. From the Dashboard, this is available under Reports. SparkPost shows you in near-realtime, the tracking of emails as they’re created, sent, received, and even opened. This makes troubleshooting email problems really easy because you can see where emails are in the delivery process.

 

SparkPost with WordPress

sparkpost wordpressIf you want to configure your WordPress site to use SparkPost for sending email, they have their own WordPress Plugin available. I have to say using SparkPost in WordPress is really simple, and it has saved me a lot of headaches. I’ve used the SparkPost plugin with some of my clients whose hoster started blocking their email. Switching over to SparkPost completely bypasses the SMTP nightmare with some hosters (GoDaddy, Bluehost, I’m looking at you). Once you have the plugin installed and configured, all email will automatically route through SparkPost

 

sparkpost wordpressThe setup is pretty easy. Install the Plugin from WordPress repository, activate it, and then go to the Plugin Settings (located as a submenu under Settings).

To get the Plugin working you just need to check the “Send email using SparkPost” checkbox. Then paste in the API Key you created earlier. Click Save at the bottom and you’re done. There’s a few optional setting you may want to use. For example, you can override the WordPress default email address and name. You can also utilize SparkPost Templates in WordPress.

 

SparkPost with Python

I’m a big fan of using SparkPost in Python scripts. Instead of having to setup and configure Sendmail on each server I can just use SparkPost.

A fully function Python client is available on Github including full documentation. Setup is pretty easy with PIP

sudo pip install sparkpost

Sending email is equally simple in Python. I usually create a Function like the one below. To simplify storing my API Keys I have one config file on my server with various API Keys. I then load them as needed.

from sparkpost import SparkPost

Config = ConfigParser.ConfigParser()

Config.read("/opt/agnet/conf/agnet.conf")

def get_config_value(section):
 dict1 = {}
 options = Config.options(section)
 for option in options:
 try:
 dict1[option] = Config.get(section, option)
 if dict1[option] == -1:
 DebugPrint("skip: %s" % option)
 except:
 print("exception on %s!" % option)
 dict1[option] = None
 return dict1

def Send_Message(sendto, message, subject):
  sp = SparkPost(get_config_value("cloud")['sparkpost_key'])

  response = sp.transmissions.send(
      recipients=[sendto],
      html=message,
      from_email='WireandFrost <postmaster@wireandfrost.com>',
      subject=subject
  )

  print(response)
  return

 

SparkPost with PHP

If you’re writing PHP code and want to tap into SparkPost there’s a PHP client published on Github. Just like the Python client, its easy to setup and get working. The Readme on Github provides all the details to get started, but the basic PHP code to send email looks like this:

<?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']);

$promise = $sparky->transmissions->post([
    'content' => [
        'from' => [
            'name' => 'WireandFrost',
            'email' => 'portsmater@wireandfrost.com',
        ],
        'subject' => 'First Mailing From PHP',
        'html' => '<html><body><h1>Congratulations, {{name}}!</h1><p>You just sent your very first mailing!</p></body></html>',
        'text' => 'Congratulations, {{name}}!! You just sent your very first mailing!',
    ],
    'substitution_data' => ['name' => 'YOUR_FIRST_NAME'],
    'recipients' => [
        [
            'address' => [
                'name' => 'YOUR_NAME',
                'email' => 'YOUR_EMAIL',
            ],
        ],
    ],
]);
?>

So if you’re looking for a framework to making managing email just a little bit easier, I think SparkPost is really the tool for the job. The promise fast (and cheap/free) and¬†efficient email delivery, with real-time tracking. From what I’ve seen, they really deliver on these promises. And with many official and non-official clients available, it’s very easy to get started.

  • sparkpost api
  • sparkpost
  • sparkpost message tracking
I’m an avid Technologist, while I do work on architecting solutions that span from the desktop to the data center, my passion is Web Infrastructure.
We will be happy to hear your thoughts

      Leave a reply