Basic Sript Help

Hi–I’m working with a free PHP contact form script I found online. I am really new to PHP and need some help: I’d like to remove the “$email” and “$subject” fields and make the “$name” field optional. Can anyone give me some pointers… I’d really appreciate it!!!

Here’s the code:

[php]

<?php if (session_id() == '') session_start(); include_once "contact-config.php"; $error_message = ''; if (!isset($_POST['submit'])) { showForm(); } else { //form submitted $error = 0; if(!empty($_POST['name'])) { $name[2] = clean_var($_POST['name']); } else { $error = 1; $name[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] = 'Invalid email'; } } 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 "contact-securimage.php"; $securimage = new Securimage(); $valid = $securimage->check($_POST['captcha_code']); if(!$valid) { $error = 1; $code[3] = 'color:#FF0000;'; $code[4] = 'Incorrect code'; } } if ($error == 1) { $error_message = 'Please correct/enter field(s) in red.'; showForm(); } else { 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); $body = "$name[0]: $name[2]\r\n\r\n"; $body .= "$email[0]: $email[2]\r\n\r\n"; $body .= "$message[0]:\r\n$message[2]\r\n"; if (!$from) $from_value = $email[2]; else $from_value = $from; $headers = "Content-type: text/plain; $charset" . "\r\n"; $headers .= "From: $from_value" . "\r\n"; $headers .= "Reply-To: $email[2]" . "\r\n"; mail($to,"$subject_prefix - $subject[2]", $body, $headers); if (!$thank_you_url) { if ($use_header_footer) include $header_file; echo ''."\n"; echo '
'."\n"; echo $GLOBALS['thank_you_message']."\n"; echo '
'."\n"; if ($use_header_footer) include $footer_file; } else { header("Location: $thank_you_url"); } } } //else submitted function showForm() { global $name, $email, $subject, $message, $code; global $where_included, $use_header_footer, $header_file, $footer_file; global $form_width, $form_height, $form_background, $form_border_color, $form_border_width, $form_border_style, $cell_padding, $left_col_width; if ($use_header_footer) include $header_file; echo $GLOBALS['error_message']; echo <<<EOD
{$name[0]}
{$email[0]} {$email[4]}
{$subject[0]}
{$message[0]} {$message[2]}
  CAPTCHA Image
{$code[0]} {$code[4]}

(Please enter the text in the image above. Text is not case sensitive.)
Click here if you cannot recognize the code.
All fields are required.
EOD; if ($use_header_footer) 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; } ?>

[/php]

The trouble with that is you wont be able to send a email without the subject and the email.

Ok, I got it sorted out so it’s going to automatically create a subject:

[php]
mail($to,"$subject_prefix - ", $body, $headers);
[/php]

Does anyone know how I could add a date(“h:i:s d-m-Y”) stamp to the subject after the dash?

yep, put what you typed in there.

[php]mail($to,"$subject_prefix - date(‘h:i:s d-m-Y’) ", $body, $headers);[/php]

When I do that, it simply outputs the text “date(‘d-m-Y h:i’)” and not the actual date.

I don’t see where $subject_prefix is coming from, but try mail($to,"$subject_prefix - ".date(‘h:i:s d-m-Y’), $body, $headers);

sorry was on the wrong side of the quote

[php]mail($to,$subject_prefix." - ".date(‘h:i:s d-m-Y’), $body, $headers);[/php]

That did it–thanks!!

One other weird thing on this script is that I can’t get it to use the full sender name I prefer:

