Newsletter Plugin - Subscriber's name and any other field?

8 posts by 3 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: May 14, 2013   (RSS)

Is there a hook in the process to allow the use of more than just an email address?  Most of my clients that use another open-source list manager almost always request first and last names so they can personalize the newsletter going out.  Some of them also request the ability to have other fields in the register form such as birth month/day, anniversary month/day, etc. so they can send messages to these people on these special days.

Does the newsletter system offer any of these capabilities?

Thanks,

Gary.

Hi Gary,

Unfortunately, there isn't currently a way you can only send the news letter to users on specific days or define which users receive the newsletter.

But I have come up with a workaround for adding subscriber related placeholders to the newsletter. If you open up the newsLetterBuilder.php file, which you should find in your plugins directory. Around line 913 you should see the function nlb_placeholders, I've added a new line that will load the user fields from the subscribers section into the placeholders:

function nlb_placeholders($to_email = '', $messageNum = '0', $subscriber = array()) {
  $placeholders = array();

  //Set users based variables to a placeholders first.
  $placeholders = mysql_get('_nlb_subscribers', null, "email = '$to_email'");

  // load/cache newsletter settings
  static $newsletterSettings;
  if (!isset($newsletterSettings)) { $newsletterSettings  = mysql_get('_nlb_settings', 1); }

  // define shortcut vars
  $subscriberNum     = @$subscriber['num'] ? $subscriber['num']     : '0';
  $subscriberAuthkey = @$subscriber['num'] ? $subscriber['authkey'] : '0';

  // define placeholders - values defined lower in the list can use placeholder defined above them in the list
  // internal placeholders (undocumented)
  $placeholders['message_num']     = $messageNum;
  $placeholders['subscriber_num']  = @$subscriber['num'];
  $placeholders['manage_url']      = $newsletterSettings['manage_url'];

  // public placeholders - values defined lower in the list can use placeholder defined above them in the list
  $placeholders['email_header']    = ''; // placeholder removed in v2.03 - remove the placeholder if it's left in old newsletter content
  $placeholders['email_footer']    = ''; // placeholder removed in v2.03 - remove the placeholder if it's left in old newsletter content
  $placeholders['hostname']        = @$_SERVER['HTTP_HOST'];
  $placeholders['from_name']       = $newsletterSettings['from_name'];
  $placeholders['from_email']      = $newsletterSettings['from_email'];
  $placeholders['to_email']        = $to_email;
  $placeholders['archive_url']     = $newsletterSettings['archive_url'];
  $placeholders['confirm_url']     = $newsletterSettings['manage_url'] . "?n=$subscriberNum&a=$subscriberAuthkey";
  $placeholders['unsubscribe_url'] = $newsletterSettings['manage_url'] . "?n=$subscriberNum&a=$subscriberAuthkey&m=$messageNum&u=1"; // if usernum(n) isn't specified user will just get a debug error message

  // make archive_url trackable
  if ($GLOBALS['NLB_TRACK_LINKS'] && $to_email) { // don't encode for viewer pages
    $encodedLink = urlencode(_nlb_htmlspecialchars_decode($placeholders['archive_url']));
    $placeholders['archive_url'] = $newsletterSettings['manage_url'] . "?l=$encodedLink&m=$messageNum&n=$subscriberNum";
  }

  //
  return $placeholders;
}

So if you added a field called age to the subscribers section. You could add this field to the newsletter by adding #age# where you wanted the age to appear.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Sorry I didn't reply sooner.  I've been out of the office quite a bit this week.

Thank you for the great information.  I will take a look at it.

Gary.

By gversion - April 15, 2013

Hi Greg,

This modification is great! Is it going to be featured by default in future releases of the Newsletter plugin?

Thanks,

Greg

By gversion - May 7, 2013

Hi Greg,

Could you please explain how I can collect the user's "first name" in the newsletter subscription form?

I have tried the following to collect the first name but it doesn't work, only the email is collected:

                                <form method="post" action="#">
                                    <input type="hidden" name="submitForm" value="1" />
                                    <input type="text" name="first_name" value="<?php echo htmlencode(@$_REQUEST['first_name']) ?>" size="30"/>
                                    <input type="text" name="e" value="<?php echo htmlencode(@$_REQUEST['e']) ?>" size="30"/>
                                    <input type="submit" name="subscribe" value="Subscribe" />
                                </form>

