SELECTING A CONTACT FORM'S EMAIL RECIPIENTS USING FIELDS IN THEIR ACCOUNT RECORD (AN ALL CMSB IMPLEMENTATION) - Jun 28th, 2015


The initial purpose of this recipe is to be able to control an organization’s board of director positions, committee
memberships and easily designate who the chairpersons of committees are and who is to receive email for the various
positions and committees.

This implementation (thanks to a lot of help from Ross Fairbairn from Interactive Tools) uses the built in mail
functions in CMSB and their associated Email Templates, and a single viewer to accomplish the task.

To make the board of director positions and committees flexible, I created a multi record editor called “Board Of
Director Positions”. The records in this section have one text field called “Position” and a dragSortOrder field.
NOTE: The records in “Board Of Director Positions” are where the options in the form below come from.

The organization wanted to allow a member to hold more than one position on the board of directors and/or to sit on up
to 3 committees.

So, in the “User Accounts” section there are 3 sets of 3 Board Of Director fields.

A list field for position (1, 2 and 3), a check box for the chairperson of each, and a check box for the designated
email recipient for each.

The Board of Director Position pull down lists fields (board_of_director_position_1, board_of_director_position_2, and
board_of_director_position_3) get their options from the database “board_of_director_positions” with Option Values
from the “num” field, and Option Labels from the Positions Field.

The check box fields (board_of_director_position_1_chair, board_of_director_position_2_chair,
board_of_director_position_3_chair), signify that this person is the chairperson.

The check box fields (board_of_director_position_1_email, board_of_director_position_2_email,
board_of_director_position_3_email) designate the person who gets email for the board position or committee.

This implementation uses 2 Email Templates, one for the message that’s sent to the recipient
(BOARD-OF-DIRECTOR-CONTACT), and one for a confirmation message to the sender (BOARD-OF-DIRECTOR-CONTACT-CONFIRMATION)

Each of these has placeholder variables set up for contact.firstName, contact.lastName, contact.email, contact.message,
board.position, and bod.email.

Here’s the basic setup for the Templates.

BOARD-OF-DIRECTOR-CONTACT

From: your_from_email@your_site.com
Reply To: #contact.email#
To: #bod.email#

Subject: A Message For The #board.position#

Message HTML:

Hello #board.position#,

You've received a message from #contact.firstName# #contact.lastName#

Here's what it said:

#contact.message#

You can contact them via: #contact.email#

Or just reply to this email

Thanks,

This message was sent from IP Address: #server.remote_addr#


BOARD-OF-DIRECTOR-CONTACT-CONFIRMATION

From: your_from_email@your_site.com
Reply To: your_reply_email@your_site.com
To: #contact.email#

Subject: A Copy Of Your Message To The #board.position#

Message HTML:

Hello #contact.firstName# #contact.lastName#,

Thanks for caring enough to contact the #board.position#.

Here's a copy of your message:

#contact.message#

Your message has been received and you should get a response shortly

Best,


***DON'T FORGET TO CHANGE ALL REFERENCES TO GENERIC PATHS TO MATCH YOUR OWN***
*** IF YOU USE NO-REPLY@YOUR_DOMAIN.COM AS THE RETURN EMAIL ADDRESS, AND DON'T SET UP A REAL EMAIL ACCOUNT FOR THAT
ADDRESS AT YOUR DOMAIN, SOME PROVIDERS MAY BLOCK YOUR EMAILS***

You can set the email properties in Admin>General to log only for testing and debugging and then set to either send only
or send and log after the form is working the way you want it to.

Here’s the code for the viewer (without CAPTCHA) The Code with Google’s New “I’M NOT A ROBOT”, NO CAPTCHA,
RECAPTCHA is below, You’ll need to format it to match the look of your site.


<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/path_to_your_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."); }
?>
<?php
// load records
list($board_of_director_positionsRecords, $board_of_director_positionsMetaData) = getRecords(array(
'tableName' => 'board_of_director_positions',
));

?>
<?php $errorsAndAlerts = "";

