PHP Program for Submitting Form to E-mail
Recently during a website redesign, I wanted to create a program to submit forms to email. Many solutions I found didn’t work, so I developed my own.
<html><body><?phpfunction spamcheck($field) { //filter_var() sanitizes the e-mail //address using FILTER_SANITIZE_EMAIL $field=filter_var($field, FILTER_SANITIZE_EMAIL);
//filter_var() validates the e-mail //address using FILTER_VALIDATE_EMAIL if(filter_var($field, FILTER_VALIDATE_EMAIL)) { return TRUE; } else { return FALSE; } }
if (isset($_REQUEST['w_14'])) {//if "email" is filled out, proceed
//check if the email address is invalid $mailcheck = spamcheck($_REQUEST['w_14']); $w_1_check = $_REQUEST['w_1']; $w_8_check = $_REQUEST['w_8']; $w_13_check = $_REQUEST['w_13']; if ($mailcheck==FALSE) { echo "<script language=\"JavaScript\">\r\n"; echo " alert(\"Please fill in a valid E-Mail so our staff can contact you. Thank you!\");\r\n"; echo " history.back();\r\n"; echo "</script>"; } if ($w_1_check=='') { echo "<script language=\"JavaScript\">\r\n"; echo " alert(\"Please fill in the company name so our staff can contact you. Thank you!\");\r\n"; echo " history.back();\r\n"; echo "</script>"; } if ($w_8_check=='') { echo "<script language=\"JavaScript\">\r\n"; echo " alert(\"Please fill in your name so our staff can contact you. Thank you!\");\r\n"; echo " history.back();\r\n"; echo "</script>"; } if ($w_13_check=='') { echo "<script language=\"JavaScript\">\r\n"; echo " alert(\"Please fill in QQ/MSN number so our staff can contact you. Thank you!\");\r\n"; echo " history.back();\r\n"; echo "</script>"; } else {//send email $email = $_REQUEST['w_14'] ; $subject = $_REQUEST['w_1'] . " " . $_REQUEST['w_8'] ; $message = "Company Name :". $_REQUEST['w_1'] . "\r\n" . "Website Languages :". $_REQUEST['w_2_1'] . "/" . $_REQUEST['w_2_2'] . "/" . $_REQUEST['w_2_3'] . "/" . $_REQUEST['w_2_4'] . "/" . $_REQUEST['w_2_5'] . "/" . "\r\n" . "Reference Sites :". $_REQUEST['w_3'] . "\r\n" . "Main Business :". $_REQUEST['w_4'] . "\r\n" . "Budget :". $_REQUEST['w_5'] . "-" . $_REQUEST['w_6'] . "\r\n" . "Completion Time :". "Expected date " . $_REQUEST['w_7'] . "\r\n" . " Expected days " . $_REQUEST['w_7_1'] . "\r\n" . "Other Requirements :". $_REQUEST['w_7_2'] . "\r\n" . "=============================================================" . "\r\n" . "Name :". $_REQUEST['w_8'] . "\r\n" . "Gender :". $_REQUEST['w_9'] . "\r\n" . "Phone :". $_REQUEST['w_11'] . "\r\n" . "Mobile :". $_REQUEST['w_12'] . "\r\n" . "QQ/MSN :". $_REQUEST['w_13'] . "\r\n" . "Email :". $_REQUEST['w_14'] . "\r\n" . "Skype :". $_REQUEST['w_15'] . "\r\n" . "Zip Code :". $_REQUEST['w_16'] . "\r\n" . "Address :". $_REQUEST['w_17'] . "\r\n"; mail("admin@zhaojian.net", "$subject Website Request Form", $message, "From: $email"); echo "<script language=\"JavaScript\">\r\n"; echo "alert(\"Submission successful! Please wait for our staff to contact you. Redirecting to homepage.\");\r\n"; echo "location.replace(\"http://www.zhaojian.net/\");\r\n"; echo "</script>"; } }else {//if "email" is not filled out, display the form echo "<form id='form1' action='http://www.zhaojian.net/b/b.php' method='post' name='form1'> ... </form>"; }?>
</body></html>