Ahmad Awais

NAVIGATE


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: β˜… 85 β‘‚ 21 β†’#

πŸš€ 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 preferred method.

composer require ahmadawais/sendy-php-api

Step 0. Require the wrapper#

// New way using PSR4 Standard autoloader. Recommended
require_once . '/vendor/autoload.php';

// Old way of requiring all files manually. Not recommended.
require_once( 'API.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#

use AhmadAwais\Sendy\API as Sendy;

$sendy = new Sendy( $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.'
    ];

πŸ“

Changelog#

❯ Read the changelog here β†’

KEY: πŸ“¦ NEW, πŸ‘Œ IMPROVE, πŸ› FIX, πŸ“– DOC, πŸš€ RELEASE, and βœ… TEST

I use Emoji-log, you should try it and simplify your git commits.

πŸ“ƒ

License & Conduct#

πŸ™Œ

Connect#

GitHub @AhmadAwais (follow) To stay up to date on free & open-source software

Twitter @MrAhmadAwais (follow) To get #OneDevMinute daily hot tips & trolls

YouTube AhmadAwais (subscribe) To tech talks & #OneDevMinute videos

Blog: AhmadAwais.com (read) In-depth & long form technical articles

LinkedIn @MrAhmadAwais (connect) On the LinkedIn profile y’all

πŸ‘Œ

Me (Ahmad Awais) and my incredible wife (Maedah Batool) are two engineers who fell in love with open source and then with each other. You can read more about me here. If you or your company use any of my projects or like what I’m doing then consider backing me. I’m in this for the long run. An open-source developer advocate.

Ahmad on Twitter

Ahmad on Twitter

This also happens to be my first composer package, so yay! πŸŽ‰

Feedback is more than welcomed at GitHub.

Founder & CEO at Langbase.com Β· Ex VP DevRel Rapid Β· Google Developers Advisory Board (gDAB) founding member. πŸ§‘β€πŸ’» AI/ML/DevTools Angel Investor ❯ AI/ML Advisory Board member San Francisco, DevNetwork

🎩 Award-winning Open Source Engineer & Dev Advocate 🦊 Google Developers Expert Web DevRel πŸš€ NASA Mars Ingenuity Helicopter mission code contributor πŸ† 8th GitHub Stars Award recipient with 3x GitHub Stars Award (Listed as GitHub's #1 JavaScript trending developer).

🌳 Node.js foundation Community Committee Outreach Lead, Member Linux Foundation, OpenAPI Business Governing Board, and DigitalOcean Navigator. πŸ“Ÿ Teaching thousands of developers Node.js CLI Automation (100 videos Β· 22 Projects) & VSCode.pro course. Over 142 Million views, 22 yrs Blogging, 56K developers learning, 200+ FOSS.

✌️ Author of various open-source dev-tools and software libraries utilized by millions of developers worldwide ⓦ WordPress Core Developer πŸ“£ TEDx Speaker with 100+ international talks.

✨ As quoted by: Satya Nadella Β· CEO of Microsoft β€” Awais is an awesome example for developers.
πŸ™Œ Leading developers and publishing technical content for over a decade πŸ’œ Loves his wife (Maedah) ❯ Read more about Ahmad Awais.

πŸ‘‹β€¦ Awais is mostly active on Twitter @MrAhmadAwais

πŸ“¨

Developers Takeaway

Stay ahead in the web dev community with Ahmad's expert insights on open-source, developer relations, dev-tools, and side-hustles. Insider-email-only-content. Don't miss out - subscirbe for a dose of professional advice and a dash of humor. No spam, pinky-promise!

✨ 172,438 Developers Already Subscribed
Comments 3
  • Hasse
    Posted on

    Hasse Hasse

    Reply Author

    Hi,

    I understand that you are a good coder that made stuff with SENDY API.

    Do you know if itΒ΄s possible to do this with Sendy?
    – Automatically create a unique mail list in Sendy when a new WP blog post is created
    – When a user follow the unique blog post he will be added only to that unique list
    – If the blog post gets updated or commented all of the followers should automatically receive an email notice
    – Is a part of the solution to add the “blog ID” as a custom field and then make some segmentation on the list?
    – I canΒ΄t use a static sign up widget thatΒ΄s already assigned to a single list, it must be dynamic
    – Is there a limit for how many list Sendy can handle?
    – A lot of petition/crowdfunding sites have this feature

    I have examined over 100 different plug-ins and most of them are outdated. Also spent several hours of reading different forums. 90% of the answers is Mailchimp :(

    I would really appreciate a hint or link to something thatΒ΄s at least built 2019!

    Regards,

    //Hasse


    • Ahmad Awais
      Posted on

      Ahmad Awais Ahmad Awais

      Reply Author

      All of this is very easily possible with Sendy but it’s custom code. You will either have to write it yourself or hire a decent programmer to do it for you.


  • Catalin Ionescu
    Posted on

    Catalin Ionescu Catalin Ionescu

    Reply Author

    Hi
    Can you help me with this project?
    I have a Sendy installed and an email builder.
    All I want is the posibillitiy toi save from builder straight to Sendy as a template depending of wich brand client am I. i have for example more brand clients inside Sendy and they will have different builder interface (one user , one api). So every builder file is other link.
    Thank you so much