msugi002 Posted March 13, 2018 Share Posted March 13, 2018 Hello all, I am new to PHP and I am trying to find a successful way to send the contents of a form as an e-mail to an administrator e-mail address. I've followed a few tutorials on youtube, tried to connect things to each other using my prior knowledge on programming, and ended up with the following: (note: name of this file is "form.php", and I have changed information on this form for privacy reasons) <?php if (isset($_POST['submitButton'])) { require 'phpmailer/PHPMailerAutoload.php'; function sendemail($nm, $hp, $em, $mp, $ch, $mc){ //create an instance of PHP Mailer $mail = new PHPMailer(); //set a host $mail->host = "stmp.gmail.com"; //enable STMP //$mail->isSTMP() //note: this method may not always work. //$mail->SMTPDebug = 2; //set authentication to true $mail->STMPAuth = true; //Set login details for Gmail account. $mail->Username = "admin@company.com"; $mail->Password = "Password"; //Set types of protection $mail->STMPSecure = "ssl"; //or we can use TLS //set a port $mail->Port = 465; //or 587 if TLS //set content of e-mail $mail->setFrom(address: '$email', name: '$name'); $mail->addAddress(address: 'admin@company.com', name: 'Name of the Administrator'); $mail->Subject = 'New Contact'; $mail->Body = "You've recieved a new contact! <br /></br />" . $nm . "<br />" . $em . "<br />" . $hp . "<br />" . $mp . "<br />" . $ch . "<br />" . $mc; $mail->isHTML(true); //send an email return $mail->send(); } $name = $_POST['name']; $homephone = $_POST['homePhone']; $email = $_POST['email']; $mobilephone = $_POST['mobilePhone']; $children = $_POST['children']; $messagecontent = $_POST['messageContent']; return sendemail($name, $homephone, $email, $mobilephone, $children, $messagecontent)); if (sendemail($name, $homephone, $email, $mobilephone, $children, $messagecontent)){ $msg = "Mail was sent successfully"; else $msg = "Woops, something went wrong. Please Try Again"; }; }?><!DOCTYPE html><html> <head> <title>Signup Form</title> </head> <body> <form action="form.php" method="post"> <label>Name:</label> <input type="text" name="name" /> <label>Home Phone:</label> <input type="text" name="homePhone" /> <label>Email:</label> <input type="text" name="email" /> <label>Mobile Phone:</label> <input type="text" name="mobilePhone" /> <label> Names of Children:</label> <input id="namesOfChildren" type="text" name="children" /> <label>Message:</label> <textarea name="messageContent"></textarea> <input id="submit" type="submit" name="submitButton" value="Send" /> <? echo $msg ?> </form> </body></html> Now I am just seeing this at the top of the screen everytime I refresh the form, but it wont let me send the form: new contact!" . $nm . "" . $em . "" . $hp . "" . $mp . "" . $ch . "" . $mc; $mail->isHTML(true); //send an email return $mail->send(); } $name = $_POST['name']; $homephone = $_POST['homePhone']; $email = $_POST['email']; $mobilephone = $_POST['mobilePhone']; $children = $_POST['children']; $messagecontent = $_POST['messageContent']; return sendemail($name, $homephone, $email, $mobilephone, $children, $messagecontent)); if (sendemail($name, $homephone, $email, $mobilephone, $children, $messagecontent)){ $msg = "Mail was sent successfully"; else $msg = "Woops, something went wrong. Please Try Again"; }; } ?> I'm wondering if anybody could help me. If you know any videos online that are helpful, I would appreciate it. Quote Link to comment Share on other sites More sharing options...
requinix Posted March 14, 2018 Share Posted March 14, 2018 It means you don't have PHP set up correctly. It must be installed appropriately for whatever web server software you're using (such as Apache or nginx), the file needs to be named with the .php extension, and your browser has to say "http://" in the address bar (unless you're using Chrome, which won't show you that part even though it is there). Quote Link to comment Share on other sites More sharing options...
msugi002 Posted March 14, 2018 Author Share Posted March 14, 2018 Thank you for your response, I have PHP downloaded on my computer, and I downloaded XAMPP, turned it on, and enabled the network and services (Apache included). I tested it out on a website I already have up, but now it just shows all of my e-mail info in the website (which I immediately changed). The submit button no longer sends me to an error page, but it didn't do anything. Does this mean I have to inquire with the hosting service I am using to see how I can fix this? Quote Link to comment Share on other sites More sharing options...
requinix Posted March 14, 2018 Share Posted March 14, 2018 You probably don't need to talk to your hoster. Not yet. What do you mean it just "shows all your email info"? If the submit button doesn't send you to an error page, what do you end up at? Quote Link to comment Share on other sites More sharing options...
msugi002 Posted March 14, 2018 Author Share Posted March 14, 2018 (edited) What I meant by that is that I used $mail->Username and $mail->Password to put in my e-mail and password. And all that info just popped up at the top of my website. I fixed it though, so it doesn't do that anymore. After changing the code some more, the form seems to be functioning, but now it only gives me the "Woops, something went wrong. Please Try Again" validation everytime I try sending it: <?php if (isset($_POST['submitButton'])) { require 'phpmailer/PHPMailerAutoload.php'; function sendemail($nm, $hp, $em, $mp, $ch, $mc){ //create an instance of PHP Mailer $mail = new PHPMailer(); $mail->host = "stmp.gmail.com"; //enable STMP //$mail->isSTMP(); $mail->STMPAuth = true; $mail->Username = "redacted"; $mail->Password = "redacted"; $mail->STMPSecure = "ssl"; //or we can use TLS $mail->Port = 465; //or 587 if TLS //set content of e-mail $mail->setFrom($em, $nm); $mail->addAddress('redacted', 'Name of the Administrator'); $mail->Subject = 'New Contact'; $mail->Body = "You've received a new contact! <br /></br />" . $nm . "<br />" . $em . "<br />" . $hp . "<br />" . $mp . "<br />" . $ch . "<br />" . $mc; $mail->isHTML(true); $mail->send(); } $name = $_POST['name']; $homephone = $_POST['homePhone']; $email = $_POST['email']; $mobilephone = $_POST['mobilePhone']; $children = $_POST['children']; $messagecontent = $_POST['messageContent']; if (empty($name) || empty($homephone) || empty($email) || empty($mobilephone) || empty($children) || empty($messagecontent)){ $msg = "Woops, something went wrong. Please Try Again"; } else{ $msg = "Mail was sent successfully"; return sendemail($name, $homephone, $email, $mobilephone, $children, $messagecontent); }; } Edited March 14, 2018 by cyberRobot please use [code][/code] tags Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted March 14, 2018 Share Posted March 14, 2018 ...but now it only gives me the "Woops, something went wrong. Please Try Again" validation everytime I try sending it: Did you check all your form variables to make they are not empty? You could start by displaying the $_POST variable. <?php //... $children = $_POST['children']; $messagecontent = $_POST['messageContent']; print '<pre>' . print_r($_POST, true) . '</pre>'; if (empty($name) || empty($homephone) || empty($email) || empty($mobilephone) || empty($children) || empty($messagecontent)){ //... ?> If the POST array looks good, check the individual variables ($name, $homephone, etc.) to make sure the values were assigned correctly. For example: $messagecontent = $_POST['messageContent']; var_dump($messagecontent); Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted March 14, 2018 Share Posted March 14, 2018 Side note: the <label> tags are set up incorrectly. They need to surround both the label and the input field. <label>Name: <input type="text" name="name" /></label> ...or you can use the <label> tag's "for" attribute and the input field's "id" attribute to establish a partnership. More information can be found here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label There's an easy way to tell if the <label> tag is working. Clicking the field label should place the cursor in the corresponding form field. Quote Link to comment Share on other sites More sharing options...
Solution msugi002 Posted March 14, 2018 Author Solution Share Posted March 14, 2018 Hello, Thank you for your feedback. I checked to make sure that all the Variables were not empty. The form started working when I put values in the form, other than just zeroes. I then used Javascript so that the values will be displayed and sent in a way that will work best for the form (i.e. putting the dashes in the phone number). After working on it more I got it to finally work. Thank you! Also thank you for letting me know about the label tags. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted March 15, 2018 Share Posted March 15, 2018 The form started working when I put values in the form, other than just zeroes. Have you considered using something other than the empty() function when testing the values? You will want to try something different for the number of children since "0" should be an acceptable answer. For example, you could do something like this: if ($name=='' || $homephone=='' || $email=='' || $mobilephone=='' || $children=='' || $messagecontent==''){ Better yet, you could generate a list of errors based on the fields left blank. If there are errors, re-display the form (populated with the user's previous responses) and let the user know which fields are missing. Quote Link to comment 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.