Join Newsletter on User Registration

By gversion - March 22, 2019

Hello,

I have added a checkbox to the account registration form so that users can opt into the newsletter.

If the user opts into the newsletter then they receive the email from NewsletterBuilder asking them to confirm their subscription, which is great.

The new user is always created in the database, which is also great.

The problem is that the USER-SIGNUP email template is no longer being sent, which crucially contains the user's password.

Below is the code I am using:

    // add user
    if (!$errorsAndAlerts) {

      // generate password
      $passwordText = wsm_generatePassword();
      $passwordHash = getPasswordDigest($passwordText);

      //
      $colsToValues = array();
      $colsToValues['createdDate=']     = 'NOW()';
      $colsToValues['updatedDate=']     = 'NOW()';
      $colsToValues['createdByUserNum'] = 0;
      $colsToValues['updatedByUserNum'] = 0;

      // fields defined by form:
      //$colsToValues['agree_tos']      = $_REQUEST['agree_tos'];
      $colsToValues['first_name']         = $_REQUEST['first_name'];
      $colsToValues['last_name']         = $_REQUEST['last_name'];
      $colsToValues['email']            = $_REQUEST['email'];
      $colsToValues['username']         = coalesce( @$_REQUEST['username'], $_REQUEST['email'] ); // email is saved as username if usernames not supported
      $colsToValues['password']         = $passwordHash;
      // ... add more form fields here by copying the above line!
      $userNum = mysql_insert(accountsTable(), $colsToValues, true);

      // set access rights for CMS so new users can access some CMS sections
      $setAccessRights = true; // set to true and set access tables below to use this
      if ($setAccessRights && accountsTable() == "accounts") { // this is only relevant if you're adding users to the CMS accounts table

        // NOTE: You can repeat this block to grant access to multiple sections
        mysql_insert('_accesslist', array(
          'userNum'      => $userNum,
          'tableName'    => '_listings',   // insert tablename you want to grant access to, or 'all' for all sections
          'accessLevel'  => '6',         // access level allowed: 0=none, 6=author, 9=editor
          'maxRecords'   => '',          // max listings allowed (leave blank for unlimited)
          'randomSaveId' => '123456789', // ignore - for internal use
        ),true);
      }

      $subscribe = 0;
      if (isset($_REQUEST['subscribe'])) {
        $subscribe = 1;
      }
      
      //add to subscribers
      if ($subscribe == 1){
        $_REQUEST['e'] = $colsToValues['email'];
        $_REQUEST['subscribe'] = 'Update Subscriptions';
        $_REQUEST['submitForm'] = 1;
        $_REQUEST['n'] = "";
        $_REQUEST['a'] = "";
        $_REQUEST['lists'] = array(1);

        list($errorsAndAlerts, $lists, $authUserNum, $authUserEmail) = nlb_frontend_dispatcher3();
      }

      // send message
      list($mailErrors, $fromEmail) = wsm_sendSignupEmail($userNum, $passwordText);
      if ($mailErrors) { alert("Mail Error: $mailErrors"); }

      // show thanks
      $_REQUEST        = array(); // clear form values
      $showSignupForm  = false;
      redirectBrowserToURL("/thankyou-register.php");
      exit;
    }

Could someone please help me out with this?

Thank you,

Greg