September 22, 201115 yr Hi,Everyone Im Looking For A Contact Form That Will Send An Email To 2 Site Owners And 1 Customer Email Address With The Fields Name,Email,Phone And Messege The Customer Email Saying "Thank You For Contacting Us" And The Owner Email Saying Whats Entered Into The Fields
September 23, 201115 yr Seeing this made me get down and finish off cobbling together a couple of bits of PHP I found. Actually worked first time, which is a first for me where PHP is concerned. It has the facility to submit to multiple recipients and will also send a response back to the sender. <?php /* Set e-mail recipient */ $myemail = "recipient1@example.com , recipient2@example.com"; if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $clientIP = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $clientIP = $_SERVER['REMOTE_ADDR']; } /* Check all form inputs using check_input function */ $yourname = check_input($_POST['yourname'], "Enter your name"); $subject = check_input($_POST['subject'], "Write a subject"); $email = check_input($_POST['email']); $website = check_input($_POST['website']); $comments = check_input($_POST['comments'], "Write your comments"); /* If e-mail is not valid show error message */ if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) { show_error("E-mail address not valid"); } /* If URL is not valid set $website to empty */ if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website)) { $website = ''; } /* Let's prepare the message for the e-mail */ $message = "Hello! Your contact form has been submitted by: Name: $yourname E-mail: $email URL: $website Comments: $comments End of message "; /* Send the message using mail() function */ mail($myemail, $subject, $message); /* Prepare autoresponder subject */ $respond_subject = "Thank you for contacting us!"; /* Prepare autoresponder message */ $respond_message = "Hello! Thank you for contacting us! We will get back to you as soon as possible! Yours sincerely, Your name www.yourwebsite.com "; /* Send the message using mail() function */ mail($email, $respond_subject, $respond_message); /* Redirect visitor to the thank you page */ header('Location: thanks.html'); exit(); /* Functions we used */ function check_input($data, $problem='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) { show_error($problem); } return $data; } function show_error($myError) { ?> <html> <body> <b>Please correct the following error:</b><br /> <?php echo $myError; ?> </body> </html> <?php exit(); } ?>
September 23, 201115 yr Remember to add a CAPTCHA or an IP based 'flood' control (preferably both) because using the script as is means there is a risk of your web hosts locking your site down for spamming* if somebody by accident or intent decides to send you multiple messages in a short space of time. * does not matter who the messages are intended for, it's messages per minute via the mail() function that trips the alarm
September 23, 201115 yr Remember to add a CAPTCHA or an IP based 'flood' control (preferably both) because using the script as is means there is a risk of your web hosts locking your site down for spamming* if somebody by accident or intent decides to send you multiple messages in a short space of time. * does not matter who the messages are intended for, it's messages per minute via the mail() function that trips the alarm Excellent point. Now has Flood prevention that prevents a message being sent within 1 min from the previous message. <?php /* Set e-mail recipient */ $myemail = "recipient1@example.com , recipient2@example.com"; if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $clientIP = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $clientIP = $_SERVER['REMOTE_ADDR']; } /* Flood Prevention */ $fromIP = $_SERVER['REMOTE_ADDR'] . "\n"; $filename = 'lastip.txt'; $handle = fopen($filename, 'r+'); $ok = 1; if($handle) { $lastIP = fgets($handle); if($lastIP == $fromIP) { $modt = filemtime($filename); $nowis = time(); // unix time counts in seconds if($nowis - $modt < 60) $ok = 0; } rewind($handle); } else // file doesn't exist, create it $handle = fopen($filename, 'w'); fwrite($handle, $fromIP); fclose($handle); sleep(10); // anti-flooding measure if(0==$ok) { print <<<END <html><body> ERROR: You cannot send another message so soon!<br>Please wait 1 minute then retry </body></html> END; exit; } /* Check all form inputs using check_input function */ $yourname = check_input($_POST['yourname'], "Enter your name"); $subject = check_input($_POST['subject'], "Write a subject"); $email = check_input($_POST['email']); $website = check_input($_POST['website']); $likeit = check_input($_POST['likeit']); $how_find = check_input($_POST['how']); $comments = check_input($_POST['comments'], "Write your comments"); /* If e-mail is not valid show error message */ if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) { show_error("E-mail address not valid"); } /* If URL is not valid set $website to empty */ if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website)) { $website = ''; } /* Let's prepare the message for the e-mail */ $message = "Hello! Your contact form has been submitted by: Name: $yourname E-mail: $email URL: $website Like the website? $likeit How did he/she find it? $how_find Comments: $comments End of message "; /* Send the message using mail() function */ mail($myemail, $subject, $message); /* Prepare autoresponder subject */ $respond_subject = "Thank you for contacting us!"; /* Prepare autoresponder message */ $respond_message = "Hello! Thank you for contacting us! We will get back to you as soon as possible! Yours sincerely, Your name www.yourwebsite.com "; /* Send the message using mail() function */ mail($email, $respond_subject, $respond_message); /* Redirect visitor to the thank you page */ header('Location: thanks.html'); exit(); /* Functions we used */ function check_input($data, $problem='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) { show_error($problem); } return $data; } function show_error($myError) { ?> <html> <body> <b>Please correct the following error:</b><br /> <?php echo $myError; ?> </body> </html> <?php exit(); } ?>
September 23, 201115 yr Might wanna include the check_input() function there^ Which? This one? /* Functions we used */ function check_input($data, $problem='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) { show_error($problem); } return $data; } Or am I missing some other part of the dark art of PHP?
September 23, 201115 yr Which? This one? /* Functions we used */ function check_input($data, $problem='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) { show_error($problem); } return $data; } Or am I missing some other part of the dark art of PHP? Wow am I going blind? Ignore me, I seem to have missed that part. lol
Create an account or sign in to comment