When I insert “Larche Chicago Ideabox” into the “From:” field, it ends up outputting only “Larche”. If I add a backslash (i.e. “Larche\ Chicago\ Ideabox” it will include the “Chicago” but not the Ideabox. Very strange!

[php] $body = “$name[0]: $name[2]\r\n\r\n”;
$body .= “$message[0]:\r\n$message[2]\r\n”;

$headers = "Content-type: text/plain; $charset" . "\r\n";
$headers .= "From: Larche Chicago Ideabox" . "\r\n";
$headers .= "Reply-To: $email[2]" . "\r\n";

mail($to,$subject_prefix." - ".date('m/d/Y g:ia'), $body, $headers);

[/php]

You can’t use html inside of the headers, and where is the input coming from?

try
[php]
$headers = “Content-type: text/plain; $charset” . “\r\n”.
“From: Larche Chicago Ideabox” . “\r\n”.
“Reply-To: $email[2]” . “\r\n”;
[/php]

That still outputs only “Larche”.

It’s really odd–I can’t figure out why it will only do one word (two words if I use a backslash).

(the html in the code was just to emphasize the code for the above post, sorry!)

this is the full code–where most of the input is coming from:

[php]

<?php if (session_id() == '') session_start(); include_once "contact-config.php"; $error_message = ''; if (!isset($_POST['submit'])) { showForm(); } else { //form submitted $error = 0; if(!empty($_POST['name'])) { $name[2] = clean_var($_POST['name']); } else { $error = 0; $name[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 "contact-securimage.php"; $securimage = new Securimage(); $valid = $securimage->check($_POST['captcha_code']); if(!$valid) { $error = 1; $code[3] = 'color:#FF0000;'; $code[4] = 'Incorrect code'; } } if ($error == 1) { $error_message = 'Please correct/enter field(s) in red.'; showForm(); } else { if (function_exists('htmlspecialchars_decode')) $message[2] = htmlspecialchars_decode($message[2], ENT_QUOTES); $body = "$name[0]: $name[2]\r\n\r\n"; $body .= "$message[0]:\r\n$message[2]\r\n"; $headers = "Content-type: text/plain; $charset" . "\r\n"; $headers .= "From: Ideabox" . "\r\n"; $headers .= "Reply-To: $email[2]" . "\r\n"; mail($to,$subject_prefix." - ".date('m/d/Y g:ia'), $body, $headers); if (!$thank_you_url) { if ($use_header_footer) include $header_file; echo ''."\n"; echo '
'."\n"; echo $GLOBALS['thank_you_message']."\n"; echo '
'."\n"; if ($use_header_footer) include $footer_file; } else { header("Location: $thank_you_url"); } } } //else submitted function showForm() { global $name, $message, $code; global $where_included, $use_header_footer, $header_file, $footer_file; global $form_width, $form_height, $form_background, $form_border_color, $form_border_width, $form_border_style, $cell_padding, $left_col_width; if ($use_header_footer) include $header_file; echo $GLOBALS['error_message']; echo <<<EOD

Name (optional)


Your idea
{$message[2]}

CAPTCHA Image
Click for a new captcha

Enter the above text as it's written
{$code[4]}

EOD; if ($use_header_footer) include $footer_file; } function clean_var($variable) { $variable = strip_tags(stripslashes(trim(rtrim($variable)))); return $variable; } ?>

[/php]

Most likely because it thinks its not valid, try doing
[php]
$headers = “Content-type: text/plain; $charset\r\n”;
$headers .= “From: Larche Chicago Ideabox <$email[2]>\r\n”;
$headers .= “Reply-To: $email[2]\r\n”;
[/php]

That outputs “(unknown sender)”

Just for the heck of it, try
[php]
$from = “Larche Chicago Ideabox”;

$headers = “Content-type: text/plain; $charset\r\n”;
$headers .= “From: “.$from.”\r\n”;
$headers .= “Reply-To: $email[2]\r\n”;[/php]

I tried that already :slight_smile:

It only outputs the first word “Larche”

Are you passing the headers though a substr function or something? i’ve never seen anything that’ll do that unintentionally. Just for the heck of it, try
[php]$headers = “Content-type: text/plain; $charset\r\n”;
$headers .= “From: [email protected]\r\n”;
$headers .= “Reply-To: $email[2]\r\n”;[/php]

Yes, that outputs correctly–if it’s one word, it seems to be fine

Sponsor our Newsletter | Privacy Policy | Terms of Service