// form submit
if (@$_REQUEST['formSubmit']) {

// error checking
if (!@$_REQUEST['reason']) { $errorsAndAlerts .= "You must select someone to contact\n"; }
if (!@
$_REQUEST['first_name']) { $errorsAndAlerts .= "You must enter a first name\n"; }
if (!@
$_REQUEST['last_name']) { $errorsAndAlerts .= "You must enter a last name\n"; }

if (!@
$_REQUEST['email']) { $errorsAndAlerts .= "You must enter your email!\n"; }
else if(!
isValidEmail(@$_REQUEST['email'])) { $errorsAndAlerts .= "Please enter a valid email (example:
user@example.com)\n"; }
else if (@
$_REQUEST['email'] != @$_REQUEST['email2']) { $errorsAndAlerts .= "Your emails must match\n"; }

if (!@
$_REQUEST['message']) { $errorsAndAlerts .= "You must enter a message\n"; }

// send emails
if (!$errorsAndAlerts) {

// look up email address (3 fields for board_of_director_position)

$customWhere = "(board_of_director_position_1 = '". mysql_escape($_REQUEST['reason']) ."' AND
board_of_director_position_1_email = '1') OR (board_of_director_position_2 = '". mysql_escape($_REQUEST['reason']) ."'
AND board_of_director_position_2_email = '1') OR (board_of_director_position_3 = '". mysql_escape($_REQUEST['reason'])
.
"' AND board_of_director_position_3_email = '1')";

$accountRecords = mysql_select("accounts", $customWhere );

$bodEmail = "";
foreach (
$accountRecords as $accountRecord) {
$bodEmail .= @$accountRecord['email'] .", ";
}

$bodEmail = substr($bodEmail, 0, -2);

// look up bod position
$bodPositionRecord = mysql_get("board_of_director_positions", mysql_escape($_REQUEST['reason']));
$bodPosition = $bodPositionRecord['position'];

// no email supplied for this account
if (!$bodEmail || $bodEmail == "") {
$commonInformationRecord = mysql_get("common_information", 1);
$bodEmail = $commonInformationRecord['board_backup_email'];
}

// send email to BOD member
$emailHeaders = emailTemplate_loadFromDB(array(
'template_id' => 'BOARD-OF-DIRECTOR-CONTACT',
'placeholders' => array(
'contact.firstName' => $_REQUEST['first_name'],
'contact.lastName' => $_REQUEST['last_name'],
'contact.email' => $_REQUEST['email'],
'contact.message' => $_REQUEST['message'],
'board.position' => $bodPosition,
'bod.email' => $bodEmail,

)));
$mailErrors = sendMessage($emailHeaders);
if (
$mailErrors) { alert("Mail Error: $mailErrors"); }

// send confirmation email to sender
$emailHeaders = emailTemplate_loadFromDB(array(
'template_id' => 'BOARD-OF-DIRECTOR-CONTACT-CONFIRMATION',
'placeholders' => array(
'contact.firstName' => $_REQUEST['first_name'],
'contact.lastName' => $_REQUEST['last_name'],
'contact.email' => $_REQUEST['email'],
'contact.message' => $_REQUEST['message'],
'board.position' => $bodPosition,
'bod.email' => $bodEmail,

)));
$mailErrors = sendMessage($emailHeaders);
if (
$mailErrors) { alert("Mail Error: $mailErrors"); }

if (!
$mailErrors) {
$_REQUEST = array();
$errorsAndAlerts = "
<div class='heading_font' align='center'>THANKS FOR CONTACTING US

</div>
<div align='center'>

<div class='text_font' align='left'><b>Your Message has been sent successfuly.

Thanks for caring enough to contact us.

We'll get back to you shortly.

Best,

The Organization</b></div>
</div>
</div>";
}

}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome To Our Contact Form</title>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<meta name="viewport" content="width=device-width, target-densitydpi=device-dpi, initial-scale=1">
</head>

<body >
<table align="center" width="100%" border="0" cellspacing="0" cellpadding="20">
<tr>
<td valign="top" align="left" ><div align="left">

<div align="center"><b>CONTACT OUR BOARD OF DIRECTORS
AND COMMITTEE CHAIRS</b>

</div>

<div align="left">
<?php if (@$errorsAndAlerts == ""): ?>
<<b>USE THIS FORM TO SEND A MESSAGE DIRECTLY TO THE PERSON WHO CAN ANSWER YOUR QUESTION:</b>
<?php endif ?>


<?php if (@$errorsAndAlerts): ?>
<div style="color: #FF0000; font-weight: bold; font-size: 16px; font-family: arial;">
<?php echo $errorsAndAlerts; ?>

</div>
<?php endif ?>
<?php if (@$errorsAndAlerts == ""): ?>
<form method="post" action="?">
<input type="hidden" name="formSubmit" value="1">
<table width="95%" border="0" cellpadding="15">
<tr >
<td width="40%" align="left" valign="middle"><label><b>Who would you
like to contact?</b></label></td>
<td style="text-align:left" width="60%" align="left" valign="middle"><?php $reason =
htmlspecialchars(@$_REQUEST['reason']); ?>
<select name="reason">
<option value="" >...Select...</option>
<?php foreach($board_of_director_positionsRecords as $bod): ?>
<option value="<?php echo $bod['num'];?>" <?php selectedIf($reason,$bod['num']) ?> ><?php echo
$bod['position'];?></option>
<?php endforeach?>
</select></td>
</tr>
<tr>
<td width="40%" align="left" valign="middle" ><b>What's your
First Name</b></td>
<td style="text-align:left" align="left" valign="middle"><input class="text" type="text" name="first_name"
id="first_name" value="<?php echo @$_REQUEST['first_name']; ?>" /></td>
</tr>
<tr>
<td width="40%" align="left" valign="middle" ><b>Your Last Name</b></td>
<td style="text-align:left" align="left" valign="middle"><input class="text" type="text" name="last_name"
id="last_name" value="<?php echo @$_REQUEST['last_name']; ?>" /></td>
</tr>
<tr>
<td width="40%" align="left" valign="middle" ><b>Your e-mail</b></td>
<td style="text-align:left" align="left" valign="middle"><input class="text" type="text" name="email"
id="email" value="<?php echo @$_REQUEST['email']; ?>" /></td>
</tr>
<tr>
<td width="40%" align="left" valign="middle"><b>Re-enter your e-mail</b></td>
<td style="text-align:left" align="left" valign="middle"><input class="text" type="text" name="email2"
id="email2" value="<?php echo @$_REQUEST['email2']; ?>" /></td>
</tr>
<tr>
<td width="460%" align="left" valign="middle"><b>What's your message</b></td>
<td style="text-align:left" align="left" valign="middle"><textarea class="textarea" name="message"
cols="60" rows="6" id="message"><?php echo @$_REQUEST['message']; ?></textarea></td>
</tr>
<tr>
<td colspan="2" align="left" valign="middle"><input type="submit" name="form_submitted" value="Submit Your
Message" /></td>
</tr>
</table>
</form>
<?php endif ?>
</div></td>
</tr>
</table>
</body>
</html>


WITH GOOGLE'S “I’M NOT A ROBOT”, NO CAPTCHA, RECAPTCHA


<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/path_to_your_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."); }
?>
<?php
// load records
list($board_of_director_positionsRecords, $board_of_director_positionsMetaData) = getRecords(array(
'tableName' => 'board_of_director_positions',
));

?>
<?php
function validateGoogleCaptcha(){

$errorsAndAlerts = "";

if (!@
$_REQUEST['g-recaptcha-response']) { $errorsAndAlerts .= "Please check the anti-spam 'I am not a robot'
checkbox!\n";
$showSignupForm = true; // don't change this value
}
else {
// check recaptcha
$postdata = array();
$postdata['secret'] = 'YOUR SECRET GOOGLE CAPTCHA KEY GOES HERE';
$postdata['response'] = @$_REQUEST['g-recaptcha-response'];
$postdata['remoteip'] = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?". http_build_query($postdata, '', '&');
list(
$json, $httpStatusCode, $headers, $request) = getPage($url, 5, '', true);
$recaptchaResponse = json_decode($json, true);

if (!
$recaptchaResponse['success']) {
if (
is_array($recaptchaResponse['error-codes'])) {
if (
in_array('missing-input-secret', $recaptchaResponse['error-codes'])) { $errorsAndAlerts .= "There's a
problem with recaptcha, please let us know! (no secret)\n"; }
if (
in_array('invalid-input-secret', $recaptchaResponse['error-codes'])) { $errorsAndAlerts .= "There's a
problem with recaptcha, please let us know! (invald secret)\n"; }
if (
in_array('missing-input-response', $recaptchaResponse['error-codes'])) { $errorsAndAlerts .= "Please
fill out the recaptcha box!\n";
$showSignupForm = true; // do we need this line?
}
if (
in_array('invalid-input-response', $recaptchaResponse['error-codes'])) { $errorsAndAlerts .= "Please
fill out the recaptcha box again, your answer was incorrect!\n";
$showSignupForm = true; // do we need this line?
}
}
if (!
$errorsAndAlerts) { $errorsAndAlerts .= "Invalid captcha response, please try again or contact us directly
and let us know."; }
@
trigger_error("Failed recaptcha on signup form", E_USER_NOTICE);
}
}
return
$errorsAndAlerts;
}
?>
<?php $errorsAndAlerts = "";
$showSignupForm = true;
// form submit
if (@$_REQUEST['formSubmit']) {
$errorsAndAlerts .= validateGoogleCaptcha();
// error checking
if (!@$_REQUEST['reason']) { $errorsAndAlerts .= "You must select someone to contact\n"; }
if (!@
$_REQUEST['first_name']) { $errorsAndAlerts .= "You must enter a first name\n"; }
if (!@
$_REQUEST['last_name']) { $errorsAndAlerts .= "You must enter a last name\n"; }

if (!@
$_REQUEST['email']) { $errorsAndAlerts .= "You must enter your email!\n"; }
else if(!
isValidEmail(@$_REQUEST['email'])) { $errorsAndAlerts .= "Please enter a valid email (example:
user@example.com)\n"; }
else if (@
$_REQUEST['email'] != @$_REQUEST['email2']) { $errorsAndAlerts .= "Your emails must match\n"; }

if (!@
$_REQUEST['message']) { $errorsAndAlerts .= "You must enter a message\n"; }

// send emails
if (!$errorsAndAlerts) {
$showSignupForm = false;

// look up email address (3 fields for board_of_director_position)

$customWhere = "(board_of_director_position_1 = '". mysql_escape($_REQUEST['reason']) ."' AND
board_of_director_position_1_email = '1') OR (board_of_director_position_2 = '". mysql_escape($_REQUEST['reason']) ."'
AND board_of_director_position_2_email = '1') OR (board_of_director_position_3 = '". mysql_escape($_REQUEST['reason'])
.
"' AND board_of_director_position_3_email = '1')";

$accountRecords = mysql_select("accounts", $customWhere );

$bodEmail = "";
foreach (
$accountRecords as $accountRecord) {
$bodEmail .= @$accountRecord['email'] .", ";
}

$bodEmail = substr($bodEmail, 0, -2);

// look up bod position
$bodPositionRecord = mysql_get("board_of_director_positions", mysql_escape($_REQUEST['reason']));
$bodPosition = $bodPositionRecord['position'];

// no email supplied for this account
if (!$bodEmail || $bodEmail == "") {
$commonInformationRecord = mysql_get("common_information", 1);
$bodEmail = $commonInformationRecord['board_backup_email'];
}

// send email to BOD member
$emailHeaders = emailTemplate_loadFromDB(array(
'template_id' => 'BOARD-OF-DIRECTOR-CONTACT',
'placeholders' => array(
'contact.firstName' => $_REQUEST['first_name'],
'contact.lastName' => $_REQUEST['last_name'],
'contact.email' => $_REQUEST['email'],
'contact.message' => $_REQUEST['message'],
'board.position' => $bodPosition,
'bod.email' => $bodEmail,

)));
$mailErrors = sendMessage($emailHeaders);
if (
$mailErrors) { alert("Mail Error: $mailErrors"); }

// send confirmation email to sender
$emailHeaders = emailTemplate_loadFromDB(array(
'template_id' => 'BOARD-OF-DIRECTOR-CONTACT-CONFIRMATION',
'placeholders' => array(
'contact.firstName' => $_REQUEST['first_name'],
'contact.lastName' => $_REQUEST['last_name'],
'contact.email' => $_REQUEST['email'],
'contact.message' => $_REQUEST['message'],
'board.position' => $bodPosition,
'bod.email' => $bodEmail,

)));
$mailErrors = sendMessage($emailHeaders);
if (
$mailErrors) { alert("Mail Error: $mailErrors"); }

if (!
$mailErrors) {
$_REQUEST = array();
$errorsAndAlerts = "
<div align='center'>THANKS FOR CONTACTING US

</div>
<div align='center'>

<div align='left'><b>Your Message has been sent successfuly.

Thanks for caring enough to contact us.

We'll get back to you shortly.

Best,

The Organization</b></div>
</div>
</div>";
}

}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome to<?php echo $organization_informationRecord['organization_name'] ?></title>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<meta name="viewport" content="width=device-width, target-densitydpi=device-dpi, initial-scale=1">
</head>
<body >
<table align="center" width="100%" border="0" cellspacing="0" cellpadding="20">
<tr>
<td valign="top" align="left" ><div align="left">

<div align="center"><b>CONTACT OUR BOARD OF DIRECTORS
AND COMMITTEE CHAIRS</b>

</div>

<div align="left">
<?php if ($showSignupForm): ?>
<b>USE THIS FORM TO SEND A MESSAGE DIRECTLY TO THE PERSON WHO CAN ANSWER YOUR QUESTION:</b>
<?php endif ?>


<?php if (@$errorsAndAlerts ): ?>
<div style="color: #FF0000; font-weight: bold; font-size: 16px; font-family: arial;">
<?php echo $errorsAndAlerts; ?>

</div>
<?php endif ?>
<?php if ($showSignupForm == "true"): ?>
<form method="post" action="?">
<input type="hidden" name="formSubmit" value="1">
<table width="95%" border="0" cellpadding="15">
<tr >
<td width="40%" align="left" valign="middle"><label ><b>Who would you
like to contact?</b></label></td>
<td style="text-align:left" width="60%" align="left" valign="middle"><?php $reason =
htmlspecialchars(@$_REQUEST['reason']); ?>
<select name="reason">
<option value="" >...Select...</option>
<?php foreach($board_of_director_positionsRecords as $bod): ?>
<option value="<?php echo $bod['num'];?>" <?php selectedIf($reason,$bod['num']) ?> ><?php echo
$bod['position'];?></option>
<?php endforeach?>
</select></td>
</tr>
<tr>
<td width="40%" align="left" valign="middle" ><b>What's your
First Name</b></td>
<td style="text-align:left" align="left" valign="middle"><input class="text" type="text" name="first_name"
id="first_name" value="<?php echo @$_REQUEST['first_name']; ?>" /></td>
</tr>
<tr>
<td width="40%" align="left" valign="middle" ><b>Your Last Name</b></td>
<td style="text-align:left" align="left" valign="middle"><input class="text" type="text" name="last_name"
id="last_name" value="<?php echo @$_REQUEST['last_name']; ?>" /></td>
</tr>
<tr>
<td width="40%" align="left" valign="middle"><b>Your e-mail</b></td>
<td style="text-align:left" align="left" valign="middle"><input class="text" type="text" name="email"
id="email" value="<?php echo @$_REQUEST['email']; ?>" /></td>
</tr>
<tr>
<td width="40%" align="left" valign="middle" ><b>Re-enter your e-mail</b></td>
<td style="text-align:left" align="left" valign="middle"><input class="text" type="text" name="email2"
id="email2" value="<?php echo @$_REQUEST['email2']; ?>" /></td>
</tr>
<tr>
<td width="460%" align="left" valign="middle" ><b>What's your message</b></td>
<td style="text-align:left" align="left" valign="middle"><textarea class="textarea" name="message"
cols="60" rows="6" id="message"><?php echo @$_REQUEST['message']; ?></textarea></td>
</tr>
<tr>
<td colspan="2" style=" font-weight: bold;" valign="top">Please check the "I'm not a robot" box below
before submitting.

<div class="g-recaptcha" data-theme="light" data-sitekey="YOUR GOOGLE CAPTCHA SITE KEY GOES
HERE"></div></td>
</tr>
<tr>
<td colspan="2" align="left" valign="middle"><input type="submit" name="form_submitted" value="Submit Your
Message" /></td>
</tr>
</table>
</form>
<?php endif ?>
</div></td>
</tr>
</table>
</body>
</html>



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service