kross Posted July 13, 2013 Share Posted July 13, 2013 Hello everyone.. I know next to nothing about PHP but I'm trying to set up a mail form on a html web page. The code I've used should work, but for some reason the email wont send. When I hit send, the message pops up saying that it sent, but it doesnt send. What am I missing or doing wrong? Here's the code <?php function spamcheck($field) { //filter_var() sanitizes the e-mail //address using FILTER_SANITIZE_EMAIL $field=filter_var($field, FILTER_SANITIZE_EMAIL); //filter_var() validates the e-mail //address using FILTER_VALIDATE_EMAIL if(filter_var($field, FILTER_VALIDATE_EMAIL)) { return TRUE; } else { return FALSE; } } if (isset($_REQUEST['email'])) {//if "email" is filled out, proceed //check if the email address is invalid $mailcheck = spamcheck($_REQUEST['email']); if ($mailcheck==FALSE) { echo "Invalid input"; } else { //send email $name = $_REQUEST['name'] ; $email = $_REQUEST['email'] ; $subject = $_REQUEST['subject'] ; $message = $_REQUEST['message'] ; mail("someone@something.com", "Subject: $subject", $message, "From: $email" ); echo "Sent. Thank you!"; } } else //if "email" is not filled out, display the form { echo "<form method='post' action='contact.php'> What's your name? <br /> <input name='name' type='text' required /> <br /> What's your Email address? <br /> <input name='email' type='text' required> <br /> What is the matter regarding? <br /> <input name='subject' type='text' required> <br /> What is your message? <br /> <textarea name='message' rows='15' cols='50' required></textarea> <br /> <br /> <input type='submit' value='Send'> </form>"; } ?> Keep in mind that the someone@something.com is not the email I tried sending it to. I've tried multiple email accounts I have on yahoo and gmail with no luck. And yes, the file is saved in a folder on a server that has php installed. Thank you for your time!!! Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 13, 2013 Share Posted July 13, 2013 If you are trying to learn from what you are doing, great, I'm on my phone a it's hard to read your code right now. Otherwise i distribute a fully functional contact form here http://amecms.com/article/Easy-to-use-contact-form-with-validation that you can use Quote Link to comment Share on other sites More sharing options...
kross Posted July 13, 2013 Author Share Posted July 13, 2013 yeah I'd like to know why my code doesnt work... but thank you! I appreciate it Quote Link to comment Share on other sites More sharing options...
Coreye Posted July 13, 2013 Share Posted July 13, 2013 Have you switched hosts recently? Try the below code on your server: <?php if(mail("someone@example.com", "Subject: $subject", $message, "From: $email")) { echo "Email Sent."; } else { echo "Email NOT Sent."; } ?> Quote Link to comment Share on other sites More sharing options...
kross Posted July 13, 2013 Author Share Posted July 13, 2013 I'm sorry, but was I supposed to plug that into my existing code or run that by itself essentially? I plugged it into mine and I think I'm getting a syntax error because the page is completely blank when I load it. This is what I did <?php function spamcheck($field) { //filter_var() sanitizes the e-mail //address using FILTER_SANITIZE_EMAIL $field=filter_var($field, FILTER_SANITIZE_EMAIL); //filter_var() validates the e-mail //address using FILTER_VALIDATE_EMAIL if(filter_var($field, FILTER_VALIDATE_EMAIL)) { return TRUE; } else { return FALSE; } } if(isset($_REQUEST['email'])) {//if "email" is filled out, proceed //check if the email address is invalid $mailcheck = spamcheck($_REQUEST['email']); if ($mailcheck==FALSE) { echo "Invalid input"; } else { //send email $name = $_REQUEST['name'] ; $email = $_REQUEST['email'] ; $subject = $_REQUEST['subject'] ; $message = $_REQUEST['message'] ; if(mail("someone@example.com", "Subject: $subject", $message, "From: $email" )) { echo "Message sent. Thank you!"; } else { echo "Message Not sent. Sorry."; } else //if "email" is not filled out, display the form { echo "<form method='post' action='contact.php'> What's your name? <br /> <input name='name' type='text' required /> <br /> What's your Email address? <br /> <input name='email' type='text' required> <br /> What is the matter regarding? <br /> <input name='subject' type='text' required> <br /> What is your message? <br /> <textarea name='message' rows='10' cols='25' required></textarea> <br /> <br /> <input type='submit' value='Send'> </form>"; } ?> Quote Link to comment Share on other sites More sharing options...
Coreye Posted July 13, 2013 Share Posted July 13, 2013 Run it separately. Quote Link to comment Share on other sites More sharing options...
kross Posted July 13, 2013 Author Share Posted July 13, 2013 just that code by itself in its own file? Quote Link to comment Share on other sites More sharing options...
Coreye Posted July 13, 2013 Share Posted July 13, 2013 Yes. Quote Link to comment Share on other sites More sharing options...
kross Posted July 13, 2013 Author Share Posted July 13, 2013 (edited) It returned "Email Sent" but I didnt recieve an email. I even waited a few minutes I checked spelling, resent, and tried different addresses, but still wont work Edited July 13, 2013 by kross Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 13, 2013 Share Posted July 13, 2013 Is the server on a paid hosting account or localhost? Quote Link to comment Share on other sites More sharing options...
kross Posted July 13, 2013 Author Share Posted July 13, 2013 (edited) its a server provided to me by my school it has my own login and is password protected Edited July 13, 2013 by kross Quote Link to comment Share on other sites More sharing options...
kross Posted July 13, 2013 Author Share Posted July 13, 2013 if there actually is something wrong with my server, what kind of tests or actions can I perform to find out for sure? Quote Link to comment Share on other sites More sharing options...
Solution fastsol Posted July 13, 2013 Solution Share Posted July 13, 2013 The script test that Coreye gave was the most common way to test a mail(). If the mail never arrives it would indicate only a couple things. One, even though mail() is available on your server doesn't mean they have a mail service setup, to know that you would need to ask them or see if they have sample code of how to send mail on their servers. Two, their mail service is being over strict and marking any of your attempts as spam. Have you checked your spam forlder by chance to make sure they didn't arrive there instead? If they are in you spam folder by accident, you may really want to consider using the phpmailer library from http://phpmailer.worxware.com/ cause they have all the mail headers setup correctly and many other features you can do easily. That will help ensure "better" that your mail arrives in the inbox and not spam folders. Quote Link to comment Share on other sites More sharing options...
kross Posted July 14, 2013 Author Share Posted July 14, 2013 I figured it out... Using PuTTy, I logged into my server and a message popped up saying that new "security" feature was added to the servers and that email can only be sent to addresses provided by school. no personal email addresses are allowed anymore. Thanks for your help guys! 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.