Jump to content
xisto Community
Sign in to follow this  
Dyth

Form Mail Php - Use Php To Send Form Through Email

Recommended Posts

I'm not good at JavaScript - you might be able to find lots of them at HotScripts.Com or javascript.internet.com - but here's an easy alternative. I've got a ready-to-run PHP Script that does exactly the same thing. Take a look at it - the comments inside the script will guide you through what to do/what not to do. Make sure you read all the comments properly. Otherwise this is an easy 3 step process:

Step 1: Design and put a form on your webpage

Step 2: Enter your correct email address and post_page link below (see inside script)

Step 3: Upload the form file & the script file to your webspace

N.B. Make sure the name of the script file is in exactly the same character case as specified in the FORM ACTION="scriptname" section.

 

<?php/*************************************************************Step 1:Create a form on your web-page with whatever fields are needed. It should use POST action tosend the form input to this php script. Your form MUST contain two fields named "Name" & "Email" - the rest can be whatever you like. These two fields should be titled as I stated for the scriptto be able to extract the name and email of the user. See example here. <form action="sendformtoemail.php" method="post"><table><tr>............ Form content goes here. Typical form would contain Name, Email & Comments ............ Fields & Submit and Clear Buttons.<td>Name:</td><td><input type="text" size="40" name="Name"></td></tr><tr>Email address:</td><td><input type="text" size="40" name="Email"></td></tr><tr>............ Rest of fields</tr></table></form>**************************************************************//*************************************************************Step 2:Enter the email address to which the form will be mailed:**************************************************************/$email_address = "put your email address here between quotes";/*************************************************************The post_action varibale contain a link to which users are re-directed after successfulsubmission of the form. If you don't want any other page to load leave it to "/"**************************************************************/$post_page = "/";/*************************************************************Step 3:Save this file as "sendformtoemail.php" upload it together with your webpage into the samedirectory as your Form page.  **************************************************************/// ***********************************************************// CRITICAL REGION// DO NOT EDIT THE CODE BELOW THIS UNLESS YOU KNOW WHAT YOU'RE DOINGif ($_SERVER['REQUEST_METHOD'] != "POST"){	exit;}// This loops through each line of the post and determines if any field has been left blank.// If so that field's contents are not includedwhile ( list ( $field, $value ) = each ( $_POST ) ){ 	if ( ! ( empty ( $value ) ) ) 	{   $notempty = 1;	}		$message = $message . "$field: $value\n\n"; } // If field is blank go back to referer URl, i.e. the form's pageif ( $notempty !== 1 ){	header ( "location: $_SERVER[HTTP_REFERER]" );	exit;}// This line extracts the email address of the submitter from the form. This is why I emphasized// on being particular in naming the Name & Email text-fields.$Email = stripslashes ( $_POST['Email'] );// Set subject of email, associated header and re-format message body$email_subject = "Feedback from my site";$email_header = "From: " . $Email . "\n" . "Return-Path: " . $Email . "\n" . "Reply-To: " . $Email . "\n";$message = stripslashes($message);// This is the step that actually uses the unix client named "mail" to mail the form to you.mail ( $email_address, $email_subject, $message, $email_header );?><!-- END CRITICAL REGION   *********************************************************** --><!-- ONCE AGAIN YOU CAN EDIT THE PART BELOW THIS --><!-- This part loads by default if you don't specify any after-post URL. This notifies the user     that the form has been mailed properly. FEEL FREE TO EDIT THIS PART TO SUIT YOUR NEEDS. --><html><head><title>You've successfully submitted the feedback</title></head><body>	<font face="Tahoma">  Thank you <?php print stripslashes($_POST['Name']); ?><br>  Your form has been sent.<br><!-- Remember the $post_page variable that contained a link for the page to load after submission ?     This is where it comes into action. If it is left "/" clicking this link will redirect you to     you main index page. -->    <a href="<?php print "$post_page"; ?>">Click here to continue</a>	</font></body></html>

