Hi guys,
I’m new to PHP and new to this forum as well. I would like to know if anyone can help me out with a bit of an issue here. I’m designing a website for a church which uses a PHP form I downloaded which contains captcha. I want to use this form because it is simple and I have implemented it before.
The problem I’m experiencing is that I can only email 1 recipient or multiple at the same time. Since this is a chuch, I want to email individuals based on the user’s input. I have added a variable called $recipient, but it’s not working. If I specify an email address in the $To, then it works. I need to be able to have the user select who they want the form to go to, like a department as an example. Any help is much appreciated.
thanks,
Ronaldo
<!-- Start Contact Form -->
<?php
/* session_start(); */
/*******************************************************************************
* Title: Easy PHP Contact Form
* Version: 1.3 @ October 23, 2009
* Author: Vishal P. Rao
* Website: http://www.easyphpcontactform.com
********************************************************************************
* COPYRIGHT NOTICE
* Copyright 2009 Vishal P. Rao. All Rights Reserved.
*
* This script may be used and modified free of charge by anyone
* AS LONG AS COPYRIGHT NOTICES AND ALL THE COMMENTS REMAIN INTACT.
* By using this code you agree to indemnify Vishal P. Rao or
* www.easyphpcontactform.com from any liability that might arise from
* it's use.
*
* Selling the code for this program, in part or full, without prior
* written consent is expressly forbidden.
*
* Obtain permission before redistributing this software over the Internet
* or in any other medium. In all cases copyright and header must remain
* intact. This Copyright is in full effect in any country that has
* International Trade Agreements with the India
*
* Removing any of the copyright notices without purchasing a license
* is illegal!
*******************************************************************************/
/*******************************************************************************
* Script configuration - Refer README.txt
*******************************************************************************/
/* Email address where the messages should be delivered */
$to = '$recipient'; //'[email protected]'; // separate emails by comma
/* From email address, in case your server prohibits sending emails from addresses other than those of your
own domain (e.g. [email protected]). If this is used then all email messages from your contact form will appear
from this address instead of actual sender. */
$from = '';
/* This will be appended to the subject of contact form message */
$subject_prefix = 'IVN Online ';
/* Form header file */
$header_file = 'form-header.php';
/* Form footer file */
$footer_file = 'form-footer.php';
/* Form width in px or % value */
$form_width = '100%';
/* Form background color
$form_background = '#F7F8F7';*/
/* Form border color */
$form_border_color = '#CCCCCC';
/* Form border width */
$form_border_width = '1px';
/* Form border style. Examples - dotted, dashed, solid, double */
$form_border_style = 'solid';
/* Form cell padding */
$cell_padding = '5px';
/* Form left column width */
$left_col_width = '25%';
/* Form font size */
$font_size = '';
/* Empty/Invalid fields will be highlighted in this color */
$field_error_color = '#FF0000';
/* Thank you message to be displayed after the form is submitted. Can include HTML tags. Write your message
between <!-- Start message --> and <!-- End message --> */
$thank_you_message = <<<EOD
<!-- Start message -->
<p>{$confirmation}</p><br /><br /><br /><br /><br /><br /><br /><br />
<!-- End message -->
EOD;
/* URL to be redirected to after the form is submitted. If this is specified, then the above message will
not be shown and user will be redirected to this page after the form is submitted */
/* Example: $thank_you_url = 'http://www.yourwebsite.com/thank_you.html'; */
$thank_you_url = 'thankyou.php';
/*******************************************************************************
* Do not change anything below, unless of course you know very well
* what you are doing :)
*******************************************************************************/
$name = array($lang[CONTATO_NAME],'name',NULL,NULL);
//$phone = array('Phone','phone',NULL,NULL);
$phone = array($lang[CONTATO_PHONE],'phone',NULL,NULL);
$email = array($lang[CONTATO_EMAIL],'email',NULL,NULL,NULL);
$recipient = array('recipient','recipient',NULL,NULL,NULL);
$subject = array($lang[CONTATO_SUBJECT],'subject',NULL,NULL);
$message = array($lang[CONTATO_MESSAGE],'message',NULL,NULL);
$code = array($lang[CONTATO_CODE],'captcha_code',NULL,NULL,NULL);
$required = array($lang[CONTATO_REQUIRED_FIELDS]);
$captcha1 = array($lang[CONTATO_CAPTCHA1]);
$captcha2 = array($lang[CONTATO_CAPTCHA2]);
$error_message = '';
if (!isset($_POST['submit'])) {
showForm();
} else { //form submitted
$error = 0;
if(!empty($_POST['name'])) {
$name[2] = clean_var($_POST['name']);
if (function_exists('htmlspecialchars')) $name[2] = htmlspecialchars($name[2], ENT_QUOTES);
}
else {
$error = 1;
$name[3] = 'color:#FF0000;';
}
if(!empty($_POST['phone'])) {
$phone[2] = clean_var($_POST['phone']);
if (function_exists('htmlspecialchars')) $phone[2] = htmlspecialchars($phone[2], ENT_QUOTES);
}
else {
$error = 1;
$phone[3] = 'color:#FF0000;';
}
if(!empty($_POST['email'])) {
$email[2] = clean_var($_POST['email']);
if (!validEmail($email[2])) {
$error = 1;
$email[3] = 'color:#FF0000;';
$email[4] = '<strong><span style="color:#FF0000;">Invalid email</span></strong>';
}
}
else {
$error = 1;
$email[3] = 'color:#FF0000;';
}
if(!empty($_POST['subject'])) {
$subject[2] = clean_var($_POST['subject']);
if (function_exists('htmlspecialchars')) $subject[2] = htmlspecialchars($subject[2], ENT_QUOTES);
}
else {
$error = 1;
$subject[3] = 'color:#FF0000;';
}
if(!empty($_POST['message'])) {
$message[2] = clean_var($_POST['message']);
if (function_exists('htmlspecialchars')) $message[2] = htmlspecialchars($message[2], ENT_QUOTES);
}
else {
$error = 1;
$message[3] = 'color:#FF0000;';
}
if(empty($_POST['captcha_code'])) {
$error = 1;
$code[3] = 'color:#FF0000;';
} else {
include_once "securimage.php";
$securimage = new Securimage();
$valid = $securimage->check($_POST['captcha_code']);
if(!$valid) {
$error = 1;
$code[3] = 'color:#FF0000;';
$code[4] = '<strong><span style="color:#FF0000;">Incorrect code</span></strong>';
}
}
if ($error == 1) {
$error_message = '<span style="font-weight:bold;font-size:90%;">Please correct/enter field(s) in red.</span>';
showForm();
} else {
if (function_exists('htmlspecialchars_decode')) $name[2] = htmlspecialchars_decode($name[2], ENT_QUOTES);
if (function_exists('htmlspecialchars_decode')) $phone[2] = htmlspecialchars_decode($phone[2], ENT_QUOTES);
if (function_exists('htmlspecialchars_decode')) $subject[2] = htmlspecialchars_decode($subject[2], ENT_QUOTES);
if (function_exists('htmlspecialchars_decode')) $message[2] = htmlspecialchars_decode($message[2], ENT_QUOTES);
$message = "$name[0]: $name[2]\r\n$phone[0]: $phone[2]\r\n$email[0]: $email[2]\r\n\r\n$message[0]:\r\n$message[2]\r\n";
if (!$from) $from_value = $email[2];
else $from_value = $from;
$headers = "From: $from_value" . "\r\n" . "Reply-To: $email[2]";
mail($to,"$subject_prefix - $subject[2]", $message, $headers);
if (!$thank_you_url) {
include $header_file;
echo $GLOBALS['thank_you_message'];
echo "\n";
include $footer_file;
}
else {
header("Location: $thank_you_url");
}
}
} //else submitted
function showForm()
{
global $name, $phone, $email, $subject, $message, $recipient, $required, $captcha1, $captcha2, $code, $header_file, $footer_file, $form_width, $form_background, $form_border_color, $form_border_width, $form_border_style, $cell_padding, $left_col_width, $font_size;
include $header_file;
echo $GLOBALS['error_message'];
echo <<<EOD
<form method="post" class="cForm">
<table style="width:{$form_width}; background-color:{$form_background}; border:{$form_border_width} {$form_border_style} {$form_border_color}; padding:10px; font-size:{$font_size};" class="contactForm">
<tr>
<td>
<input type="radio" name="recipient" value="[email protected]">Hotmail</input>
<input type="radio" name="recipient" value="[email protected]">Yahoo</input>
</td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$name[3]}">{$name[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$name[1]}" value="{$name[2]}" /></td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$phone[3]}">{$phone[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$phone[1]}" value="{$phone[2]}" /></td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$email[3]}">{$email[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$email[1]}" value="{$email[2]}" /> {$email[4]}</td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$subject[3]}">{$subject[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$subject[1]}" value="{$subject[2]}" size="60" /></td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$message[3]}">{$message[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><textarea name="{$message[1]}" cols="60" rows="6">{$message[2]}</textarea></td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding};"> </td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><img id="captcha" src="securimage_show.php" alt="CAPTCHA Image" /></td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$code[3]}">{$code[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$code[1]}" size="10" maxlength="5" /> {$code[4]}
<br /><br />{$captcha1[0]}<br />
<a href="#" onclick="document.getElementById('captcha').src = 'securimage_show.php?' + Math.random(); return false">{$captcha2[0]}</a>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:left; vertical-align:middle; padding:{$cell_padding}; font-size:90%; font-weight:bold;">{$required[0]}</td>
</tr>
<tr>
<td colspan="2" style="text-align:left; vertical-align:middle; padding:{$cell_padding};"><input type="submit" name="submit" value="Submit" style="border:1px solid #999;background:#E4E4E4;margin-top:5px;" /></td>
</tr>
</table>
</form>
<div style="{$form_width};text-align:right;font-size:80%;">
</div>
EOD;
include $footer_file;
}
function clean_var($variable) {
$variable = strip_tags(stripslashes(trim(rtrim($variable))));
return $variable;
}
/**
Email validation function. Thanks to http://www.linuxjournal.com/article/9585
*/
function validEmail($email)
{
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.')
{
// local part starts or ends with '.'
$isValid = false;
}
else if (preg_match('/\\.\\./', $local))
{
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
{
// character not valid in domain part
$isValid = false;
}
else if (preg_match('/\\.\\./', $domain))
{
// domain part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/',
str_replace("\\\\","",$local)))
{
$isValid = false;
}
}
if ($isValid && function_exists('checkdnsrr'))
{
if (!(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) {
// domain not found in DNS
$isValid = false;
}
}
}
return $isValid;
}
?>
<!-- End contact form -->