Saturday, December 7

Send Mail Function in PHP

Contact forms are a wonderful means of interaction with the visitors of your website. Any form on a website can be used for 2 major functions. To store a data in a database (MySql, SQL, Flat-file and others) or send the collected data to a email address.
In my this post I will explain  how to send mail to an address containing the collected fields from the form. In the later posts I will explain how to add the same data to the database directly.
In this example we will use 4 fields to take input from the user. We will create a form first having 4 fields
  • Name
  • Email Address
  • Subject
  • Message
First we will create a contact.html
The sample page should look like this:

<head>
<title>Contact Form</title>
</head>
<body>
<form action="sendmail.php" method="get" name="contactform">
Name:<input name="name" type="text" size="30" /><br />
Email:<input name="email" type="text" size="30" /><br />
Subject:<input name="subject" type="text" /><br />
Message:<textarea name="message" cols="30" rows="2"></textarea>
</form>
</body>
</html>

In this form we have set 4 input fields and method to GET and action to sendmail.php.
Next step we create a file sendmail.php with the following content..

<?php
$subject="Message regarding".$_GET ['subject'];
$headers .= "From: \"".$_GET ['name']."\ <".$_GET['email'].">\r\n";
$headers .='Content-type: text/html; charset=iso-8859-1';
mail ("someemail@something.com", $subject, "
<html>
<head>
<title>Contact Form</title>
</head>
<body><br><br>
". "Name: ".$_GET ['name']. "<br>
"."Email: ".$_GET['email']."<br>
"."Subject: ".$_GET['subject']."<br>
"."Message: ".$_GET['message']."<br>
"."Ipaddress: ".$_SERVER['REMOTE_ADDR']."
</body>
</html>" , $headers);
echo ("Thank you for your message.");
?>

When the visitor will click the submit button after filling in his details the content will be send to the sendmail.php as we have defined it in the action event of the form.
The sendmail.php will collect the information received from the contact form through GET method and will use the inbuild php function mail() to send the content to the specified email address. I have also added a an additional ip address field in the mail command which will retrieve the ip from which the form was submitted.

You might have noticed that in this form we have used GET as the method of form submission. But you can also use POST method instead. Just replace the method="get" to method="post", also you must change every GET in the php script with POST, or rather you can use $_REQUEST, which is method independent.
If you would like to know what is the difference between GET and POST method you can visit this page.

Difference between GET and POST methods

2 comments:

  1. brother i have one html file as login page ...and one php file to send data how to attach on website

    ReplyDelete
    Replies
    1. You must have a php enabled web host.
      If you are not sure then please ask your hosting provider for the same.
      After you have uploaded the files to your server and made the necessary changes, simple open the html file link in your browser and click the submit button. If everything is correct the message should be sent else error would be displayed accordingly.

      Delete

Comment anything you want. Just be polite and give respect to others!
I am simply going to remove the comments which are offensive or are off topic.
And please don't spam your website links in comments. I don't, neither should you.