See if this helps you. All the best :)

Share this post


Link to post
Share on other sites

// ***********************************************************// CRITICAL REGION
// DO NOT EDIT THE CODE BELOW THIS UNLESS YOU KNOW WHAT YOU'RE DOING
if ($_SERVER['REQUEST_METHOD'] != "POST")
{
exit;
}


This is not really necessary in my opinion because it doesn't really matter if the data is PUTted or POSTed.

and you might want to lower the <?php - tag to under the html stuff. Or else you get syntax errors

extra note : if you store something in a variable, use ' .... ' instead of " ... " since you only need "...." if you have regular expressions like /n in the data. using single quotes speeds the processing up a tiny bit since the parser does not have to look for regular expressions.

You also might want to check if the sending succeeds, and use

if (mail(bla,bla,bla,bla) == false ) { echo 'Sending failed'; }

or something.

Those were my suggestions, note that i'm not trying to out-smart you or anything :(, just trying to help :).

Looking at your code, i can see that you have good coding-etiquette, nice comments and stuff. I hardly comment my code :blush:. So I gotta learn that one day too ;).

Share this post


Link to post
Share on other sites

Well now, since the other was so rudely closed... (ironic as it was a moderator who initially replyed to my post... and the topic was left open... weird. And my appologies for digging up an close-to-month old topic... got yelled at for starting a new one...)

If I understand this correctly, under this line:

$post_page = "/";if the "/" is changed to a url, the url will be displayed after submission instead of:<html><head><title>You've successfully submitted the feedback</title></head><body><font face="Tahoma"> Thank you <?php print stripslashes($_POST['Name']); ?><br> Your form has been sent.<br><!-- Remember the $post_page variable that contained a link for the page to load after submission ?    This is where it comes into action. If it is left "/" clicking this link will redirect you to    you main index page. -->   <a href="<?php print "$post_page"; ?>">Click here to continue</a></font></body></html>


Or would I have to edit the html at the end of the code in order for it to display the "thank you" page I would like the users to see?

Comments back from m^e on this would be appreciated, as it is their code... :rolleyes:

Share this post


Link to post
Share on other sites

You can freely edit the HTML at the end of the code - this HTML gives shape to your Thank You page.. so you can change it to whatever you like..

 

The $post_page="/" --> This variable contains the link, that is loaded AFTER the Thank You page. If you want a different page to load after Thank You, ONLY then you should include the link here. If you leave it to simply "/" - as you can see from this code

<a href="<?php print "$post_page"; ?>">Click here to continue</a>// This gets parsed as:<a href="/">Click here to continue</a>
which means, you're being taken back to the top-level or index page of your site.

 

Hope this will clear it up. :rolleyes:

 

P.S. Also when quoting some sort of code in your posts, use the CODE tag to enclose it - that makes easier reading. Am modifying your post to demonstrate it.

Share this post


Link to post
Share on other sites

(ironic as it was a moderator who initially replyed to my post... and the topic was left open...

Are you talking about microscopic here?

M^E give you the link to this topic a few minutes before I could do and he didn't think it was necessary to close it.

I thought that it might be better to close it so nobody else is going to reply here, and if they had comments they would reply here. So i did (you still with me :) )

and about

close-to-month old topic

That doesn't matter because knowledge = knowledge, even if its almost an year old. Maybe it might feel silly to reply to something a month old, but if you have a question and post it it there, it will show up on the last post thingy on the bottom and people will read it and reply to it. It's not so that noones going to reply because the topics old :rolleyes:

Who cares if the topic was started a month ago?

Share this post


Link to post
Share on other sites

Many forums I've been to in the past view the digging up of old topics to be spammy, as a multitude of people will just go thread by thread and reply to everything, even though it has no current relevence.In any event, I'll be testing everything out and seeing if it all works the way I'm hoping it will sometime either today or tommorow. Thanks for the help up to now...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.