SteveH Posted January 18, 2010 Share Posted January 18, 2010 Hello This has to be one of the simplest PHP scripts I have seen (a contact form). It has two simple files: <HTML> <HEAD> </HEAD> <BODY> <form action="/cf.php" method="post"> <p><label for="input-email">Your Email: </label><br/><input type="text" name="email" id="input-email" size="40"/></p> <p><label for="input-message">Your Message: </label><br/><textarea type="text" name="message" id="input-message" rows="10" cols="40"></textarea></p> <p><button type="submit" name="submit">Send</button></p> </form> </BODY> </HEAD> and the PHP processing script called cf.php: <?php if (isset($_GET['success'])) { ?><p><span style="color:green;">Message successfully sent.</span> Thank you!</p><?php } else { if (!empty($_POST['email']) && !empty($_POST['message'])) { $to = '[email protected]'; $subject = 'Contact from my_site.com'; $message = $_POST['message']."\r\n\r\nSender IP: ".$_SERVER["REMOTE_ADDR"]; $headers = 'From: '.$_POST['email']."\r\n". 'Reply-To: '.$_POST['email']."\r\n"; mail($to, $subject, $message, $headers); header("Location: /cf.php?success"); } else { ?><p><span style="color:red;">Error detected.</span> Please make sure you have filled all fields. Press back button to go back.</p><?php } } ?> Can I ask, please, what might be the reason why emails are not sent (despite the 'success' message). Thanks Steve Quote Link to comment https://forums.phpfreaks.com/topic/188934-simple-email-issue/ Share on other sites More sharing options...
ShadowIce Posted January 18, 2010 Share Posted January 18, 2010 You would need an email server setup in order to use the mail() function. Been there, done that. Best thing to do would be to buy a .com address that actually has that mail server built into it. That's what I did, and I'll tell ya man, it was the BEST move I've made so far =) Quote Link to comment https://forums.phpfreaks.com/topic/188934-simple-email-issue/#findComment-997594 Share on other sites More sharing options...
SteveH Posted January 18, 2010 Author Share Posted January 18, 2010 Hello Shadowice Thanks for your reply. I do have a proper domain name and hosting service with an email server. So, given the above, why would the script I have posted not work? Thanks. Steve Quote Link to comment https://forums.phpfreaks.com/topic/188934-simple-email-issue/#findComment-997605 Share on other sites More sharing options...
ShadowIce Posted January 18, 2010 Share Posted January 18, 2010 Did you echo all ur variables to be SURE that was the problem? Quote Link to comment https://forums.phpfreaks.com/topic/188934-simple-email-issue/#findComment-997620 Share on other sites More sharing options...
SteveH Posted January 18, 2010 Author Share Posted January 18, 2010 I didn't Shadowice, how would I do that, please? Thanks. Steve PD: It wouldn't be related to permissions, would it? Quote Link to comment https://forums.phpfreaks.com/topic/188934-simple-email-issue/#findComment-997624 Share on other sites More sharing options...
oni-kun Posted January 18, 2010 Share Posted January 18, 2010 If I'm not mistaken: if (isset($_GET['success'])) { //... header("Location: /cf.php?success"); Will cause an infinite loop, You are saying 'IF success = true, go to ?success', and it will go 'if success=true, go to ?success'. And no, Permissions will not be relevant to mail() unless your MX records for your domain are not set up (May require contact to support department to check) Quote Link to comment https://forums.phpfreaks.com/topic/188934-simple-email-issue/#findComment-997627 Share on other sites More sharing options...
ShadowIce Posted January 18, 2010 Share Posted January 18, 2010 <?php if (isset($_GET['success'])) { ?><p><span style="color:green;">Message successfully sent.</span> Thank you!</p><?php } else { if (!empty($_POST['email']) && !empty($_POST['message'])) { $to = '[email protected]'; $subject = 'Contact from my_site.com'; $message = $_POST['message']."\r\n\r\nSender IP: ".$_SERVER["REMOTE_ADDR"]; $headers = 'From: '.$_POST['email']."\r\n". 'Reply-To: '.$_POST['email']."\r\n"; mail($to, $subject, $message, $headers); header("Location: /cf.php?success"); } else { ?><p><span style="color:red;">Error detected.</span> Please make sure you have filled all fields. Press back button to go back.</p><?php } } echo "isset::Success: ".isset($_GET['success'])."<br>\n"; echo "Success: ".$_GET['success']."<br>\n"; echo "Email: ".$_POST['email']."<br>\n"; echo "Message: ".$_POST['message']."<br>\n"; echo "To: ".$to."<br>\n"; echo "Subject: ".$subject."<br>\n"; echo "Message: ".$message."<br>\n"; echo "Headers: ".$headers."<br>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/188934-simple-email-issue/#findComment-997629 Share on other sites More sharing options...
SteveH Posted January 18, 2010 Author Share Posted January 18, 2010 Thanks for your replies again! OK, I have deleted everything from my cf.php file and copied and pasted exactly what Shadowice posted here (but inserted the my correct email addresses). The form, incidentally, is here: http://proofreading4students.com/emailTest.html Before I came to this forum, I deliberately typed in a wrong email address (missing the @ etc) and I was told to press the back button. Now, with ShadowIce's script, I do not get that - so it is not picking up on errors. But I do not get any server messages or on screen errors. Thanks. Steve Quote Link to comment https://forums.phpfreaks.com/topic/188934-simple-email-issue/#findComment-997646 Share on other sites More sharing options...
ShadowIce Posted January 18, 2010 Share Posted January 18, 2010 did u try turning on error_reporting(E_ALL) ? Quote Link to comment https://forums.phpfreaks.com/topic/188934-simple-email-issue/#findComment-997658 Share on other sites More sharing options...
Genesis730 Posted January 19, 2010 Share Posted January 19, 2010 Maybe I know what I'm talking about, maybe I don't... but shouldn't it look like this? <form action="cf.php" method="post"> (Without the /) Quote Link to comment https://forums.phpfreaks.com/topic/188934-simple-email-issue/#findComment-997790 Share on other sites More sharing options...
oni-kun Posted January 19, 2010 Share Posted January 19, 2010 Maybe I know what I'm talking about, maybe I don't... but shouldn't it look like this? <form action="cf.php" method="post"> (Without the /) / Means document root, so if it is not then for compatibility it should be removed. A valid example would be /usr/home/htdocs/folder/cf.php Quote Link to comment https://forums.phpfreaks.com/topic/188934-simple-email-issue/#findComment-997794 Share on other sites More sharing options...
SteveH Posted January 19, 2010 Author Share Posted January 19, 2010 Hello Many thanks for your posts again. I have removed this: echo "isset::Success: ".isset($_GET['success'])."<br>\n";echo "Success: ".$_GET['success']."<br>\n";echo "Email: ".$_POST['email']."<br>\n";echo "Message: ".$_POST['message']."<br>\n";echo "To: ".$to."<br>\n";echo "Subject: ".$subject."<br>\n";echo "Message: ".$message."<br>\n";echo "Headers: ".$headers."<br>\n"; and whether I use: <form action="[b]/[/b]cf.php" method="post"> or <form action="cf.php" method="post"> the 'success' screen tells me the message is sent, but it is not received. Aaahh! Steve Quote Link to comment https://forums.phpfreaks.com/topic/188934-simple-email-issue/#findComment-998055 Share on other sites More sharing options...
Buddski Posted January 19, 2010 Share Posted January 19, 2010 You are doing the header() call all the time as long as the $_POST['email'] and $_POST['message'] are not empty.. You need to actually TEST the mail function itself... if (mail($to, $subject, $message, $headers)) { header("Location: /cf.php?success"); } else { header("Location: /cf.php?failed"); } Quote Link to comment https://forums.phpfreaks.com/topic/188934-simple-email-issue/#findComment-998064 Share on other sites More sharing options...
SteveH Posted January 19, 2010 Author Share Posted January 19, 2010 Hello I have done that, Buddski: //mail($to, $subject, $message, $headers); //header("Location: /cf.php?success"); // redirects the sender to success page //} else { if (mail($to, $subject, $message, $headers)) { header("Location: /cf.php?success"); } else {header("Location: /cf.php?failed"); } I have completed the simple form and was redirected to the 'success' page after clicking on 'submit'. The form details should be sent to my own personal address at Yahoo!, but they are not (they are not in my 'Spam' folder, either. Steve Quote Link to comment https://forums.phpfreaks.com/topic/188934-simple-email-issue/#findComment-998084 Share on other sites More sharing options...
Buddski Posted January 19, 2010 Share Posted January 19, 2010 You may need have some server settings that need adjusting or your host may not accept the use of the mail() function. (I had that with my old host, it would only allow authenticated mail to be sent) And as the PHP Manual says Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise. It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination. Quote Link to comment https://forums.phpfreaks.com/topic/188934-simple-email-issue/#findComment-998090 Share on other sites More sharing options...
SteveH Posted January 19, 2010 Author Share Posted January 19, 2010 Hello Buddski Thanks again. I'll contact the Web hosting service - I'm very tempted to go back to ASP and Windows!! Steve Quote Link to comment https://forums.phpfreaks.com/topic/188934-simple-email-issue/#findComment-998110 Share on other sites More sharing options...
Buddski Posted January 19, 2010 Share Posted January 19, 2010 There are alternatives.. Which are a little more indepth but I have never had an issue with.. PEAR::Mail is a good mail script as it uses an actual SMTP host to send emails.. Quote Link to comment https://forums.phpfreaks.com/topic/188934-simple-email-issue/#findComment-998113 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.