Archive for the 'Bronto' Category

Bronto, PHP, and SOAP - Adding Contacts, Part II + Sending Messages

Earlier I outlined a problem I was trying to solve via the Bronto SOAP API. To give you a better idea what I’m going for, here’s a more extensive description …

When people purchase something from my client, at the bottom of the form, there is a checkbox they can check to elect to subscribe to my client’s email list. We want to do a double opt-in situation … so once they check out, they’ll then need to be sent a confirmation email, and then they confirm their subscription. The idea is that we want to be absolutely sure that the people on the receiving end of my client’s marketing emails really do want to receive them.

In my client’s Bronto account, we’ve set up a confirmation/welcome email, and previously we were importing the list of emails on a weekly basis. When you manually import contacts like this, Bronto gives you the option of sending a confirmation email. We wanted to automate this process such that each person was automatically sent a confirmation email within moments of checking out. The big issue was that many people were failing to confirm, probably because of the time difference between them actually buying and receiving the confirmation email.

The problem … in Bronto, you cannot use the API to simply send a pre-existing message (our welcome/confirmation message) to a single person … you have to send to a list.

So, my first idea was to automatically add each user to a “Confirmation Queue” mailing list. Every five minutes or so, I’d have an automated process run that scheduled a delivery of the welcome/confirmation message to everybody in the “Confirmation Queue” … and then the next time the script ran, those people would be removed from the queue, and it would schedule another delivery to the people still in the queue (if there were any).

I quickly ran into a problem with my new plan … Bronto won’t send a message to an unconfirmed contact (as far as I can tell). Of course, the contact has to be unconfirmed until they get the confirmation message, otherwise we run the risk of sending messages to people who haven’t completed the double opt-in process for some reason. And … there doesn’t appear to be any way to tell Bronto that I’m sending a confirmation message, and thus actually send to these people. So … it’s time for a new plan.

My new plan involves handling the confirmation email myself … I’ll store a list of people who have completed the first step of the confirmation process (the checkout opt-in), and immediately send them a confirmation email. The confirmation link in the email will trigger my app, which will then add them to the appropriate mailing list in Bronto as verified people, and remove them from the queue. People who haven’t confirmed within 7 days will be removed automatically. So … this appears to be a workable solution.

But … what if you do want to send an email from Bronto? Well, first we’re going to assume you’ve actually already created the message. What we’re going to do is add another method to my existing Bronto class from my previous post that will take care of this for us.

In order to send a message, we need the Bronto ID of at least one list (or segment), and the Bronto ID of the message we want to send. I already showed you how to dump a list of your existing lists in the last installment … and getting a list of messages is equally simple … you just make a call to readMessages(). Add the following to your Bronto class:


public function dumpMessages()
{
    header('content-type: text/plain');
    print_r($this->_conn->readMessages());
    exit;
}

Then just find the ID of the message you want to send. Now to send the actual message, add the following method to the Bronto class:


public function sendMessage($listID, $messageID)
{

    // Set the default date to UTC, which is the way Bronto understands time.
    date_default_timezone_set('UTC');

    // Set up the parameters for our mailing.
    $mailing = array(
        'start' => date('Y-m-d h:i:s'),
        'messageId' => $messageID,
        'recipients' => array(
            array('type' => 'list', 'id' => $listID)
        ),
        'fromEmail' => 'sender@example.com',
        'fromName' => 'Bronto Test',
        'replyEmail' => 'replyto@example.com'
    );

    // Now we're going to send the message.
    $rs = $this->_conn->writeDeliveries(array(
        'deliveries' => array($mailing),
        'handler' => array('mode' => 'insert')
    ));

}

Now … let’s look at what’s happening with the writeDeliveries() method … you’re passing it two parameters, ‘deliveries’ and ‘handler.’ ‘Deliveries’ is an array, and it will contain one or more associative arrays (or hashes) describing the mailing or mailings that you are scheduling. All of the items I’ve put in my $mailing array are required. The ’start’ parameter describes when the mailing will be sent. It is important that you define your timezone as UTC as I’ve done, and also assure that the time on your server is accurate … otherwise your mailing may not be sent when you expect. As far as I can tell, if you specify any time in the past here, Bronto will schedule the mailing to go out as soon as possible.

The ‘recipients’ property can take an array of associative arrays, each of which describe a list. The ‘type’ is either going to be ‘list’ or ’segment’.

For the ‘handler’ option in writeDeliveries(), there is only one option, mode, and it can only be set to ‘insert’. Go figure. You’d think it would be optional in this case, but it isn’t … perhaps in the future there will be more options here, maybe so you can modify a mailing scheduled in the future.

Beyond that, it should be pretty self explanatory. Good luck!

Bronto (Email Marketing), SOAP and PHP - Adding New Contacts

I’ve been working with a client that has been using Bronto for email marketing purposes for several months now. Recently, we made the decision to activate Bronto’s SOAP API access for their account so that we can add contacts to our mailing lists automatically. One reason we wanted to do this was because a few months ago, we put in place a privacy policy that specified that we’d use a double opt-in methodology for adding people to our lists, and we found that manually importing the contacts every so often just wasn’t working … only about 1-2% of the people who had checked the first opt-in box were confirming their subscription. I expected that some people might change their mind and not confirm, or the confirmation request might get caught in their spam folder/filter, etc., but 1-2% is pretty low. My guess is people forgot they’d checked the “yes, I want to subscribe to your newsletter” box in the days since they’d done so, or something like that.

Documentation for the Bronto API exists, but is sparse for somebody who is just getting started with it, even if they do have a good bit of experience with SOAP. Once I got past the provided “Hello, World!” tutorial, I found myself in the dark using trial and error to figure out how things worked.

So, I decided to start out by demonstrating how to add (or update) a contact, and add that contact to a mailing list in your Bronto account.

I’m assuming that anybody reading this has already enabled API access (it’s $150 extra a month), created a user with API access in their Bronto account, and has already figured out how to successfully log in. With no further ado, here’s some example code showing how to add a contact. In my case, I’ve created a Bronto class that allows me to do things easily, and I expect that I’ll add to it as time goes by and our operations get more complicated.


<?php

class Bronto
{
    
    private $_conn = null;
    private $_sessionID = null;
    
    public function __construct()
    {
        // Connect to the Bronto API and tell it to throw SoapExceptions
        $this->_conn = new SoapClient(BRONTO_WSDL, array('exceptions' => true));
        $rs = $this->_conn->login(array(
            'username' => BRONTO_USER,
            'password' => BRONTO_PASS, 
            'sitename' => BRONTO_SITE
        ));
        // Adjust the target URL (in case it's changed) and set the session var in the
        // SOAP header
        $this->_sessionID = $rs->return->sessionId;
        $header = new SoapHeader('http://api.bronto.com', 'sessionHeader', 
            array('sessionId' => $this->_sessionID));
        $this->_conn->__setLocation($rs->return->serviceURL);
        $this->_conn->__setSoapHeaders(array($header));
    }
    
    public function writeContact($email, $format = 'html')
    {
        try {
            // Insert this contact into our account (or update them if they
            // are already there), set them as active, and add them to 
            // a mailing list. Tell Bronto to do these things in insertUpdate mode.
            $rs = $this->_conn->writeContacts(array(
                'contacts' => array(
                    array(
                        'email' => $email,
                        'status' => 'active',
                        'msgPref' => $format,
                        'customSource' => '[source of this contact]‘,
                        ‘lists’ => array(’[mailing list id]‘)
                    )
                ),
                ‘handler’ => array(
                    ‘mode’ => ‘insertUpdate’,
                    ‘listsMode’ => ‘insertUpdate’
                )
            ));
        } catch (Exception $e) {
            // gracefully handle the error somehow
        }
    }
    
}

?>

Then to actually use this code, assuming you’ve defined BRONTO_WSDL, BRONTO_USER, and BRONTO_PASS somewhere, you do this:


$bronto = new Bronto();
$bronto->writeContact('j.clifton@four-eight-four.com', 'html')

That’s it!

The tough part of this, for me, was figuring out exactly what to pass the writeContacts() SOAP method … even with the documentation. My major issue was figuring out out specify the list I wanted. It’s not clear in the documentation, so first I assumed that I was just passing the list name … but that’s not the case. After blindly trying a few things, I finally figured out that you have to pass it the list id. Finding your lists id is pretty easy … once you’ve connected, just do this (in my example, you’d have to add a function to the Bronto class):


public function dumpListIDs()
{
    header('content-type: text/plain');
    print_r($this->_conn->readLists());
    exit;
}

That will give you something like this for the output:


stdClass Object
(
    [return] => stdClass Object
        (
            [lists] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 07d003ec000000000000000000000000abcd
                            [name] => Corporate Clients
                        )

                    [1] => stdClass Object
                        (
                            [id] => 07d003ec0000000000000000000000001234
                            [name] => Main Mailing List
                        )

                    [2] => stdClass Object
                        (
                            [id] => 07d003ec000000000000000000000000ac24
                            [name] => Special Offers
                        )

                )

        )

)

Now … there’s only one problem with this setup. My initial intent was to have the new contact set as unconfirmed, send them an “invitation” so they could confirm their subscription, and add them to the main mailing list. However … if you try to add a contact the way using the method I just demonstrated, but set their status as “unconfirmed”, Bronto complains “Insert requires active status” … so that’s not going to work. So, it’s back to the drawing board for me, until I can figure out my next step (or either contact Bronto support on Monday if I can’t figure it out over the weekend). When I do, I’ll post a follow-up.