Ahmad Awais

SHARE


Open Sourcing My Sendy PHP API Wrapper

Ahmad AwaisAhmad Awais

I recently started using Sendy.co (aff) to send emails. I was having trouble finding a good API wrapper for it. Spent more than five hours searching for one and ended up building one myself. I hope it helps.

🚀 GITHUB: ★ 25 ⑂ 10 →

🚀 Sendy PHP API Wrapper: Complete API interfacing.

[![emoji-log](https://cdn.rawgit.com/ahmadawais/stuff/ca97874/emoji-log/flat-round.svg)](https://github.com/ahmadawais/Emoji-Log/)

📨 SENDY API

With this Sendy PHP API Wrapper you can do the following:

SUBSCRIBERS LISTS CAMPAIGNS
Subscribe Set List Create
Unsubscribe Get List Draft
Delete subscriber Active subscriber count Send
Subscription status List Segments handling Assign to brands

Getting Started

Getting started is easy. Here’s how you do it. You can check the example.php file as well. Obviously, you’ll have to download the wrapper to your current setup. Several ways to do that.

#1 MANUAL INSTALL:

Git clone this repo and include ./src/Sendy.php in your project.

#2 COMPOSER INSTALL:

Composer Install is the prefered method.

composer require ahmadawais/sendy-php-api

Step 0. Require the wrapper

require_once( 'Sendy.php' );

Step 1. Configure it

// 2. Configuration.
$config = [
    'sendyUrl' => 'https://send_installation_url.com', // Your Sendy installation URL (without trailing slash).
    'apiKey'   => 'XXXXXXXXXXXXXXXX', // Your API key. Available in Sendy Settings.
    'listId'   => 'XXXXXXXXXXXXXXXX',
];

Step 2. Init

$sendy = new \SENDY\API( $config );

API KEY METHODS

  1. Method: subscribe().
  2. Method: unsubscribe().
  3. Method: subStatus().
  4. Method: delete().
  5. Method: subCount().
  6. Method: campaign().

Method #1: Subscribe

// Method #1: Subscribe.
$responseArray = $sendy->subscribe(
    [
        'email'     => '[email protected]', // This is the only field required by sendy.
        'name'      => 'Name', // User name (optional).
        'custom'    => 'Field Value', // You can custom fields as well (optional).
        'country'   => 'US', // User 2 letter country code (optional).
        'ipaddress' => 'XX.XX.XX.XXX', // User IP address (optional).
        'referrer'  => 'https://AhmadAwais.com/', // URL where the user signed up from (optional).
        'gdpr'      => true, // GDPR compliant? Set this to "true" (optional).
    ]
);

Method #2: Unsubscribe

$responseArray = $sendy->unsubscribe( '[email protected]' );

Method #3: Subscriber Status

$responseArray = $sendy->subStatus( '[email protected]' );

Method #4: Delete Subscriber

$responseArray = $sendy->delete( '[email protected]' );

Method #5: Subscriber Count of a list

$responseArray = $sendy->subCount();

Method #6: Campaign — Draft And/Or Send as well

// Method #6: Campaign — Draft And/Or Send as well.
$responseArray = $sendy->campaign(
    array(
        'from_name'            => 'Your Name',
        'from_email'           => '[email protected]',
        'reply_to'             => '[email protected]',
        'title'                => 'Title', // the title of your campaign.
        'subject'              => 'Your Subject',
        'plain_text'           => 'An Amazing campaign', // Optional.
        'html_text'            => '<h1>Amazing campaign</h1>',
        'brand_id'             => 1, // Required only if you are creating a 'Draft' campaign. That is `send_campaign` set to 0.
        'send_campaign'        => 0, // SET: Draft = 0 and Send = 1 for the campaign.
        // Required only if you set send_campaign to 1 and no `segment_ids` are passed in.. List IDs should be single or comma-separated.
        'list_ids'             => 'XXXXXXXX, XXXXXXXX',
        // Required only if you set send_campaign to 1 and no `list_ids` are passed in. Segment IDs should be single or comma-separated.
        'segment_ids'          => '1',
        // Lists to exclude. List IDs should be single or comma-separated. (optional).
        'exclude_list_ids'     => '',
        // Segments to exclude. Segment IDs should be single or comma-separated. (optional).
        'exclude_segments_ids' => '',
        'query_string'         => 'XXXXXXXX', // Eg. Google Analytics tags.
    )
);

Method #7: Set List ID

// Method #7: Change the `XXXXXXX` you are referring to at any point.
$sendy->setListId( 'XXXXXXX' );

Method #8: Get List ID

// Method #7: Get the `XXXXXXX` you are referring to at any point.
$sendy->getListId();

Response

The response of this PHP wrapper is custom built. At the moment, it always returns a PHP Array. This array has the status of your action and an appropriate message in the response.

    // E.g. SUCCESS response.
    [
        'status'  => true,
        'message' => 'Already Subscribed'
    ];

    // E.g. FAIL response.
    [
        'status'  => false,
        'message' => 'Some fields are missing.'
    ];

Change log

Changes to the "Sendy-PHP-API" for Sendy.

Automated release notes can be found here →

License & Credits

The code is licensed under MIT and a huge props to Jacob Bennett for his initial work on the lib.
Requires at least PHP 5.3.0 (otherwise remove the namespace).


🙌 WPCOUPLE PARTNERS:

This open source project is maintained by the help of awesome businesses listed below. What? Read more about it →

This also happens to be my first composer package, so yay! 🎉

Feedback is more than welcomed at GitHub.

📨

TheDevTakeaway: Developers' Newsletter!

Takeaway my professional opinion about Open Source, JavaScript/Cloud, Full Stack Development, building a business, growing a family, and everything in between! Delivered to your inbox once a month. 🤣 I'm even funny some times. 👻 I hate spam! Pinky Promise!

🎩 Full-time #OpenSourcerer 🥑 JavaScript Developer Advocate 🔥 Ridiculously hard-working Full Stack Web DeveloperRegular WordPress Core Developer 📣 TEDx Speaker ✌️ Spends 50-80% of his time building professional FOSS (Free & Open Source Software) used by over 103,700 Developers 🙌 Cloud Community Evangelist 🎯 JavaScript & DevOps fanboy 😂 Cracks silly jokes 💜 Loves his wife (Maedah) 🍕 Learn more about Ahmad ↣

Say 👋 on Twitter @MrAhmadAwais | Subscribe to the WordPress Takeaway!