Xyth Posted September 29, 2012 Share Posted September 29, 2012 (edited) Hello, I posted a topic herebut is not getting much response so am posting a similar question here instead... Basically, I have essentially no experience with or knowledge about PHP - that said I am trying to create a simple form which, when uploaded to a website I am designing, will send an email to me with the contents of the form. Shouldn't be too difficult right? I am currently working the best I can from various pieces of example code I've found online. I have come up with 2 files - form.php and senddetails.php, the code of which I have below: <html></html><form enctype="multipart/form-data" method="post" action="senddetails.php"> <div style="text-align: center"> <input type="hidden" name="required_vars" id="required_vars" value="name,email"> </div> <table cellspacing="5" cellpadding="5" border="0" align="center"> <tr> <td valign="top" style="text-align: center"> <strong>Your Name:</strong> </td> <td valign="top"> <input type="text" name="name" id="name" size="40" value=""> </td> </tr> <tr> <td valign="top" style="text-align: center"> <strong>Address line 1:</strong> </td> <td valign="top"> <input type="text" name="address1" id="address1" size="40" value=""> </td> </tr> <tr> <td valign="top" style="text-align: center"> <strong>Address line 2:</strong> </td> <td valign="top"> <input type="text" name="address2" id="address2" size="40" value=""> </td> </tr> <tr> <td valign="top" style="text-align: center"> <strong>Town:</strong> </td> <td valign="top"> <input type="text" name="town" id="town" size="40" value=""> </td> </tr> <tr> <td valign="top" style="text-align: center"> <strong>County or State:</strong> </td> <td valign="top"> <input type="text" name="countystate" id="countystate" size="40" value=""> </td> </tr> <tr> <td valign="top" style="text-align: center"> <strong>Postcode:</strong> </td> <td valign="top"> <input type="text" name="postcode" id="postcode" size="40" value=""> </td> </tr> <tr> <td valign="top" style="text-align: center"> <strong>Message:</strong> </td> <td valign="top"> <textarea name="persmes" id="persmes" rows="6" cols="40"></textarea> </td> </tr> <tr> <td valign="top" style="text-align: center"> <strong>Your email address:</strong> </td> <td valign="top"> <input type="text" name="email" id="email" size="40" value=""> </td> </tr> <tr> <td colspan="2" style="text-align: center"> <input type="submit" value=" Submit Form "> </td> </tr> </table> </form></HTML> <?php $ToEmail = 'postmaster@localhost'; $EmailSubject = 'Details'; $mailheader = "From: ".$_POST["email"]."\r\n"; $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; $MESSAGE_BODY = "Name: ".$_POST["name"].""; $MESSAGE_BODY = "Address Line 1: ".$_POST["address1"].""; $MESSAGE_BODY = "Address Line 2: ".$_POST["address2"].""; $MESSAGE_BODY = "Town name: ".$_POST["town"].""; $MESSAGE_BODY = "County/state: ".$_POST["countystate"].""; $MESSAGE_BODY = "Postcode: ".$_POST["postcode"].""; $MESSAGE_BODY = "Message: ".$_POST["persmes"].""; $MESSAGE_BODY .= "Email: ".$_POST["email"].""; mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); ?> OK, so the code above should work right?? Is there anything obvious wrong with it? I have been trying to test it out using XAMPP and Thunderbird to send an email to myself locally but am starting to realise this may not be possible. Would it be a simpler test just to upload it a suitable domain? Or, can I just test it out with a gmail account, for example? If anyone can point me in the direction of any help with setting up Mercury, or perhaps just explain it to me in simple terms, that would be very much appreciated. I realise this question extends a little outside the scope of PHP coding alone but nonetheless I hope some of you will have enough experience doing what I'm trying to do to help me out. One final question also - is there a simple way to redirect the user to another page confirming that their details have been sent? I'm guessing this is probably much simpler to answer than the above questions. Edited September 29, 2012 by Xyth Quote Link to comment Share on other sites More sharing options...
berridgeab Posted September 29, 2012 Share Posted September 29, 2012 Testing locally requires a mailserver setting up which is out of the scope of this post, though there are examples online. I find it a pain to setup correctly so instead I test emails on my live production server. I can't see any problems with the above code so it should work ok. Quote Link to comment Share on other sites More sharing options...
berridgeab Posted September 29, 2012 Share Posted September 29, 2012 (edited) One final question also - is there a simple way to redirect the user to another page confirming that their details have been sent? I'm guessing this is probably much simpler to answer than the above questions Sure you can do a redirect by $ToEmail = 'postmaster@localhost'; $EmailSubject = 'Details'; $mailheader = "From: ".$_POST["email"]."\r\n"; $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; $MESSAGE_BODY = "Name: ".$_POST["name"].""; $MESSAGE_BODY = "Address Line 1: ".$_POST["address1"].""; $MESSAGE_BODY = "Address Line 2: ".$_POST["address2"].""; $MESSAGE_BODY = "Town name: ".$_POST["town"].""; $MESSAGE_BODY = "County/state: ".$_POST["countystate"].""; $MESSAGE_BODY = "Postcode: ".$_POST["postcode"].""; $MESSAGE_BODY = "Message: ".$_POST["persmes"].""; $MESSAGE_BODY .= "Email: ".$_POST["email"].""; if(mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader)) { header("Location: replaceWithFilename.php"); } Alternatively you could just keep the success / fail msg in the same page. if(mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader)) { echo "Success"; } else { echo "Failure"; } Edited September 29, 2012 by berridgeab Quote Link to comment Share on other sites More sharing options...
Xyth Posted September 29, 2012 Author Share Posted September 29, 2012 Thanks for the response! Actually I seem to have managed to set up a local mail server after much headache - I am using XAMPP and ArGosoft Mail Server, and having configured Mozilla Thunderbird with my postmaster@localhost account, I seem to be able to send an email to myself, which I wasn't able to before, so I assume this means it is working... However, my form still doesn't seem to do anything. I click on Submit Form and just get a blank page thrown up at me, with no email message in my inbox... is there anything else I could be missing here? Quote Link to comment Share on other sites More sharing options...
Xyth Posted September 29, 2012 Author Share Posted September 29, 2012 (edited) FWIW I think I have configured both php.ini and sendmail.ini properly... In php.ini, I have set: SMTP = localhost smtp_port = 25 ...and in sendmail.ini: smtp_server=localhost smtp_port=25 auth_username=postmaster auth_password=mypassword hostname=localhost I don't think I've made any other changes. Also, I must confess that since, as I said, I've basically been working from example code I've found online, I'm not entirely sure what this mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); ?> or this if(mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader)) { echo "Success"; } else { echo "Failure"; } ...is supposed to do. Am I supposed to be getting some sort of confirmation message on screen when I press "Submit"? Because as I said, I'm not getting anything, although senddetails.php seems to be loading, all I see is a blank page. Edited September 29, 2012 by Xyth Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 29, 2012 Share Posted September 29, 2012 senddetails.php doesn't output anything. If you've added that if/else block, it should. If you see nothing then, you need to enable error reporting. Quote Link to comment Share on other sites More sharing options...
Xyth Posted September 29, 2012 Author Share Posted September 29, 2012 Ah OK I see. Simple enough. Nonetheless it is still reporting success but I am not getting any mail in my localhost inbox... Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 29, 2012 Share Posted September 29, 2012 If you send mail to that address from an outside address does it arrive? Quote Link to comment Share on other sites More sharing options...
Xyth Posted September 29, 2012 Author Share Posted September 29, 2012 Surely it wouldn't be possible to send mail to postmaster@localhost from anywhere other than the host server...? I can send an email to myself (ie, postmaster@localhost) through the same account, and receive it in my inbox... Quote Link to comment Share on other sites More sharing options...
berridgeab Posted September 29, 2012 Share Posted September 29, 2012 Tbh it sounds like a problem with the configaration of your local mailserver. As I said its a pain to configure and get working properly locally. It should work live if you upload to your production server. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 30, 2012 Share Posted September 30, 2012 Yeah I agree you should just try it on a live server. 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.