Support Forums

Full Version: Email PHP function and connecting it to a form
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The HTML Form.


Ok, so what we are going to need for the first part of this tutorial is a page written in HTML which will show the form where we put the information for the email to be sent. this can be done inside the PHP script but we are going to make them separately for simplicities sake. I used a HTML form generator for mine and have uploaded it here for those that want to use it: http://www.mediafire.com/?cyn4jj3ewmm

Open the .html file with Notepad++ which can be downloaded here: http://sourceforge.net/projects/notepad-plus/files/ and edit the bits where i have noted:

[Image: emailcode1.png]

After that you are finished the first bit.


The PHP Script.


Ok, so here's where we get to the actual scripting part. what we are going to be using is the mail() function. Paste the following code into your text editor and save it as email.php (you must name it email.php or change some settings in the form).

Code:
</head>
<body id="main_body" >
    
    <img id="top" src="top.png" alt="">
    <div id="form_container">
    
        <h1><a>Edit this as your heading</a></h1>

<?php
//define the receiver of the email
$to = $_POST["element_1"];
//define the subject of the email
$subject = $_POST["element_2"];
//define the message to be sent. Each line should be separated with \n
$message = $_POST["element_3"];
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: you@wateva.com\r\nReply-To: you@wateva.com";
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
echo "Mail Sent";
?>

<div id="footer">
        </div>
    </div>
    <img id="bottom" src="bottom.png" alt="">
    </body>
</html>

Ok, now to explain what we have here:

Code:
</head>
<body id="main_body" >
    
    <img id="top" src="top.png" alt="">
    <div id="form_container">
    
        <h1><a>Edit this as your heading</a></h1>

This is just useless stuff, its a template that makes the page look pretty and has your heading on it.


Code:
<?php
//define the receiver of the email
$to = $_POST["element_1"];
//define the subject of the email
$subject = $_POST["element_2"];
//define the message to be sent. Each line should be separated with \n
$message = $_POST["element_3"];
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: you@wateva.com\r\nReply-To: you@wateva.com";

These are defining your variables etc etc etc... if you dont know about variables then check out the PHP 101 post because im not going into it right now.


Code:
$headers = "From: you@wateva.com\r\nReply-To: you@wateva.com";
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
echo "Mail Sent";
?>

This is the function that gets all your variables and then sends the email.


Code:
<div id="footer">
        </div>
    </div>
    <img id="bottom" src="bottom.png" alt="">
    </body>
</html>

Again this is just template to make it all look nice and pretty Tongue.


If your liked this tut then post a thanks, also keep in mind this tutorial is directed at newbies so dont bring your abuse into this thread cause its not needed lol.

Peace and have a good 1 fellas,

iPhish.
Was planning to get to formspring for a form.
This seems good. Will try sure...
Great, thanks for this.
By all means use formspring, its good. I just offered this for ppl who dont know what to name the ID's and stuff.