Patwan Posted October 10, 2016 Author Share Posted October 10, 2016 Hey folks, I took heed of your advice & have tried rectifying the form over the weekend. I noticed that PHPMailer works fine but after adding $valid functions it ceases from sending emails to my inbox. - I still think there is an error in how I code the $valid functions to enhance validation before sending mail via PHPMailer. -Could any moderator in this forum create a tutorial of "Validating a PHP contact form using Regexp before sending via PHPMailer" so that me & others in my position could benefit. ~ Kindest regards... Link to comment Share on other sites More sharing options...
dsonesuk Posted October 10, 2016 Share Posted October 10, 2016 IF your phpmailer SMTP version works then this should work <?php //initialize variables and set to empty values $name = $phone = $email = $business = " "; $nameErr = $phoneErr = $emailErr = $businessErr = " "; ini_set('display_errors', 1); error_reporting(E_ALL); if ($_SERVER["REQUEST_METHOD"] == "POST") { $valid = true; //check if name contains letters and white-space if (empty($_POST["name"])) { $valid = false; $nameErr = "Please fill out this field"; echo 'name is empty<br>'; } else { $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ,.-]*$/", $name)) { $valid = false; $nameErr = "*Only Letters and white-spaces are allowed*"; echo 'name did not pass regexp<br>'; } /* else { echo'name is valid<br>'; } */ } //check if phone contains numbers if (empty($_POST["phone"])) { $valid = false; $phoneErr = "Please fill out this field"; echo'phone is empty<br>'; } else { $phone = test_input($_POST["phone"]); if (!preg_match("/^[0-9 ,+.-]*$/", $phone)) { $valid = false; $phoneErr = "*Only numbers are allowed*"; echo 'phone did not pass regexp<br>'; } /* else { echo' phone is valid<br>'; } */ } //check if email is valid if (empty($_POST["email"])) { $valid = false; $emailErr = "Please fill out this field"; echo'email is empty<br>'; } else { $email = test_input($_POST["email"]); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $valid = false; $emailErr = "*Invalid email address*"; echo 'email did not pass regexp<br>'; } /* else { echo'email is valid<br>'; } */ } //check if business field is valid if (empty($_POST["business"])) { $valid = false; $businessErr = "Please fill out this field"; ///WHERE is this used? echo 'business is empty<br>'; } else { $business = test_input($_POST["business"]); if (!preg_match("/^[a-zA-Z-.+:; ]*$/", $business)) { $valid = false; $businessErr = "*Only letters and spaces are allowed*"; ///WHERE is this used? echo 'business did not pass regexp<br>'; } /* else { echo 'business is valid<br>'; } */ } require ("PHPMailer_5.2.4/class.phpmailer.php"); //including phpmailer class $mail = new PHPMailer(); $mail->IsSMTP(); // set mailer to use SMTP $mail->SMTPDebug = 0; $mail->Host = "smtp.gmail.com"; // specify main and backup server $mail->SMTPSecure = "ssl"; // Connect using a TLS connection $mail->Port = 465; //Gmail SMTP port $mail->SMTPAuth = true; // turn on SMTP authorization $mail->Username = "my gmail account"; // SMTP username $mail->Password = " ************** "; // SMTP password $mail->From = $email; //email of sender $mail->FromName = $name; //name of the sender $mail->AddAddress("my gmail account", "Patwan"); //email address of recepient and name $mail->AddAddress("my gmail account", "Test email"); // name is optional $mail->AddReplyTo($_POST["email"], $_POST["name"]); //Address to which recepient will reply $mail->WordWrap = 100; // set word wrap to 100 characters $mail->IsHTML(true); // set email format to HTML $mail->Subject = "Contact Form"; //subject of email $mail->Body = "Name: " . $_POST['name'] . "<br>Phone: " . $_POST['phone'] . "<br>Email: " . $_POST['email'] . "<br>Business: " . $_POST['business']; //$mailphp = false; if ($valid) { // if (!$mailphp) { if (!$mail->Send()) { echo 'Form could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; exit; } else {hierarchy header('Location:http://change-to-you-domain/full-hierarchy-folder-path-to-thank-you-folder/thank_you.html'); } } } //} function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <!DOCTYPE html> <html> <head> <meta name= "robots" content= "noindex, nofollow"> <meta charset="UTF-8"> <meta name= "author" content= "Kelly James"> <link href= "style.css" rel= "stylesheet" type= "text/css"> <title> Contact form </title> </head> <body> <img src= "images/logo.gif" border= "0" width= "240" height="160"> <br> <div class="form"> <form method= "post" action= "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" > <h4 id="h4form"> Please fill the form below to kick start your project. <br> <br> <font color="red"> *required field </font> </h4> <br> What is your name? <input type= "text" class= "input" name= "name" placeholder= "e.g Nelson Mandela" value="<?php echo $name; ?>" required> <span class= "error"> * <br> <?php echo $nameErr; ?> </span> <br> <br>What is your phone number? <input type= "text" class= "input" name= "phone" value="<?php echo $phone; ?>" placeholder= "e.g 0722222222" required> <span class= "error"> * <br> <?php echo $phoneErr; ?> </span> <br> <br> What is your email address? <input type= "email" class= "input" name= "email" placeholder= "will not be published" value="<?php echo $email; ?>" required> <span class= "error"> * <br> <?php echo $emailErr; ?> </span> <br> <br> What is the name of your business or institution? <input type= "text" class= "input" name="business" placeholder= "e.g Microsoft Corporation" value= "<?php echo $business; ?>" required> <span class= "error"> * <br> <?php echo $businessErr; ?> </span> <br> <br> <button class="submit" type= "submit" value= "Submit"> Submit Form </button> </form> </div> </body> </html> Link to comment Share on other sites More sharing options...
Patwan Posted October 11, 2016 Author Share Posted October 11, 2016 Hey dsonesuk, I really appreciate your help. Does the code above work,,? Cause it still doesn't work for me.... Link to comment Share on other sites More sharing options...
dsonesuk Posted October 11, 2016 Share Posted October 11, 2016 Yes! It works! Obviously gmail username, password need to be changed, I did not install phpmailer, but created a varible to represent email being sent, used that in if condition, if $valid is true, and my variable is true it redirects to ty page.  Ha!, just noticed  } else {hierarchy header('Location:http://change-to-you-domain/full-hierarchy-folder-path-to-thank-you-folder/thank_you.html'); }  REMOVE "hierarchy" IN BOLD Link to comment Share on other sites More sharing options...
Patwan Posted October 11, 2016 Author Share Posted October 11, 2016 Wow, Thanks, Let me Keep trying my luck Link to comment Share on other sites More sharing options...
Patwan Posted October 12, 2016 Author Share Posted October 12, 2016 Wow! wow!, the form finally gets to work. You guys are the BEST!! :Happy: Thank you very much dsonesuk & justsomeguy... Be Blessed Link to comment Share on other sites More sharing options...
Patwan Posted October 12, 2016 Author Share Posted October 12, 2016 Do you guys have a tutorial in W3schools of "How to send an auto-responder email to a client"? Link to comment Share on other sites More sharing options...
dsonesuk Posted October 12, 2016 Share Posted October 12, 2016 Good!, the other suggestion is not to include $_POST[....] in the email message, but the variables that went through validation function. Link to comment Share on other sites More sharing options...
Patwan Posted October 12, 2016 Author Share Posted October 12, 2016 Duly noted, I'll put that into consideration Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now