The PHP code in the HEAD is as follows:

<?php
  // error checking
  if (!@$GLOBALS['NEWSLETTER_BUILDER_PLUGIN']) { die("You must activate the newsletter plugin to see this page."); }

  // load newsletter settings
  $newsletterSettings  = mysql_get('_nlb_settings', 1);

  // dispatch form actions
  list($errorsAndAlerts, $isConfirmSubscribe, $isConfirmUnsubscribe) = nlb_manage_dispatch();


?>

I'd be very grateful for any help you can provide.

Thank you,

Greg

By gregThomas - May 13, 2013

Hi Greg,

Because of the way the subscription system works the easiest method is to change the subscription page so that the saving and sending of an e-mail to a user takes place on that page instead of the nlb_manage_dispatch function. That way you can have full control of what fields are processed and on which error checking is carried out on. I've attached an updated version of the m.php file, this is the main change to the top of the page:

  if(@$_REQUEST['saveUser']){
        // error checking
    if     (!@$_REQUEST['e'])              { $errorsAndAlerts .= "No email address specified!<br/>\n"; }
    elseif (!isValidEmail($_REQUEST['e'])) { $errorsAndAlerts .= "Invalid email specified, email must be in the format: user@example.com<br/>\n"; }
    if(!@$_REQUEST['first_name'])          { $errorsAndAlerts .= "You must enter your full first name<br/>\n";}

    //
    if (!$errorsAndAlerts) {

      // convert "Display Name" <local-part@domain> to local-part@domain
      $_REQUEST['e'] = array_value(isValidEmail($_REQUEST['e']), 0, 0);
      $subscriber    = mysql_get('_nlb_subscribers', null, array('email' => $_REQUEST['e']));

      // create subscriber if they don't already exist
      if (!$subscriber) {
        $colsToValues = array(
          'email'      => $_REQUEST['e'],
          'first_name' => $_REQUEST['first_name'],
          'authkey'    => _nlb_generateAuthKey()
        );
        $recordNum    = mysql_insert('_nlb_subscribers', $colsToValues, true);
        $subscriber   = mysql_get('_nlb_subscribers', $recordNum);
      }

      // send message & show alert
      $errorsAndAlerts = '';
      if ($subscriber['confirmed']) {
        $errorsAndAlerts = "You're already subscribed to this newsletter.<br/>\n";
      }
      else {
        nlb_log('0', $subscriber['num'], '1');

        $to                 = $subscriber['email'];
        $newsletterSettings = mysql_get('_nlb_settings', 1);
        $mailErrors         = nlb_sendMessage($to, $newsletterSettings['confirm_subject'], $newsletterSettings['confirm_html'], $subscriber);
        if ($mailErrors) { die("Mail Error: $mailErrors"); }

        // send alert
        $errorsAndAlerts .= "Thanks, we've emailed you at " .htmlencode($to). ", to confirm your subscription just check your mail and click the confirmation link.<br/>\n";
        $errorsAndAlerts .= "If you don't receive an email from us within a few minutes check your spam filter for messages from " .htmlencode($newsletterSettings['from_email']). "<br/>\n";
        $_REQUEST = array(); // clear form
      }
    }
  }

and here is the updated form:


  <form method="post" action="#">
    <input type="hidden" name="saveUser" value="1" />
    Email: <input type="text" name="e" value="<?php echo htmlencode(@$_REQUEST['e']) ?>" size="50"/>
    <input type="text" name="first_name" value="<?php echo htmlencode(@$_REQUEST['first_name']) ?>" size="30"/>
    <input type="submit" name="subscribe" value="Subscribe" /><br/>
  </form>

Before the processing of the form took place inside the nlb_manage_dispatch function, but I've changed it so that it takes place at the top of the subscription management page instead.  I've added error checking for the first name and added the first name field to the colsToValues array so that it is saved to the section. I've changed the name of the hidden field from submitForm to saveUser so that the nlb_manage_dispatch function doesn't try and process the data as well. Otherwise this is the same system that was used to save a user in the nlb_manage_dispatch function.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com
Attachments:

m_001.php 6K

By gversion - May 14, 2013

Hi Greg,

Thanks so much for your help with this. It seems there was more thought required than I expected!

I will let you know if I run into any problems.

Regards,

Greg