iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (35)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

July 20, 2008
Wiki: A Solution to the Web Design Problem
About
 
July 19, 2008
Top 10 Social Networking Sites
About
 
July 19, 2008
Plurk or Twitter: Which one is Better?
About
 
July 19, 2008
Create Menus with Lists and CSS
About
 
July 17, 2008
Poll: What do you do when you can't design?
About
 
July 17, 2008
22 Reasons to Plurk
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP /Email Systems

Contact Form: PHP and mail(); 

  Views:    10120
  Votes:    3
by Kirill Talanov 11/21/03 Rating: 

Synopsis:

Have you ever wondered how the automated feedback forms work? Ever wanted to make one for yourself? Creating a mailer form is easier than you think and this tutorial will guide you through the process.
Pages: firstback1 forwardlast
The Article

Here comes the fun part, coding the actual PHP!

You may remember that in the first part of the script we changed the 'action' attribute of the <form> tag to point to a page called 'mailer.php', now we just have to code that page.

<?php

$name = $_POST['name'];
$comments = $_POST['comments'];

Remember how we "named" our form fields in the previous page? Well, here's when those names come into play.Once the user hits submit they values of those fields are passed as "POST variables" and the user is redirected to 'mailer.php'.The data passed through the previous page can now be accessed in the form of: $_POST['nameofvariable'], but to make it easier we will assign those POST variables to regular variables. This is done in the $var = $_POST['var'] part.

mail("webmaster@yourdomain", "Feedback from your website!", "Hi! Somebody sent you comments through your site!\n\nTheir name: $name\n\nTheir comments:\n$comments");

What we've been all waiting for, the mailing part. The e-mail is sent to you via PHP's mail(); function, which works in the following way:

mail("you@youremailaddress.com", "The Subject of the E-mail", "The message.")


You may have noticed that we used '\n\n' in the body of the message, this means "new line" - 
whenever \n is typed it will result in a new line in the actual e-mail.
 
And $name and $comments will of course be the values of the data passed through the form.
 
Finally, we will tell the user that the message has been sent:
 
 echo "<p>E-mail sent, thank you.</p>";
 
 Let's wrap it all up into one file:
 
mailer.php
 

<html>
<head>
<title>My PHP Mailer</title>
</head>

<body>
<?php

$name = $_POST['name'];
$comments = $_POST['comments'];

mail("webmaster@yourdomain.com", "Feedback from your website!", "Hi! Somebody sent you comments through your site!\n\nTheir name: $name\n\nTheir comments:\n$comments");
echo "<p>E-mail sent, thank you.</p>";

?>
</body>
</html>

 
 
You should now know the basics of working with forms, passing data through pages and mailing 
using PHP's built-in mail(); function!
Pages: firstback1 forwardlast

Similar/related articles:


 
  Sponsors