After I'd implemented the Board of Directors contact form (See "Selecting A Form's Email Recipients Using Fields In Their Account Record (A CMSB implementation)" my cient wanted to use the same approach to send emails from the contact page based on the question that the sender wanted to ask.
I created a multi-record section called "Contact Page Questions" that had 2 text fields called "Question" and "Response"
In an existing single record section called "Common Information", I added 2 text fields called "Contact Page Backup Email" and "Contact Page Generic Reply"
You'll also need to create 2 new Email Templates. CONTACT-PAGE-QUESTION-CONTACT and CONTACT-PAGE-QUESTION-CONFIRMATION
The CONTACT-PAGE-QUESTION-CONTACT template is set up like this:
From: your_from_email@your_site.com (use a real email address, I set up no-reply@my_domain.com) Reply-To: #contact.email# To: #bod.email# Subject: A Message Regarding: "#board.position#" Message:
Hello,
You've received a message from #contact.firstName# #contact.lastName#, regarding: "#board.position#".
Here's what it said:
#contact.message#
You can contact them via: #contact.email#
Or just reply to this email.
Thanks,
The Help Desk
This message was sent from IP Address: #server.remote_addr#
The CONTACT-PAGE-QUESTION-CONFIRMATION template is set up like this:
From: your_from_email@your_site.com (use a real email address, I set up no-reply@my_domain.com) Reply To: your_reply_email@your_site.com (use a real email address, I used no-reply@my_domain.com here too) To: #contact.email# Subject: A Message Regarding: "#board.position#" Message:
Hello #contact.firstName# #contact.lastName#,
Thanks for caring enough to contact us regarding: "#board.position#".
Here's a copy of your message:
#contact.message#
Your message has been received and you should get a response shortly
Best,
The Help Team
Here's the code that I came up with for the viewer (you'll have to format it to match your site design):
<?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 // With multiple email addresses and 3 Question Fields, form hidden after submit, Captcha, custom replies?> <?php // load records list($contact_page_questionsRecords, $contact_page_questionsMetaData) = getRecords(array( 'tableName' => 'contact_page_questions', ));
?> <?php $errorsAndAlerts = ""; ?> <?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 a question for your 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 contact_page_question_email_assignment) $customWhere2 = "(contact_page_question_email_assignment_1 = '". mysql_escape($_REQUEST['reason']) ."') OR (contact_page_question_email_assignment_2 = '". mysql_escape($_REQUEST['reason']) ."') OR (contact_page_question_email_assignment_3 = '". mysql_escape($_REQUEST['reason']) ."')"; $accountRecords = mysql_select("accounts", $customWhere2 ); $questionEmail = ""; foreach ($accountRecords as $accountRecord) { $questionEmail .= @$accountRecord['email'] .", "; } $questionEmail = substr($questionEmail, 0, -2); // no email supplied for this account if (!$questionEmail || $questionEmail == "") { $commonInformationRecord = mysql_get("common_information", 1); $questionEmail = $commonInformationRecord['contact_page_backup_email']; } // look up contact Page Questions $emailQuestionRecord = mysql_get("contact_page_questions", mysql_escape($_REQUEST['reason'])); $bodPosition = $emailQuestionRecord['question']; //Look Up Custom Reply $customError =""; foreach ($contact_page_questionsRecords as $record) { if (@$record['question'] == $bodPosition) { @$customError = $record['response']; } } // no custom reply if (!$customError || $customError == "") { $commonInformationRecord = mysql_get("common_information", 1); $customError = $commonInformationRecord['contact_page_generic_reply']; } // send email to BOD member $emailHeaders = emailTemplate_loadFromDB(array( 'template_id' => 'CONTACT-PAGE-QUESTION-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' => $questionEmail, ))); $mailErrors = sendMessage($emailHeaders); if ($mailErrors) { alert("Mail Error: $mailErrors"); } // send confirmation email to sender $emailHeaders = emailTemplate_loadFromDB(array( 'template_id' => 'CONTACT-PAGE-QUESTION-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' => $questionEmail, ))); $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 successfully. Thanks for caring enough to contact us. $customError Best, The N.A.W.A.Florida Chapter</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" /> <script src="https://www.google.com/recaptcha/api.js" async defer></script> <title>Welcome</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 US...</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 ($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>Tell us your reason for contacting us.</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($contact_page_questionsRecords as $question): ?> <option value="<?php echo $question['num'];?>" <?php selectedIf($reason,$question['num']) ?> ><?php echo $question['question'];?></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>
|