Adding custom text to emailOnApproved Plugin

By gkornbluth - September 6, 2015

Hi All,

I thought this would be an easy thing to do, but I’m stumped again. Hopefully, someone has already done this and can pass on a solution.

I’m using the emailOnApproved plugin (code below) and I’d like to be able to pull some text from a text box field called ‘custom_text’ in a single record editor called ‘email_inserts’ and insert that text into the body of the email message that’s sent by the plugin.

I tried adding a list records call and inserting a variable into the message body, but that broke both the plugin and the site, so I’m not sure how to accomplish my goal.

Thanks for your help.

Jerry Kornbluth

<?php
/*
Plugin Name: Email On Approved
Description: Email the user their password once they are approved
Version: 1.00
Requires at least: 2.04
*/
// UPDATE THESE VALUES

// DON'T UPDATE ANYTHING BELOW THIS LINE

addAction('record_postsave',  'emailOnApproved_sendPassword', null, 4);

//
function emailOnApproved_sendPassword($tableName, $isNewRecord, $oldRecord, $recordNum) {
  global $CURRENT_USER, $SETTINGS;
  $fieldname = 'approved';

  // error checking
    if ($tableName != 'accounts') { return; }
  if (!array_key_exists($fieldname, $CURRENT_USER)) {
    die(__FUNCTION__ .": You must create an accounts fields called '$fieldname'!");
  }

  // send email
  $wasChecked   = intval(!$oldRecord[$fieldname] && $_REQUEST[$fieldname]);
  $wasUnchecked = intval($oldRecord[$fieldname]  && !$_REQUEST[$fieldname]);

$message=<<< __TEXT__
    Welcome!  <br><br>
 
{I'd like to insert the $custom_text here}

<br><br>
Your subscription has been  processed successfully and you now have access to the Members Only area of our web site. <br> <br>
Your user name is: {$_REQUEST['username']} <br>
and your temporary password is: {$_REQUEST['password']}<br><br>
Once you have successfully logged in, you can change your password and update your profile information. <br><br>
 
 
<a href="http://www.your_website_URL.com{$GLOBALS['WEBSITE_LOGIN_LOGIN_FORM_URL']}">Click here to login</a> <br><br>
 
Best, <br>  <br>
    
The Subscription Committee  
__TEXT__;


$headers  = 'MIME-Version: 1.0' . "\r\n";  
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  
$headers .="FROM:". $SETTINGS['adminEmail'];  
 
   if ($wasChecked) {
    $errors = mail($_REQUEST['email'],"Your membership has been successfully processed!",$message,$headers);  
    if ($errors!=1) { die("Mail Error: $php_errormsg"); }
  }
 
}
 
?>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Daryl - September 9, 2015

Hi Jerry,

Did you get any PHP errors? If yes, what are the errors?

I tried adding a test value to $custom_text and display just the $message in a test file and it worked:

$custom_text = "TEST CUSTOM TEXT";

$message=<<< __TEXT__
    Welcome!  <br><br>
  
{$custom_text}

<br><br>
Your subscription has been  processed successfully and you now have access to the Members Only area of our web site. <br> <br>
Your user name is: {$_REQUEST['username']} <br> 
and your temporary password is: {$_REQUEST['password']}<br><br> 
Once you have successfully logged in, you can change your password and update your profile information. <br><br> 
  
  
<a href="http://www.your_website_URL.com{$GLOBALS['WEBSITE_LOGIN_LOGIN_FORM_URL']}">Click here to login</a> <br><br> 
  
Best, <br>  <br>
    
The Subscription Committee  
__TEXT__;

echo $message;

That means there is no problem in inserting the variable in the message unless your $custom_text have something in it's value that might have caused the error. Or could be caused by something else.
If you could provide us with more details about the errors, we'll be able to try to pin point the cause further.

Daryl Maximo
PHP Programmer - interactivetools.com

By gkornbluth - September 9, 2015 - edited: September 9, 2015

Hi Darryl,

Thanks for looking at this.

If I put a load records call at the top of the plugin

// load record from 'common_information'
  list($common_informationRecords, $common_informationMetaData) = getRecords(array(
    'tableName'   => 'common_information',
    'where'       => '', // load first record
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $common_informationRecord = @$common_informationRecords[0]; // get first record

I get this kind of error in the error log:

E_ERROR: Call to undefined function getRecords()
/home2/nawaflco/public_html/nawafl/cmsAdmin/plugins/emailOnApproved.php (line 13)
http://nawafl.com/cmsAdmin/admin.php

If I put it lower in the plugin, but before the

$message=<<< __TEXT__
    Welcome!  <br><br>

I get an internal server error when I try to save a user account record and hence no new error message

So the problem seems to be how to connect your variable$custom_text to a section so that it gets it's value from a (single record) section field

Looking forward to the next installment...

Best,

Jerry

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Daryl - September 9, 2015

Hi Jerry,

It looks like you haven't loaded the viewer functions into the plugin.
Can you try to add this somewhere on top of the plugin file?:

include_once("../lib/viewer_functions.php")

Daryl Maximo
PHP Programmer - interactivetools.com

By Daryl - September 9, 2015

Is your getRecords() for 'common_information' is outside of the plugin function? If yes, you should call it inside the plugin function.

Daryl Maximo
PHP Programmer - interactivetools.com

By gkornbluth - September 9, 2015 - edited: September 9, 2015

Great catch Darryl,

Here's the plugin code that worked for me, just in case anyone else can use it:

Best,

Jerry Kornbluth

NOTE: My single record section is called "common_information", the text field for the variable text is called "member_welcome", and the text variable is called "$member_welcome". Don't forget to change the server path.

<?php
/*
Plugin Name: Email On Approved
Description: Email the user their password once they are approved
Version: 1.00
Requires at least: 2.04
*/
// UPDATE THESE VALUES
// DON'T UPDATE ANYTHING BELOW THIS LINE
 
addAction('record_postsave',  'emailOnApproved_sendPassword', null, 4);
//
function emailOnApproved_sendPassword($tableName, $isNewRecord, $oldRecord, $recordNum) {
  global $CURRENT_USER, $SETTINGS;
  $fieldname = 'approved';
  // error checking
    if ($tableName != 'accounts') { return; }
  if (!array_key_exists($fieldname, $CURRENT_USER)) {
    die(__FUNCTION__ .": You must create an accounts fields called '$fieldname'!");
  }
  // send email
  $wasChecked   = intval(!$oldRecord[$fieldname] && $_REQUEST[$fieldname]);
  $wasUnchecked = intval($oldRecord[$fieldname]  && !$_REQUEST[$fieldname]);

// load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('/path_to_server/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); };

// load record from 'common_information'
  list($common_informationRecords, $common_informationMetaData) = getRecords(array(
    'tableName'   => 'common_information',
    'where'       => '', // load first record
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $common_informationRecord = @$common_informationRecords[0]; // get first record
  $member_welcome  = $common_informationRecord['member_welcome'];

$message=<<< __TEXT__
    Welcome {$_REQUEST['first_name']} {$_REQUEST['last_name']},<br><br>
 
{$member_welcome}<br><br>



Your membership has been  processed successfully and you now have access to the Members Only area of our web site. <br> <br>

Your user name is: {$_REQUEST['username']} <br>
and your temporary login password is: XXXXX <br><br>

Once you have successfully logged in, you must change your password and can then update your own profile information. <br><br>
 
<a href="{$GLOBALS['WEBSITE_LOGIN_LOGIN_FORM_URL']}">Click here to login</a> <br><br>
 
Best, <br>  <br>
    
The Membership Committee  
__TEXT__;


$headers  = 'MIME-Version: 1.0' . "\r\n";  
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  
$headers .="FROM:". $SETTINGS['adminEmail'];  
 
   if ($wasChecked) {
    $errors = mail($_REQUEST['email'],"Your membership has been successfully processed!",$message,$headers);  
    if ($errors!=1) { die("Mail Error: $php_errormsg"); }
  }
 
}
 
?>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php