...

View Full Version : Why Wont my Form Script Send?



srule_
07-23-2007, 10:15 PM
Hey, i'm taking the leap into php and with my trusty book "php solotions" in hand i have attempted to create a script to send any form data submited to my e-mail.

The problem: Everything appears to work fine, However even though i get the msg "you e-mail has been sent" apon completion of the form, i never recive the e-mail!

The site: http://template1.nrtdesigns.com/contact.php

I know my host supports the function because my old script that i simple copy/pasted worked fine.

The code is as follows:

PHP Above the DOCTYPE


<?php

// process the email
if (array_key_exists('send', $_POST)) {
$to = 'info@nrtdesigns.com';
$subject = 'Feedback from Japan Journey site';

// list expected fields
$expected = array('name', 'email', 'comments');
// set required fields
$required = array('name', 'email', 'comments');
// create empty array for any missing fields
$missing = array();

//assume that there is nothing suspect
$suspect = false;
//create a pattern to locate suspect phrases
$pattern = '/Content-Type:|Bcc:|Cc:/i';

// function to check for suspct phrases
function isSuspect($val, $pattern, &$suspect) {
//if the catiable is an array, loop through each element
//and pass it recrively back to the same function
if (is_array($val)) {
foreach ($val as $item) {
isSuspect($item, $pattern, $suspect);
}
}
else {
// if one of the suspect phrases is found, set boolean to true
if (preg_match($pattern, $val)) {
$suspect = true;
}
}
}
// check the $_POST array and any subarrays for suspect content
isSuspect($_POST, $pattern, $suspect);

if ($suspect) {
$mailSent = false;
unset($missing);
}
else {
// process the $_POST variables
foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
// if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
array_push($missing, $key);
}

// Otherwise, assign to a variable of the same name as $key
elseif (in_array($key, $expected)) {
${$key} = $temp;
}
}
}

// process the $_POST variables
foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
// if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
array_push($missing, $key);
}
// otherwise, assign to a variable of the same name as $key
elseif (in_array($key, $expected)) {
${$key} = $temp;
}
}

// validate the email address
if (!empty($email)) {
// regex to ensure no illegal characters in email address
$checkEmail = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
// reject the email address if it doesn't match
if (!preg_match($checkEmail, $email)) {
array_push($missing, 'email');
}
}

// go ahead only if not suspect and all required fields OK
if (!$suspect && empty($missing)) {
// build the message
$message = "Name: $name\n\n";
$message .= "Email: $email\n\n";
$message .= "Comments: $comments";

// limit line length to 70 characters
$message = wordwrap($message, 70);

// create additional header
$additionalHeaders = 'From: NRT Designs<info@nrtdesigns.com>';
if (!empty($email)) {
$additionalHeader .= "\r\nReply-To: $email";
}

// send it
$mailSent = mail($to, $subject, $message, $additionalHeaders);
if ($mailSent) {
// $missing is no longer needed if the email is sent, so unset it
unset($missing);
}
}
}
?>


PHP My HTML Documents


<h1 class="background"> Contact Us </h1>
<h3 class="top"> Questions/Comments?</h3>
<p class="top"> Thanks for checking out Web Design Central! The site is currently in development but we will have some excellent content for you in the near future. Be sure to check back soon! </p>

<?php
if ($_POST && isset($missing)) {
?>
<p class="warning">Please complete the missing item(s) indicated by the star (*).</p>
<?php
}
elseif ($_POST && !$mailSent) {
?>
<p class="warning">Sorry, there was a problem sending your message. Please try later.</p>
<?php
}
elseif ($_POST && $mailSent) {
?>
<p><strong>Your message has been sent. Thank you for your feedback.</strong></p>
<?php } ?>


<form id="two" method="post" action="">
<fieldset id="contact">
<legend>Contact Us</legend>
<label for="name">Name : <?php
if (isset($missing) && in_array('name', $missing)) { ?>
<span class="warning">*</span><?php } ?>
</label>
<input name="name" id="name" type="text" tabindex="2"
<?php if (isset($missing)) {
echo 'value=" '.htmlentities($_POST['name']).'"';
} ?>
/>
<br />
<label for="address">e-mail : <?php
if (isset($missing) && in_array('email', $missing)) { ?>
<p="warning">*</p><?php } ?>
</label>
<input name="email" id="email" type="text" tabindex="3"
<?php if (isset($missing)) {
echo 'value=" '.htmlentities($_POST['email']).'"';
} ?>
/>
<br />
<lable for ="comments">Ask a Question : <?php
if (isset($missing) && in_array('comments', $missing)) { ?>
<span class="warning">*</span><?php } ?>
</lable>
<textarea name="comments" id="comments" tabindex="40" cols="40" rows="10"><?php
if (isset($missing)) {
echo htmlentities($_POST['comments']);
} ?></textarea>
</fieldset>
<p>
<input name="send" id="send" type="submit" value="Send message" />
<input name="button2" id="button2" type="reset" value="Reset" />
</p>
</form>

_Aerospace_Eng_
07-23-2007, 10:24 PM
Are you sure your webhost allows the php mail function?

srule_
07-23-2007, 10:32 PM
yes, it worked fine before i changed.

However my old script was not placed before the doctype. Instead i set "action" to to my php file that processed the form.
ex:

Before: <form id="two" method="post" action="do_contact.php">

now: <form id="two" method="post" action="">
And php is before doctype.


*note that the php in my present script is much diffrent from before, what i noted above is not the only diffrence.

srule_
07-25-2007, 02:00 AM
Fixed! I simply needed to add the FROM field to my script. thanks for the help



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum