...

View Full Version : script failing to write to logs and send mail RARELY



ajetrumpet
09-10-2009, 08:20 AM
all,

i have a long script to write info to a an online log and send me a message via the form that was submitted using the mail() function. my code is long because i'm no expert in PHP. here is the whole thing:
<script type="text/javascript">
function MM_goToURL() { //v3.0
var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
</script>

<?

if ($_POST["type"] == "order") {
if (! $_POST["Company"]) {
echo '<script language="javascript">alert(
"Please fill in all required fields...");history.back();</script>';
exit;
}
}
if (! $_POST["Name"] || ! $_POST["LName"] || ! $_POST["email"] || ! $_POST["phone"])
{
echo '<script language="javascript">alert(
"Please fill in all required fields...");history.back();</script>';
}
else
{
if (strpos($_POST["email"], "@")) {
//DO NOTHING
}
else {
echo '<script language="javascript">alert(
"Please provide a valid email address...");history.back();</script>';
exit;
}

if (strlen($_POST["phone"]) != 12)
{
echo '<script language="javascript">alert(
"Please provide a valid phone number...");history.back();</script>';
exit;
}
else
{
for ($i = 0; $i <= 11; $i++) {
if ($i == 3 || $i == 7) {
if (substr($_POST["phone"], $i, 1) == "-") {
//DO NOTHING
}
else {
echo '<script language="javascript">alert(
"Please provide a valid phone number...");history.back();</script>';
exit;
}
}
else {
if (is_numeric(substr($_POST["phone"], $i, 1))) {
//DO NOTHING
}
else {
echo '<script language="javascript">alert(
"Please provide a valid phone number...");history.back();</script>';
exit;
}
}
}
}

$to = $_POST["email"];
$subject = "Thank you for your inquiry!";
$body = "Dear " . $_POST["Name"] . "," . "\r\n" . "\r\n" .
"Thank you for contacting us! BLAH BLAH BLAH" . "\r\n" . "\r\n" .
"This is a system generated confirmation. Please do not respond to this message.";
$headers = 'From: No-Reply@mydomain.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();

if (mail($to, $subject, $body, $headers)) {
//echo("<p>Message successfully sent!</p>");
} else {
//echo("<p>Message delivery failed...</p>");
}

// First we set up some parameters for the receiver
$receiver_name = 'my company';
$receiver_email = 'corp@mydomain.com, head@mydomain.com';
$mydomain = 'mydomain.com';

// This is for the sender's name - NAME OF THE SENDER
$send_from = $_POST["Name"].' '.$_POST["LName"];

// This is for the sender's email - EMAIL ADDRESS OF THE SENDER
$send_from_email = $_POST["email"];

// This is for the message phone - PHONE NUMBER
$send_phone = $_POST["phone"];

//This gives the receiver the SENDER'S NAME and EMAIL ADDRESS
$send_header = 'From: ' . $_POST["email"] . "\r\n" .
'Reply-To: ' . $_POST["email"] . "\r\n" . 'X-Mailer: PHP/' . phpversion();

// This is for the message body - MESSAGE THAT IS TYPED IN BY THE SENDER
if (! $_POST["Message"]){
$send_message = 'Not Provided';}
else{
$send_message = $_POST["Message"];}

if ($_POST["specific"] == "language") {
$_POST["LanguageCheck"] = 'on'; }
elseif ($_POST["specific"] == "listen") {
$_POST["ListenCheck"] = 'on'; }
else {
}

if ($_POST["type"] == "info") {
$submit_type = "info";
// This is for the product of interest - PRODUCTS CHECKED
$send_product = '';
if ($_POST["pro"] == 'on') {
$send_product = $send_product . 'pro';
}
if ($_POST["hh"] == 'on') {
$send_product = $send_product . 'hh';
}
if ($_POST["res"] == 'on') {
$send_product = $send_product . 'res';
}
if ($_POST["lang"] == 'on') {
$send_product = $send_product . 'Language ';
}
if ($_POST["listen"] == 'on') {
$send_product = $send_product . 'Listening';
}
if ($_POST["menu"] == 'on') {
$send_product = $send_product . 'Menu';
}
if ($_POST["que"] == 'on') {
$send_product = $send_product . 'QuePage';
}
if ($send_product == '') {
$send_product = 'Not Provided ';
}
$send_title = 'iRequest: '.$_POST["Name"].' '.$_POST["LName"];
$send_body = 'Sender: ' . $send_from . "\r\n" . "\r\n" .
'Email: ' . $send_from_email . "\r\n" . "\r\n" .
'Phone: ' . $send_phone . "\r\n" . "\r\n" .
'Product(s) Interest In: ' . substr($send_product, 0, strlen($send_product) - 2) .
"\r\n" . "\r\n" . 'Message: ' . $send_message . "\r\n" . "\r\n" . "\r\n" . "\r\n" .
"The following message was sent to: " . $_POST["email"] . "\r\n" . "\r\n" . $body;
}

elseif ($_POST["type"] == "demo") {
$submit_type = "demo";
// This is for the product of interest - PRODUCTS CHECKED
$send_product = '';
if ($_POST["pro"] == 'on') {
$send_product = $send_product . 'pro';
}
if ($_POST["hh"] == 'on') {
$send_product = $send_product . 'hh';
}
if ($send_product == '') {
$send_product = 'Not Provided ';
}
$send_title = 'dRequest: '.$_POST["Name"].' '.$_POST["LName"];
$send_body = 'Sender: ' . $send_from . "\r\n" . "\r\n" .
'Email: ' . $send_from_email . "\r\n" . "\r\n" .
'Phone: ' . $send_phone . "\r\n" . "\r\n" . 'Demonstration(s) Requested: ' .
substr($send_product, 0, strlen($send_product) - 2) . "\r\n" . "\r\n" .
'Message: ' . $send_message . "\r\n" . "\r\n" . "\r\n" . "\r\n" .
"The following message was sent to: " . $_POST["email"] . "\r\n" . "\r\n" . $body;
}
else {
// This is for the product of interest - PRODUCTS CHECKED
$submit_type = "order";
$send_product = '';
if ($_POST["pro"] == 'on') {
$send_product = $send_product . 'pro';
}
if ($_POST["hh"] == 'on') {
$send_product = $send_product . 'hh';
}
if ($_POST["res"] == 'on') {
$send_product = $send_product . 'res';
}
if ($_POST["lang"] == 'on') {
$send_product = $send_product . 'Language ';
}
if ($_POST["listen"] == 'on') {
$send_product = $send_product . 'Listening';
}
if ($_POST["menu"] == 'on') {
$send_product = $send_product . 'Menu';
}
if ($_POST["que"] == 'on') {
$send_product = $send_product . 'QuePage';
}
if ($send_product == '') {
$send_product = 'Not Provided ';
}
$send_title = 'oRequest: '.$_POST["Name"].' '.$_POST["LName"];
$send_body = 'Sender: ' . $send_from . "\r\n" . "\r\n" .
'Email: ' . $send_from_email . "\r\n" . "\r\n" .
'Phone: ' . $send_phone . "\r\n" . "\r\n" .
'Product(s) Requested: ' . substr($send_product, 0, strlen($send_product) - 2) .
"\r\n" . "\r\n" . 'Message: ' . $send_message . "\r\n" . "\r\n" . "\r\n" . "\r\n" .
"The following message was sent to: " . $_POST["email"] . "\r\n" . "\r\n" . $body;
}

// Now we need to send the message
$deliver = mail($receiver_email, $send_title, $send_body, $send_header);

// the PHP mail() function = true if message processed so we check for this below.
if ($deliver == TRUE) {
$ContentAdd = "";
$redirect = "yes";
} else {
$redirect = "no";
$ContentAdd = " -- FORM SUBMISSION FAILED! REASON UNKNOWN...";
}

// This code will record the order submission in the orders LOG file
if ($submit_type == "info") {
$path = "info.txt"; }
elseif ($submit_type == "demo") {
$path = "demos.txt"; }
else { $path = "orders.txt"; }

$s_name = $_POST["Name"] . " " . $_POST["LName"];
$s_email = $_POST["email"];
$s_phone = $_POST["phone"];
$s_date = date(DATE_COOKIE);
$Content = $s_name . ", " . $s_email . ", " .
$s_phone . ", " . $s_date . $ContentAdd;

$fh = @fopen($path,"a");
fputs($fh, $Content . "<br>");
fclose ($fh);
if ($redirect == "yes") {
header("Location: http://www.mydomain.com/formsubmission.php");
} else {
echo '<p>We had a problem sending your request.
Please e-mail your message directly to us at: corp@mydomain.com
<input name="button" type="button" id="button" onclick=javascript:MM_goToURL(
"parent","index.php");return document.MM_returnValue value="Home" />';
}
}

?>every once in awhile (say, about every 5th time, which is quite often for us) I will see in the site activity logs that someone visited the page http://www.mydomain.com/formsubmission.php. and the only way to get there is by submitting a form through the above script, as it redirects you to the confirmation page. sometimes i will see a visit from an IP address to this page, but I will get nothing in my inbox giving me the information that they filled out in the form....nor will script write the information that it gets from the form submission to the appropriate log file! I don't get this. I assume that it might be because the mail() function failed for some reason. but even if it fails, the code still is supposed to write to the log file i specify, but that doesn't happen. I suppose it's possible that theses occasional visits are being made by IT company robots that are indexing the internet like google does or something like that, but I'm doubtin' it. the last time this happened was today, and the visitor was from a small town located 10 miles away from me. and i'm doubting that anyone having internet access in that town is aware of this page on my domain and even has the desire to type the URL in the web address bar directly, so i have to assume they submitted a form.

any advice from anyone on here with this? if i am missing information that is being submitted to me, i really need another way to get this stuff done, as i'm sure there are other problems with this process that i'm not aware of. but fortunately they are not a problem yet but i'm sure they will be as my volume of traffice increases on my site. thanks!

Zangeel
09-10-2009, 09:57 AM
Sorry jsut want to point out this



if (strpos($_POST["email"], "@")) {
//DO NOTHING
}
else {
echo '<script language="javascript">alert(
"Please provide a valid email address...");history.back();</script>';
exit;
}


There's nooooo reason for that. If you're not gonna DO anything you dont need the if there in the first place, n why use stripos when you can use regex?



if (!preg_match("/[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.{1}[a-zA-Z0-9-.]+$/", $email))
{
echo '<script>BlahBlah Email</script>';
}

ajetrumpet
09-10-2009, 09:59 AM
n why use stripos when you can use regex?

because i'm no expert in PHP, that's why. i have seen the snippet you posted before, and i will change it at some point, but haven't gotten around to it yet. thanks for posting it here too....i will make sure to get it in the script so it's more useful in the future. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum