php - Contact form validation and email send. Works. How to do things better? -


this code works , works although know there redundancies , better ways things. one, instead of using form action=mypage.php, have php html code. when tried have in mypage.php, none of variables found in $_post. though isset result true on $_post. so... weird, , screw it, put in html. trying wamp, wouldn't work on server either.

another issue, refresh of page after submission send email well, might resolved if solve form action issue above.

how's validation? recommended improve process or security?

<h1><div class="titlewrapper"><div class="titlecontact">contact</div></div></h1>        <div class="contact">           <ul class="contactcolumn">             <li>               <div class="fields">                 <form method='post'>                   <input type="text" name="name" placeholder="your name" maxlength="50">                   <input type="text" name="email" placeholder="your email" maxlength="80">                   <textarea name ="message" placeholder="your message" onfocus="this.placeholder = ''" onblur="this.placeholder = 'your message'" maxlength="2000"></textarea>                   <input type="checkbox" name="copy" style="display: inline-block;float:left"><span style="float:left;padding:17px 0 0 5px">send me copy</span>                   <input type="submit" name="submit" value="submit">                 </form>               </div>             </li>           </ul>            <ul class="contactcolumn">             <div class="rightside">               <p>                 still have questions?               </p>               <p>                 need more information?               </p>               <p>                 feel free contact us!               </p>             </div>           </ul>            <?php           // define variables , set empty values           $nameerr = $emailerr = $gendererr = $websiteerr = "";           $name = $email = $gender = $comment = $website = "";            if ($_server["request_method"] == "post") {              if (empty($_post["name"])) {                $nameerr = "name required";              } else {                $name = test_input($_post["name"]);                // check if name contains letters , whitespace                if (!preg_match("/^[a-za-z ]*$/",$name)) {                  $nameerr = "only letters , white space allowed in name";                }              }               if (empty($_post["email"])) {                $emailerr = "email required";              } else {                $email = test_input($_post["email"]);                // check if e-mail address well-formed                if (!filter_var($email, filter_validate_email)) {                  $emailerr = "invalid email format";                }              }               if (empty($_post["message"])) {                $comment = "";              } else {                $comment = test_input($_post["message"]);              }              if (empty($nameerr) && empty($emailerr)){                $to = "myemail@email.com";                $subject = "contact form submission";                $subject2 = "copy of contact form";                $message = $name . " wrote following:" . "\n\n" . $_post['message'];                $message2 = "here copy of message " . $name . "\n\n" . $_post['message'];                 $headers = "from:" . $email;                $headers2 = "from:" . $to;                mail($to,$subject,$message,$headers);                 if (isset($_post['copy'])) {                  mail($email,$subject2,$message2,$headers2); // sends copy of message sender                }                echo   "<div class='errormessagewrapper'><div class='errormessage'><div style='color:#333'>                        thank contacting " .                        $name .                        ". in touch shortly. </div></div>";                // can use header('location: thank_you.php'); redirect page.              }              if (!empty($nameerr)) {                echo "<div class='errormessagewrapper'><div class='errormessage'><li style='list-style-type: circle;'>" . $nameerr . "</li></div></div>";              }              if (!empty($emailerr)) {                echo "<div class='errormessagewrapper'><div class='errormessage'><li style='list-style-type: circle;'>" . $emailerr . "</li></div></div>";              }           }            function test_input($data) {              $data = trim($data);              $data = stripslashes($data);              $data = htmlspecialchars($data);              return $data;           }           ?>       </div> 

i thinking make send email simpler less code suggest phpmailer less code , easier use.

take @ documentation phpmailer at: phpmailer documentation

phpmailer should make easier read if understand phpmailer, , simple write.

here example:

<?php  require_once "vendor/autoload.php";  $mail = new phpmailer;  $mail->from = "from@yourdomain.com"; $mail->fromname = "full name";  $mail->addaddress("recipient1@example.com", "recipient name");  $mail->ishtml(true);  $mail->subject = "subject text"; $mail->body = "<i>mail body in html</i>"; $mail->altbody = "this plain text version of email content";  if(!$mail->send())  { echo "mailer error: " . $mail->errorinfo; }  else  { echo "message has been sent successfully"; } 

here link the sending emails phpmailer: link


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -