mike3075 Posted February 20, 2020 Share Posted February 20, 2020 (edited) I really need some help. I am very new to PHP and I've been stuck for a couple of days. I created a contact form and everything seems to be working correctly, except the mail is showing it's sent, but I never receive an email in my Inbox or Spam folder. Can someone please review my code? Here is the PHP: <?php // DEFINE VARIABLE AND SET EMPTY VALUES $varfnameErr = $varlnameErr = $varemailErr = $varphoneErr = $varpositionErr = ""; $varfname = $varlname = $varemail = $varphone = $varposition = $success = ""; //FORM SUBMITTED WITH POST METHOD if ($_SERVER["REQUEST_METHOD"] == "POST") { //VALIDATE FIRST NAME if (empty($_POST["varfname"])) { $varfnameErr = "First Name is required"; } else { $varfname = test_input($_POST["varfname"]); // MAKE SURE FIRST NAME ONLY CONTAINS LETTERS AND WHITE SPACE if (!preg_match("/^[a-zA-Z ]*$/",$varfname)) { $varfnameErr = "Only letters and white space are allowed"; } } //VALIDATE LAST NAME if (empty($_POST["varlname"])) { $varlnameErr = "Last Name is required"; } else { $varlname = test_input($_POST["varlname"]); // MAKE SURE LAST NAME ONLY CONTAINS LETTERS AND WHITE SPACE if (!preg_match("/^[a-zA-Z ]*$/",$varlname)) { $varlnameErr = "Only letters and white space are allowed"; } } //VALIDATE EMAIL ADDRESS if (empty($_POST["varemail"])) { $varemailErr = "Email Address is required"; } else { $varemail = test_input($_POST["varemail"]); // MAKE SURE EMAIL ADDRESS IS FORMATTED CORRECTLY if (!filter_var($varemail, FILTER_VALIDATE_EMAIL)) { $varemailErr = "Invalid email address format"; } } //VALIDATE PHONE NUMBER if (empty($_POST["varphone"])) { $varphoneErr = "Phone number is required"; } else { $varphone = test_input($_POST["varphone"]); // MAKE SURE PHONE NUMBER IS IN CORRECT FORMAT if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$varphone)) { $varphoneErr = "Invalid telephone format"; } } //VALIDATE POSITION if (empty($_POST["varposition"])) { $varpositionErr = "Position is required"; } else { $varposition = test_input($_POST["varposition"]); } //IF ALL DATA IS CORRECT if ($varfnameErr == '' and $varlnameErr == '' and $varemailErr == '' and $varphoneErr == '' and $varpositionErr == '') { $message_body = ''; unset($_POST['submit']); //THIS IS JUST FOR TESTING PURPOSES $message_body = $varfname; //foreach ($_POST as $key => $value) { // $message_body .= "$key: $value\n"; //} $to = 'mygmail@gmail.com'; $subject = 'Volunteer Form Submission'; $message = wordwrap($message_body, 70); $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; $headers .= "From: " . $varemail . "\r\n"; $subject = 'Volunteer Form Submission'; $to = 'mygmail@gmail.com'; $result = mail($to, $subject, $message, $headers); //THIS RETURNS "1" print $result; //IF ALL DATA IS CORRECT MAKE SURE EMAIL WAS SENT if (mail($to, $subject, $message, $headers)) { $success = "Message sent. Thank you contacting us! We will reply as soon as possible."; $varfname = $varlname = $varemail = $varphone = $varposition = ""; } else { $success = "Something went wrong!"; } } } //STRIP UNWANTED CHARACTERS FROM VARIABLES function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> Here is the HTML: <div class="row regformrow"> <div class="coffee-span-12 regformcolumn"> <div class="subgrid regformsubgrid"> <div class="row regformsubgridheaderrow"> <div class="coffee-span-12 regformsubgridheadercolumn"> <h4 class="regformsubgridheadertitle">Volunteer Form</h4> </div> </div> <div class="row regformsubgridrow"> <form class="form-container regformsubgridformcontainer" action=<?= $_SERVER['PHP_SELF']; ?> method="post"> <div class="coffee-span-4 coffee-972-span-6 coffee-458-span-12 regformsubgridcolumn1"> <div class="container regformsubgridcontainer1"> <label class="label form-label-all"><span style="font-style:italic;color:#FF5454;">* Required Fields</span></label></br> <div class="formfieldgroup"> <input class="fname" name="varfname" type="text" tabindex="1" placeholder="First Name" value="<?= $varfname; ?>"><span class="requiredfield"> *</span> <span class="form-error"><?= $varfnameErr ?></span> </div> <div class="formfieldgroup"> <input class="lname" name="varlname" type="text" tabindex="2" placeholder="Last Name" value="<?= $varlname; ?>"><span class="requiredfield"> *</span> <span class="form-error"><?= $varlnameErr ?></span> </div> <div class="formfieldgroup"> <input class="email" name="varemail" type="text" tabindex="2" placeholder="Email Address" value="<?= $varemail; ?>"><span class="requiredfield"> *</span> <span class="form-error"><?= $varemailErr ?></span> </div> <div class="formfieldgroup"> <input class="phone" name="varphone" type="text" tabindex="2" placeholder="Telephone Number" value="<?= $varphone; ?>"><span class="requiredfield"> *</span> <span class="form-error"><?= $varphoneErr ?></span> </div> <div class="formfieldgroup"> <select class="select selectbox" name="varposition" id="varposition" tabindex="5"><option value="">Select one...</option><option value="Chaperone">Chaperone</option><option value="Class Monitor">Class Monitor</option><option value="Parking Attendant">Parking Attendant</option><option value="Party Coordinator">Party Coordinator</option><option value="Teacher Aid">Teacher Aid</option></select><span class="requiredfield"> *</span> <span class="form-error"><?= $varpositionErr ?></span> </div> <div class="formfieldgroup"> <input class="test" name="test" type="text" tabindex="6" placeholder="If you are human, leave this field blank" value=""> </div> <div class="formfieldgroup"> <button type="submit" class="button-submit-1" name="submit" tabindex="7" data-submit="...Sendng">Submit</button> </div> <div class="formfieldgroup"> <div class="success"><?= $success; ?></div> </div> </div> </div> </form> </div> </div> </div> </div> Edited February 20, 2020 by mike3075 Quote Link to comment https://forums.phpfreaks.com/topic/310079-contact-form-working-but-email-not-sending-or-being-received/ Share on other sites More sharing options...
requinix Posted February 20, 2020 Share Posted February 20, 2020 There are very many reasons why an email may not be sent or received. Switch your code to using PHPMailer instead of mail(). That helps with a lot of those reasons. If you still aren't getting the email after that, then there could be a problem somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/310079-contact-form-working-but-email-not-sending-or-being-received/#findComment-1574616 Share on other sites More sharing options...
mike3075 Posted February 20, 2020 Author Share Posted February 20, 2020 Thank you for the quick response. I will give it a shot Quote Link to comment https://forums.phpfreaks.com/topic/310079-contact-form-working-but-email-not-sending-or-being-received/#findComment-1574617 Share on other sites More sharing options...
ginerjm Posted February 20, 2020 Share Posted February 20, 2020 If the 'from' address from the same domain as your site is running on? IIRC you can't use a 'foreign' from address outside of your domain. Quote Link to comment https://forums.phpfreaks.com/topic/310079-contact-form-working-but-email-not-sending-or-being-received/#findComment-1574620 Share on other sites More sharing options...
mike3075 Posted February 20, 2020 Author Share Posted February 20, 2020 It's not. I will change it and see what happens. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/310079-contact-form-working-but-email-not-sending-or-being-received/#findComment-1574621 Share on other sites More sharing options...
mike3075 Posted February 20, 2020 Author Share Posted February 20, 2020 Ginerjm, that worked! Thank you very much! requinix, I will create a test page using PHPMailer. Thank you both for your suggestions! Quote Link to comment https://forums.phpfreaks.com/topic/310079-contact-form-working-but-email-not-sending-or-being-received/#findComment-1574622 Share on other sites More sharing options...
ginerjm Posted February 20, 2020 Share Posted February 20, 2020 HTH! And - switching to phpmailer is a very smart move. The old PHP mail() function didn't really give you a good result when there were errors. PHPmailer is much better and much easier to use when you want to do different things with emails like adding attachments. Quote Link to comment https://forums.phpfreaks.com/topic/310079-contact-form-working-but-email-not-sending-or-being-received/#findComment-1574639 Share on other sites More sharing options...
Strider64 Posted February 20, 2020 Share Posted February 20, 2020 Another good 3rd party mailer is Swiftmailer and I found it easy to setup. Quote Link to comment https://forums.phpfreaks.com/topic/310079-contact-form-working-but-email-not-sending-or-being-received/#findComment-1574700 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.