PHP E-mail

The Mail () Function
 
Many websites offer a way for you to send them an email from a simple form on their site. Providing the form as opposed to simply listing your email address not only looks nicer but also serves two purposes.
 
1. First, the form lets the website owner decide what information it is important to collect and prompts the users to fill in the answers to each of their questions. This way the user doesn’t forget to include important information.
 
2. Second, if you list your email directly on your site it can be picked up by bots designed to ‘farm’ email addresses. What that means for you is SPAM. Nobody likes to have their inbox flooded with SPAM, and using a form can help prevent that.
 
The mail function is phrased as: mail (to, subject, body, headers)
 
An example is:
 
mail ( “[email protected]”, “Thanks for your Feedback”, “eBIZ Education thanks you for your valuable feedback”, ” [email protected] ” )
 
Creating the email form:
 
Source code for email.php
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" 
content="text/html; charset=iso-8859-1" />
<title>PHP Tutorial: Sending Email</title>
<script language = "Javascript" 
 type="text/javascript">
<!--
function emailcheck(email) {

		var at="@"
		var dot="."
		var lat=email.indexOf(at)
		var lstr=email.length
		var ldot=email.indexOf(dot)
		if (email.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (email.indexOf(at)==-1 || em
ail.indexOf(at)==0 || email.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (email.indexOf(dot)==-1 || email.indexOf(dot)==0 
|| email.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (email.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (email.substring(lat-1,lat)==dot 
|| email.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (email.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (email.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}
	
	
	function validateForm(){	
	var to=document.emailform.to
	var message=document.emailform.message
	
	
	if ((to.value==null)||(to.value=="")){
		alert("Please Enter your Email ID")
		to.focus()
		return false
	}	
	if (emailcheck(to.value)==false){
		to.value=""
		to.focus()
		return false
	}
		
	if((message.value.length<10)|| (message.value==""))
	{
		alert("Your message should contain atleast 10 character")
		return false
	}
	return true
 }
--></script>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
}
.style1{
background:#0099FF scroll; color:#FFFFFF; font-weight:bold
}
-->
</style></head>

<body>
 <form  name="emailform" method="post"  
onsubmit="return validateForm()" action="send-email.php">

 <table width="75%" border="0" cellspacing="3" cellpadding="0">
  <tr>
    <th width="12%" align="left" scope="row">TO:</th>
    <td width="88%"><input type="text" name="to" /></td>
  </tr>
<tr>
    <th align="left" scope="row">Subject</th>
    <td><input type="text" name="subject" /></td>
  </tr>
  <tr>
    <th align="left" scope="row">Message:</th>
    <td><textarea name="message" cols="40"
 rows="5" class="style1" id="message"></textarea></td>
  </tr>
  <tr>
    <th scope="row"><input name="submit" 
type="submit" class="style1" value="Send Email" /></th>
    <td><input name="reset" type="reset" 
class="style1" value="Clear Form" /></td>
  </tr>
</table>
</form>
</body></html>
 
The above form should look like the one shown below:
 
img
 
The server side PHP script:
 
send-email.php
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 
1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" 
content="text/html; charset=iso-8859-1" />
<title><?php echo $msg;?></title>
</head>

<body>
<?php
$msg="";
$send_to=$_POST['to'];
$from=$_POST['from'];
$subject=$_POST['subject'];
$msg=$_POST['message'];
$headers = 'From: $from' . "\r\n" .
    'Reply-To: $from' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
mail($send_to,$subject,$msg,$headers);
echo "Message Sent successfully to
 <a href=\"mailto:$send_to\">$send_to</a>";

?>
</body>
</html>
 
The $headers parameter in the above script is optional, you can also skip it, e.g.
 
mail(($send_to,$subject,$msg)
 
NOTE:
 
In order to execute this PHP script successfully and send emails through it, SMTP should be configured and other mail options should be configured in the php.ini