Jump to content

Trying to get 1st PHP script to work (send email - form)


LifeInBinary

Recommended Posts

I have this in my XHTML:

<form method="post" action="contact.php"><span class="form">Email:  </span><input name="email" type="text" class="input-contact" /><br /><br /><span class="form">Message:</span>     <span class="form">Subject:  </span><select name="subject"><option value="applyclan">Apply for a Clan Page</option><option value="general">General Question/Comment</option><option value="technical">Technical Question/Comment</option><option value="report">Report a Typo/Error</option><option value="fanhate">Fan/Hate Mail</option><option value="file-submission">File Submission</option><option value="advertising">Advertising</option><option value="donation-financial">Donation - Financial</option><option value="donation-other">Donation - Other</option><option value="legal">Legal Inquiry/Notification</option><option value="other">Other (Please Specify)</option></select><br /><span class="small"><br /></span><textarea name="message" rows="10" cols="65" class="input-contact-2"></textarea><br /><input type="submit" /></form>

That is in the <body> of a page hosted on the internet.Here is the file, hosted in the same directory, named 'contact.php'

<?php  $email = $_REQUEST['email'];  $subject = $_REQUEST['subject'];  $message = $_REQUEST['message'];  mail( "my-email-address@domain.com", $subject,	$message, "From: $email" );  header( "Location: thank-you.html" );?>

When I hit the submit button, it seems to run the PHP file, and it even takes me to the 'thank-you.html' page - but I don't receive an email.What am I doing wrong?Thank you for your help,Life In Binary.

Link to comment
Share on other sites

Are you on a linux or windows server? Do you have the mail function enabled? Are you on shared or unique ip address (server wise)? Even though theres still things wrong with your script, you should get an email if the settings are correct.Ok..onto the mistakes/errors - Why are you using request? The method of the form is post, so your script should use $_POST And i would get a name from whoevers submitting the form. That way, when you reply, it feels a bit more personal. Rather than "hi random stranger who just emailed me", you can out "Hi Bob, thanks for your email etc etc"$name = $_POST['name'];$email = $_POST['email'];$message = $_POST['message'];I would also add an error page just in case the mail isn't sent. And also set the subject, but thats just my personal preferance. And turn your subject field into an "enquiry type", and stick that in the email body$to = "youremail@yourhouse.com"$subject = "website email";$headers = "From: " .$_POST['email'];$message .= "Name: " . $_POST['name'] . "\n"; $message .= "Email: " . $_POST['email'] . "\n";$message .="Enquiry: " . $_POST['enquiry'] . "\n"; // change 'enquiry' to whatever you call your enquiry/subject field.$message .= "Comments: " . $_POST['message'] . "\n";if (mail($to,$subject,$message,$headers)) { header('Location: thanks.php'); // success} else { header('Location: emailnotsent.php'); // errorWhen you get the form working, look into validating what your users put in, by limiting characters, stripping html tags etc etc..Hope that helps!:)

Link to comment
Share on other sites

Make sure you have a SMTP server on your webserver! Or else mail() simply won't work.

Link to comment
Share on other sites

If there's an error (like from any of the causes mentioned above), and you're not seeing it, you will want to enable error messages.ini_set("display_errors", 1);error_reporting(E_ALL);Also, some free email hosts like hotmail have problems with PHP email, it goes into the spam folder. Are you using a free email address for this?

Link to comment
Share on other sites

Oh guys thank you so much. Let me clarify some stuff I suppose:- I am using a script from a tutorial here (http://www.thesitewizard.com/archive/feedbackphp.shtml) so I didn't know there 'was' a POST lol. If anyone knows of a better way to start, let me know - the PHP tutorial here didn't seem to help much, it only kinda told me what 'could' be done - not how to do it lol.- I am sharing a server that someone else has generously given me FTP to, so I will have to ask him about those settings. Seems to me that I asked about PHP and MySQL once for a database, and he said yes to PHP, and that he only got one MySQL database and he was using it. So, more than that, I don't know much about the PHP settings.- About turning on error messages - THANK YOU, that is so helpful I'm assuming! Planning on doing that after I send this.@ Real Illusions - All of your suggestions were absolutely awesome, and I plan to incorporate them all - except I don't think I understand the 'enquiry' thing :/ I thought I did add a subject, but do you mean that since there are 'options' that there really kinda is no subject, so I need to change $subject to $enquiry then change every 'option' to $subject? I think I just confused both of us lol...And yes, I was planning on checking out validating here (http://www.thesitewizard.com/archive/phptutorial2.shtml) - but then again, that is the place who had me put REQUEST instead of POST - so should I maybe look somewhere else for that?@ Justsomeguy - I am using my personal Yahoo account just to test this out, but am planning on signing up an email address at Gmail for the website.Oh yeah, does anyone know how to get like email addresses @yoursitedomain.com - like an admin here could have 'admin@w3schools.com' ? Off topic, but I figured I might ask anyway. I know of Google Apps - but that confused me super bad lol - tried it for a different website, and I'm assuming there are plenty of other methods>

Link to comment
Share on other sites

@ Real Illusions - All of your suggestions were absolutely awesome, and I plan to incorporate them all - except I don't think I understand the 'enquiry' thing :/ I thought I did add a subject, but do you mean that since there are 'options' that there really kinda is no subject, so I need to change $subject to $enquiry then change every 'option' to $subject? I think I just confused both of us lol...And yes, I was planning on checking out validating here (http://www.thesitewizard.com/archive/phptutorial2.shtml) - but then again, that is the place who had me put REQUEST instead of POST - so should I maybe look somewhere else for that?
hehe..About the enquiry/subject thing..its actually very simple. In your form, change <select name="subject"> to <select name="enquiry">And then follow what i wrote in the php script, it should start to make sense :)As for form validation. Theres no set way to do it. Just the way you feel comfortable with. If you go to php.net and look up str_replace or ways to get rid of html, or even denying the use of certain characters. The site you looked at there, gives a good start i think to the validation. Work off it and see what you can come up with by looking php.net for guidance (although it may seem a little daunting at first):)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...