Automatically subscribe users to DreamHost announce lists

2009 August 19
tags: api, dreamhost
by Eddie

This is a response to a question on the DreamHost wiki posted by anonymous.

“I have a Contact page using form mail, and want to include a checkbox that enable visitors, to also subscribe to our Announce List when posting their form mail.

Is there a facility for adding users to the Announce List without using form POST”

Without using POST? I am not sure about that.. but using a checkbox to subscribe users is a snap.

Your resources;

  • DreamHost Panel API
  • PHP curl methods

So it goes like this;

Your user registers a new account, or sends you a contact message. As part of the form they submit their email address and name.  We in turn pass that email and name onto DreamHost’s panel API thereby adding the user to future announcements.

The code I will explain needs to go in the form’s receiving code. We will check for the checkbox’s value, and if necessary handle the additional choir. This code should be pasted into your contact form or registration form wherever the initial data is received and handled.

The red boundary denotes new code for the announce submission
The red boundary denotes new code for the announce submission

Since every cms, site and blog are different, I am unable to provide specifics, but yu should be able to track down the code for your form and find the portion that handles the submission. (Most obviously denoted with $_POST or $_GET variable use.)  I will also assume your email field is named ‘email’ and your name field is two fields; ‘firstName’, ‘lastName’;

Details on the API command

Below is a link to details on the command, needed parameters and possible responses.
Add Subscriber API Command

The Announce Submission Code

if(isset($_POST['subscribeMe']) && $_POST['subscribeMe'] == 1)
{
 
       //get the values we need from form(this should in aprt already be somewhere in the code your editing)
       $email=$_POST['email'];
       $fullName=$_POST['firstName']." ".$_POST['lastName'];
 
       //set values we shoudl know, and are constant
       $domain="domain of this form";
       $listname="list-name";
       $apiKey="666666666666";
 
       // using curl and passing 5 critical values to the api
       // list, domain, email, name and API Key
 
 
		$ch = curl_init('https://api.dreamhost.com/');
 		curl_setopt ($ch, CURLOPT_POST, 1);
 		curl_setopt ($ch, CURLOPT_POSTFIELDS, "key=$apiKey&cmd=announcement_list-add_subscriber&listname=$listname&domain=$domain&email=$email&name=$fullName");
 		$result=curl_exec ($ch);
 		curl_close ($ch);
 
		if ( stripos( $result,'success') > 0){
			echo "<h2>Congrats! </h2><p>You have been added to our Mailing List</p>";
		}else{
			echo "<h2> Ooops! </h2><p>Unable to add your email to our announcement list please contact site administrator.</p>";
			echo "Code: " . $result;
		}
}// end if subscribe box checked

Getting a DreamHost API key

More info on DreamHost API

9 Responses leave one →
  1. 2010 February 4
    Colin Goodier permalink

    Hi Edward,

    Thanks for the code, very useful However, I’m not sure if the line

    if ( stripos( $result,’success’) > 0){

    is right? I seem to be getting a ’1′ as the response in $result. Maybe boolean ‘true’? Leaving the code as above results in an error, although the subscription succeeds, and I get the ‘success subscribed’ text ok, but it just appears in the form response page. I think the error was because $result doesn’t contain the ‘success subscribed’ response that your code seems to expect?

    Maybe that needs to be handled differently.

    Colin

  2. 2010 February 4
    Eddie permalink

    @Colin
    Sorry you’re having troubles, let’s see if we can’t fix them.

    When I wrote the article above it worked, and was based on the API documentation, http://wiki.dreamhost.com/Api#announcement_list-add_subscriber, which states;

    Result:

    success
    sent_opt_in_email

    Possible Errors:

    no_listname
    no_domain
    no_such_listname
    no_such_domain
    no_email
    invalid_email (may have specifics after a tab)
    already_subscribed
    email_may_not_be_added_to_any_dreamhost_list_ever
    email_requested_to_be_added_in_last_two_days_already
    internal_error_opting_in

    So the code you reference checks if the result contains success. (stripos returns 0 if false, otherwise the position), so we check that it is greater than zero, is that the “1″ your referring to?

    You could use a series of if/else blocks to handle any scenario.

    Do you have php debugging on? are you seeing any specific errors?

  3. 2010 February 4
    Colin Goodier permalink

    Hi Eddie,

    I just changed the code to check for ’1′ in $result, and it worked, in the sense that it no longer gave me the error message.

    The thing is, the functionality worked fine, it did what it was supposed to (add a subscriber to the list), but it was giving me the output of the

    echo ” Ooops! Unable to add your email to our announcement list please contact site administrator.”;
    echo “Code: ” . $result;

    line, as if it hadn’t worked (when it had).

    I thought that ‘stripos’ was checking for a particular text string in $result, which wasn’t there, but maybe I misread that?

    Anyway, as I say, it seems to be working ok for me having made that one change, I just thought I’d give you the feedback on it. It does the job fine!

    Colin

  4. 2010 February 4
    Colin Goodier permalink

    Eddie,

    Just re-read your response and took it in a bit more. You say

    “So the code you reference checks if the result contains success. (stripos returns 0 if false, otherwise the position), so we check that it is greater than zero, is that the “1″ your referring to?”

    That’s what I thought it was doing. You’re checking for the string ‘success’ in $result, right? But I checked the value of $result, and it doesn’t contain a string.
    In the case of a successful operation it contains the value ’1′ (at least when I run it), which is presumably why the ‘stripos’ line return an error. Maybe the Dreamhost guys changed something? It seems to be that $result contains a boolean value, not a string.

    Colin

  5. 2010 February 5
    Colin Goodier permalink

    Hi Eddie,

    I got it. The answer is in the PHP manual entry for curl_exec()

    “Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure. ”

    The way you’ve got it set up you are returning TRUE or FALSE in $result, but you are checking for the result (i.e. ‘success’).

    So if you add the line

    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

    The ‘stripos’ line should work correctly.

    Colin

  6. 2010 February 6
    Eddie permalink

    Thanks Colin. That is a very useful find.

  7. 2010 March 6
    Dave Gamble permalink

    Heya, thanks for this article.

    Here’s the cleanup for the response testing:

    $result will be ===FALSE if the curl operation itself fails.
    (yes, triple equals.)

    if (stripos($a,$b)>1) is an improper test, please see:
    http://php.net/manual/en/function.stripos.php

    To correct the code above, you need to change:
    if ( stripos( $result,’success’) > 0){
    to
    if ( $result!==false && stripos( $result,’success’)!==false){

    This is because stripos returns logical false if it doesn’t find it.
    Another neat (perhaps better?) way to test would be !strncasecmp($result,’success’,7)

    Also in your errorcode handling, you probably want to test the case where $result===false and report that as a failure to connect to the server.

    Thanks very much, this tutorial helped me A LOT!! :D

    Dave.

  8. 2010 May 25
    Eddie permalink

    @Dave

    Thanks for the share Dave! I am really glad this article helped you too. DreamHost is building a great API that is useful for all sorts of domain admin scenarios.

Trackbacks & Pingbacks

  1. links for 2010-02-03 | Pratyush Kotturu - KE5YQZ

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS