Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Email PHP function and connecting it to a form
#1
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.
[Image: bleachsig101.gif]
WWW.BLASTDANIME.COM - STREAMING ANIME
MSN:monkeysondope@hotmail.com
Reply
#2
Was planning to get to formspring for a form.
This seems good. Will try sure...
Giveaway - A Rare Autoblog Guide. PM me for it, by clicking here.

Hey guys. It's Dr.Viper from HF. Howdy.
o HTML and PHP based website design.
o SEO/SEM services.
o Logo & GFX Design services.
And above all, everyone know what i'm good at HF... So,
o Security Consultancy.
Reply
#3
Great, thanks for this.
Reply
#4
By all means use formspring, its good. I just offered this for ppl who dont know what to name the ID's and stuff.
[Image: bleachsig101.gif]
WWW.BLASTDANIME.COM - STREAMING ANIME
MSN:monkeysondope@hotmail.com
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 766 07-27-2020, 11:26 PM
Last Post: tk-hassan
  help with mass email php ceptic 2 1,289 07-28-2012, 04:58 AM
Last Post: ceptic
  PHP email input form - having trouble getting the form to work... abayindi 4 2,395 03-19-2012, 10:02 AM
Last Post: RainbowDashFTW
  [NEED HELP] PHP Form post, get, echo Đενɨаηсε™ 5 1,800 02-06-2012, 01:16 PM
Last Post: ★Cooldude★
  How to make a form. HB Virus 10 3,056 12-30-2011, 08:03 PM
Last Post: BreShiE

Forum Jump:


Users browsing this thread: 1 Guest(s)