hatefulcrawdad Posted December 17, 2009 Share Posted December 17, 2009 WARNING!!!: I am a complete noob! I had a friend and some online tutorials help me make this form and it isn't sending. The only time it sends is if I put in the same email address as the form is set to send to in Email: input. I'm hoping its something small you guys can spot real quick. I am also hosting through Yahoo, so I know that might cause a problem maybe. All the php setting are on in my yahoo panel though. Here is the code: <?php $emailName = $_POST['Name']; $emailFrom = $_POST['Email']; $emailSubject = $_POST['Subject']; $emailMessage = "\n-----MESSAGE-----\n" . $_POST['Message']; if (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) { echo ''; } elseif (empty($emailSubject)) { echo 'You must enter a valid subject.'; } elseif (empty($emailMessage)) { echo 'You must enter a message to send to our team.'; } else { $emailTo = 'your@email.com'; $from = ''; if (!empty($emailFrom)) { $emailHeaders = 'From: "' . $emailName . '" <' . $emailFrom . '>'; } else { $emailHeaders = 'From: "Your Name" <your@email.com>'; } /* Send Email */ if (mail($emailTo, $emailSubject, $emailMessage, $emailHeaders)) { echo 'You message has been sent!'; } else { echo 'There was an internal error whilst sending your email.<br>'; echo 'Please try again later.'; } } ?> <form name="contact" method="post" action="index_test.php"> Your Name: <input type="text" name="Name"><br> Your Email: <input type="text" name="Email"><br> Subject: <input type="text" name="Subject"><br> Message:<br> <textarea name="Message" cols="30" rows="8"></textarea><br> <input type="submit" name="submit" value="Contact Us"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/ Share on other sites More sharing options...
hatefulcrawdad Posted December 17, 2009 Author Share Posted December 17, 2009 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-979497 Share on other sites More sharing options...
hatefulcrawdad Posted December 17, 2009 Author Share Posted December 17, 2009 no one loves me. Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-979533 Share on other sites More sharing options...
Deoctor Posted December 18, 2009 Share Posted December 18, 2009 try this one out <html> <body> <?php if (isset($_REQUEST['email'])) //if "email" is filled out, send email { //send email $email = $_REQUEST['email'] ; $subject = $_REQUEST['subject'] ; $message = $_REQUEST['message'] ; mail( "dr.virus.india@gmail.com", "Subject: $subject",$message, "From: $email" ); echo "Thank you for using our mail form"; } else //if "email" is not filled out, display the form { echo "<form method='post' action='mailform.php'> Email: <input name='email' type='text' /><br /> Subject: <input name='subject' type='text' /><br /> Message:<br /> <textarea name='message' rows='15' cols='40'> </textarea><br /> <input type='submit' /> </form>"; } ?> </body> </html> and one thing use the code tags Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-979669 Share on other sites More sharing options...
oni-kun Posted December 18, 2009 Share Posted December 18, 2009 no one loves me. :'( Has your code ever worked prior? The mail function relies on an SMTP server, try opening telnet and entering: telnet mail.yourdomain.com 25 or telnet smtp.yourdomain.com 25 and it should return if you can connect or not. Being Yahoo, I believe they should have documenation on how to get this to work, as usually it's not set up by default. Your code looks fine to me. Also.. since you're using Yahoo, your mailserver is shared, so your messages sent may be sent into the spam/bulk e-mail box. Check it there if you haven't. Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-979674 Share on other sites More sharing options...
hatefulcrawdad Posted December 18, 2009 Author Share Posted December 18, 2009 It does send now, no matter which email I send, so thank you so much. I just had to check on some setting for yahoo. Now when the form loads it starts with the "error" message above the form because nothing is in the form yet. How can I stop this? <?php $emailFromName = $_POST['Name']; $emailFrom = $_POST['Email']; $emailSubject = $_POST['Subject']; $emailMessage = "\n-----MESSAGE-----\n" . $_POST['Message']; if (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) { echo 'Error'; } elseif (empty($emailSubject)) { echo 'You must enter a valid subject.'; } elseif (empty($emailMessage)) { echo 'You must enter a message to send to our team.'; } else { $emailTo = 'cm@chrismdesign.com'; $from = ''; if (!empty($emailFrom)) { $emailHeaders = 'FROM: <cm@chrismdesign.com>'; } else { $emailHeaders = 'FROM: <cm@chrismdesign.com>'; } /* Send Email */ if (mail($emailTo, $emailSubject, $emailMessage, $emailHeaders)) { echo 'You message has been sent!'; } else { echo 'There was an internal error whilst sending your email.<br>'; echo 'Please try again later.'; } } ?> <form name="contact" method="post" action="index_test.php"> Your Name: <input type="text" name="Name"><br> Your Email: <input type="text" name="Email"><br> Subject: <input type="text" name="Subject"><br> Message:<br> <textarea name="Message" cols="30" rows="8"></textarea><br> <input type="submit" name="submit" value="Contact Us"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-979922 Share on other sites More sharing options...
emopoops Posted December 18, 2009 Share Posted December 18, 2009 jsut use the mail() function right after the varibales are posted and try to stay away from the $_GET make sure to keep using POST its more appropriate and dullfillig Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-979936 Share on other sites More sharing options...
hatefulcrawdad Posted December 18, 2009 Author Share Posted December 18, 2009 I don't think I was using the GET function was I? I didn't find it if I was. Anyway. The same problem exists now that I have moved the mail() function only now it says the error message and the message sent message by default. How can I have those not show until the form has been submitted? Another problem that just happened when I tried to get the form to pass the persons email and name through the message. Now none of the information gets through, just the titles I have written in. <?php $emailTo = 'cm@chrismdesign.com'; $emailName = $_POST['Name']; $emailFrom = $_POST['Email']; $emailSubject = $_POST['Subject']; $emailMessage = "\n-----MESSAGE-----\n" . $_POST['Message']; $emailMessage .= "\n-----NAME-----\n" . $_POST['Name']; $emailMessage .= "\n-----EMAIL-----\n" . $_POST['Email']; /* Send Email */ if (mail($emailTo, $emailSubject, $emailMessage, $emailHeaders)) { echo 'You message has been sent!'; } else { echo 'There was an internal error whilst sending your email.<br>'; echo 'Please try again later.'; } if (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) { echo 'Error'; } elseif (empty($emailSubject)) { echo 'You must enter a valid subject.'; } elseif (empty($emailMessage)) { echo 'You must enter a message to send to our team.'; } if (!empty($emailFrom)) { $emailHeaders = 'FROM: <cm@chrismdesign.com>'; } ?> <form name="contact" method="post" action="index_test.php"> Your Name: <input type="text" name="Name"><br> Your Email: <input type="text" name="Email"><br> Subject: <input type="text" name="Subject"><br> Message:<br> <textarea name="Message" cols="30" rows="8"></textarea><br> <input type="submit" name="submit" value="Contact Us"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-980013 Share on other sites More sharing options...
hatefulcrawdad Posted December 18, 2009 Author Share Posted December 18, 2009 Ignore my previous post. I got all the information to pass into the email now. And I moved the mail() function to the bottom again because for some reason it was sending emails each time I anyone visits the page. Like the initial load sends a blank email. I'm still getting the error message printed above the form on load though, and that isn't what I want to happen. I want nothing to show until the user enters the information and presses submit. Here is a url to my test site. www.chrismdesign.com/test/index_test.php Here is the code I'm currently using on that test page. <?php $emailTo = 'cm@chrismdesign.com'; $emailName = $_POST['Name']; $emailFrom = $_POST['Email']; $emailSubject = $_POST['Subject']; $emailMessage = "\n-----MESSAGE-----\n" . $_POST['Message']; $emailMessage .= "\n-----NAME-----\n" . $_POST['Name']; $emailMessage .= "\n-----EMAIL-----\n" . $_POST['Email']; if (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) { echo 'Error'; } elseif (empty($emailSubject)) { echo 'You must enter a valid subject.'; } elseif (empty($emailMessage)) { echo 'You must enter a message to send to our team.'; } else { if (!empty($emailFrom)) { $emailHeaders = 'FROM: <cm@chrismdesign.com>'; } /* Send Email */ if (mail($emailTo, $emailSubject, $emailMessage, $emailHeaders)) { echo 'You message has been sent!'; } else { echo 'There was an internal error whilst sending your email.<br>'; echo 'Please try again later.'; } } ?> <form name="contact" method="post" action="index_test.php"> Your Name: <input type="text" name="Name"><br> Your Email: <input type="text" name="Email"><br> Subject: <input type="text" name="Subject"><br> Message:<br> <textarea name="Message" cols="30" rows="8"></textarea><br> <input type="submit" name="submit" value="Contact Us"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-980059 Share on other sites More sharing options...
emopoops Posted December 18, 2009 Share Posted December 18, 2009 the headers need to actually b e before the mail() hon. jtlyk Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-980060 Share on other sites More sharing options...
hatefulcrawdad Posted December 18, 2009 Author Share Posted December 18, 2009 the headers need to actually b e before the mail() hon. jtlyk ??? Not sure I understand. Can you show me? Im kinda a noob. Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-980068 Share on other sites More sharing options...
emopoops Posted December 18, 2009 Share Posted December 18, 2009 the mail() has to be after the headers. Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-980087 Share on other sites More sharing options...
hatefulcrawdad Posted December 18, 2009 Author Share Posted December 18, 2009 the mail() has to be after the headers. <?php $emailTo = 'cm@chrismdesign.com'; $emailName = $_POST['Name']; $emailFrom = $_POST['Email']; $emailSubject = $_POST['Subject']; $emailMessage = "\n-----MESSAGE-----\n" . $_POST['Message']; $emailMessage .= "\n-----NAME-----\n" . $_POST['Name']; $emailMessage .= "\n-----EMAIL-----\n" . $_POST['Email']; if (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) { echo 'Error'; } elseif (empty($emailSubject)) { echo 'You must enter a valid subject.'; } elseif (empty($emailMessage)) { echo 'You must enter a message to send to our team.'; } else { /* Send Email */ if (mail($emailTo, $emailSubject, $emailMessage, $emailHeaders)) { echo 'You message has been sent!'; } else { echo 'There was an internal error whilst sending your email.<br>'; echo 'Please try again later.'; } } ?> if (!empty($emailFrom)) { $emailHeaders = 'FROM: <cm@chrismdesign.com>'; } Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-980107 Share on other sites More sharing options...
emopoops Posted December 18, 2009 Share Posted December 18, 2009 like i stated. u need to define the email headers variable before the if(mail()) Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-980112 Share on other sites More sharing options...
oni-kun Posted December 18, 2009 Share Posted December 18, 2009 What on earth are you guys doing? Try this code: <?php if (isset($_POST['Name'])){ $emailTo = 'cm@chrismdesign.com'; $emailName = $_POST['Name']; $emailFrom = $_POST['Email']; $emailSubject = $_POST['Subject']; $emailMessage = "\n-----MESSAGE-----\n" . $_POST['Message']; $emailMessage .= "\n-----NAME-----\n" . $_POST['Name']; $emailMessage .= "\n-----EMAIL-----\n" . $_POST['Email']; if (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) { echo 'Error'; } elseif (empty($emailSubject)) { echo 'You must enter a valid subject.'; } elseif (empty($emailMessage)) { echo 'You must enter a message to send to our team.'; } else { if (!empty($emailFrom)) { $emailHeaders = 'FROM: <cm@chrismdesign.com>'; } /* Send Email */ if (mail($emailTo, $emailSubject, $emailMessage, $emailHeaders)) { echo 'You message has been sent!'; } else { echo 'There was an internal error whilst sending your email.<br>'; echo 'Please try again later.'; } } } ?> <form name="contact" method="post" action="index_test.php"> Your Name: <input type="text" name="Name"><br> Your Email: <input type="text" name="Email"><br> Subject: <input type="text" name="Subject"><br> Message:<br> <textarea name="Message" cols="30" rows="8"></textarea><br> <input type="submit" name="submit" value="Contact Us"> </form> That'll prevent the code from running, if the page is first loaded (I.E. No POST data. ). Why on earth are you complaining about headers to be before mail, when it is, and giving invalid code? Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-980114 Share on other sites More sharing options...
emopoops Posted December 18, 2009 Share Posted December 18, 2009 like i stated earlier, headers before the mail thanks onibul Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-980118 Share on other sites More sharing options...
hatefulcrawdad Posted December 18, 2009 Author Share Posted December 18, 2009 like i stated. u need to define the email headers variable before the if(mail()) okay, doesn't this if (!empty($emailFrom)) { $emailHeaders = 'FROM: <cm@chrismdesign.com>'; } define it? and it did come before the mail() function before. Im sorry again, like I said, total php noob. Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-980121 Share on other sites More sharing options...
oni-kun Posted December 18, 2009 Share Posted December 18, 2009 like i stated. u need to define the email headers variable before the if(mail()) okay, doesn't this if (!empty($emailFrom)) { $emailHeaders = 'FROM: <cm@chrismdesign.com>'; } define it? and it did come before the mail() function before. Im sorry again, like I said, total php noob. Just use my code (on the previous page now), I'm not sure what he's on about. You set the headers correctly. Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-980122 Share on other sites More sharing options...
emopoops Posted December 18, 2009 Share Posted December 18, 2009 yeah the op doens tknow what she is doing. ur right onibhiu Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-980133 Share on other sites More sharing options...
oni-kun Posted December 18, 2009 Share Posted December 18, 2009 yeah the op doens tknow what she is doing. ur right onibhiu Yes he does, You're telling him to do what he already did, you're confusing him! Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-980135 Share on other sites More sharing options...
emopoops Posted December 18, 2009 Share Posted December 18, 2009 no..... that wasnt what he had in there! Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-980140 Share on other sites More sharing options...
oni-kun Posted December 18, 2009 Share Posted December 18, 2009 no..... that wasnt what he had in there! "and it did come before the mail() function before." He said it himself, that he had the header before mail(). You told him otherwise. Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-980143 Share on other sites More sharing options...
emopoops Posted December 18, 2009 Share Posted December 18, 2009 no. im pretty sure he modfied the ppst after i let him know. Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-980145 Share on other sites More sharing options...
oni-kun Posted December 18, 2009 Share Posted December 18, 2009 no. im pretty sure he modfied the ppst after i let him know. Yes, I'm sure he did just that Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-980151 Share on other sites More sharing options...
hatefulcrawdad Posted December 18, 2009 Author Share Posted December 18, 2009 What on earth are you guys doing? Try this code: That'll prevent the code from running, if the page is first loaded (I.E. No POST data. ). Why on earth are you complaining about headers to be before mail, when it is, and giving invalid code? Thanks man, I really appreciate it. I didn't understand what the other guy was trying to tell me. The only thing not working now is, if the user doesn't enter a message it is supposed to tell them "Please enter a message" but it just sends now. Not a huge deal because everthing else works. But if you can see why, that would be awesome. <?php if (isset($_POST['Name'])){ $emailTo = 'cm@chrismdesign.com'; $emailName = $_POST['Name']; $emailFrom = $_POST['Email']; $emailSubject = $_POST['Subject']; $emailMessage = "\n-----MESSAGE-----\n" . $_POST['Message']; $emailMessage .= "\n-----NAME-----\n" . $_POST['Name']; $emailMessage .= "\n-----EMAIL-----\n" . $_POST['Email']; if (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) { echo 'Please enter a valid email address'; } elseif (empty($emailName)) { echo 'Please enter your name'; } elseif (empty($emailSubject)) { echo 'Please enter a subject'; } elseif (empty($emailMessage)) { echo 'Please enter a message'; } else { if (!empty($emailFrom)) { $emailHeaders = 'FROM: <cm@chrismdesign.com>'; } /* Send Email */ if (mail($emailTo, $emailSubject, $emailMessage, $emailHeaders)) { echo 'You message has been sent!'; } else { echo 'There was an internal error whilst sending your email.<br>'; echo 'Please try again later.'; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/185522-problem-sending-email-with-php-form/#findComment-980161 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.