<?php
/*
Plugin Name: Email New Account
Description: This plugin adds a link for admin users only to the accounts list that allows user to send email to new accounts after they have been activated.
Version: 1.0
Requires at least: 2.03
Author: Michael Aguilar
Version: 1.0 
Author URI: http://www.graphiclingoes.com
License: Freeware
*/

$GLOBALS['GL_ENA_LINK_NAME'] = 'email account activated';  // the link name in the list
$GLOBALS['GL_ENA_MEMBER_LOGIN_LINK'] = 'http://www.yoursite.com'; // membership link used in email
$GLOBALS['GL_ENA_MEMBER_LOGIN_LINK_TEXT'] = 'Your Member Site Text'; // membership link text used in email

// CAUTION UPDATING CODE BELOW THIS LINE WILL CHANGE/BREAK HOW PLUGIN FUNCTION, HOWEVER, IT CAN BE CHANGED TO CUSTIMIZE TO YOUR LIKING
addFilter('listRow_actionLinks',  'gl_ena_addLink', null, 3);
addAction('admin_postlogin',  'gl_ena_sendEmail');

// add "email account activated" link
function gl_ena_addLink($actionLinks, $tableName, $record) {
  // Only show link in the accounts list viewer
  if ($tableName != "accounts") { return $actionLinks; }
   // Check to make sure current use is an admin, if not only return standard links
  if (!$GLOBALS['CURRENT_USER']['isAdmin']) { return $actionLinks; }
  // create link
  $addLink       = "?menu=$tableName&_pluginAction=gl_ena_sendEmail&gl_ena_recordNum=" . $record['num'];
  $actionLinks  .= "<br/><a href='$addLink'>{$GLOBALS['GL_ENA_LINK_NAME']}</a>";
  return $actionLinks;
}
function gl_ena_sendEmail() {
    //register globals
	global $SETTINGS;
    global $TABLE_PREFIX;
	//set variables for this function
	$_gl_ena_tableName = @$_GET['menu'];
    $_gl_ena_recordNum = (int)@$_GET['gl_ena_recordNum'];
	
	// Lock feature down to admin users only
if (!$GLOBALS['CURRENT_USER']['isAdmin']) { return; } //
   // parse url to make see if email should be sent
if(isset($_GET['_pluginAction']) && $_GET['_pluginAction'] == 'gl_ena_sendEmail') {
   // Query database to get first name, last name and email address
   // NOTE "mysql_query_fetch_all_assoc($argument)" and "mysql_escape($argument)" are CMSB functions not PHP functions
   $query  = "SELECT firstName, email, lastName FROM {$TABLE_PREFIX}".mysql_escape($_gl_ena_tableName)." WHERE num = '" .mysql_escape($_gl_ena_recordNum). "'";
   $result = mysql_query_fetch_all_assoc($query) or die("Mysql error selecting email information: ". htmlspecialchars(mysql_error()) . "\n");
   // Loop through results and send email
  foreach($result as $record) {
  $to        = $record['email'];
  $subject   = "{$_SERVER['HTTP_HOST']} Account Activated";
  $message   = "<html>\n"
             . "<head>\n"
             . "<title>ADD YOUR TITLE HERE</title>\n"
             . "</head>\n"
             . "<body>\n"
             . "Hi " . $record['firstName'] . ",<br />\n"
             . "\n"
             . "<b>Your account for: {$_SERVER['HTTP_HOST']} has been activated</b>.\n"
             . "<br /><br />\n"
             . "You now access all the features available with your account. Thank you again for "
             . "creating your account, we look forward to assiting you achieve a better future!\n"
             . "<br /><br />\n"
             . "Click below to access your account:<br />\n"
             . "<a href=\"{$GLOBALS['GL_ENA_MEMBER_LOGIN_LINK']}\" title=\"ADD YOUR TITLE HERE\">{$GLOBALS['GL_ENA_MEMBER_LOGIN_LINK_TEXT']}</a><br />\n"
             . "<br />\n"
             . "Have a great day!<br />\n"
             . "</div>\n"
             . "</body>\n"
             . "</html>";
        $headers = "From: {$SETTINGS['adminEmail']}\r\n";
        $headers .= 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $mailResult = @mail($to, $subject, $message, $headers);
          if (!$mailResult) { die("Mail Error: $php_errormsg");}
        }
		//Set alert use CMSB function "notice($message)" to notify user that the email was sent
		notice("Email sent.");
	}	
}
?>