pTutorial

New way to learn PHP

PHP 5 Send Mail without registration

Send mail

To send email without any registration use HTML forms with PHP as shown below.

HTML Form

 
<body>
<html>
<form  method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<table id="tt1">
<tr>
<td class="fhy">To</td>
<td><input name="to" type="text" value="<?php if(isset($to))   { echo $to;} ?>"><br /><?php  echo "<div class='error'>";
if(isset($error))   { 
echo $error;
}
echo "</div>";    ?></td>

</tr>
<tr>
<td >Subject</td>
<td><input name="subject" type="text" value="<?php if(isset($subject))   { echo $subject;} ?>" > <?php  echo "<div class='error'>";
if(isset($error1))   { 
echo $error1;
}
echo "</div>";    ?> </td>

</tr>
<tr>
<td class="fhy">From</td>
<td><input name="from" type="email" value="<?php if(isset($from))   { echo $from;} ?>" > <?php  echo "<div class='error'>";
if(isset($error2))   { 
echo $error2;
}
echo "</div>";    ?> </td>

</tr>


<tr>
<td class="fhy">Messege</td>
<td><textarea name="mess" cols="19" rows="12"><?php if(isset($message))   { echo $message;} ?></textarea>  <?php  echo "<div class='error'>";
if(isset($error3))   { 
echo $error3;
}
echo "</div>";    ?></td>

</tr>

<tr>
<td><input name="sub" type="submit" value="Send" /></td>

</tr>
</table>
</form>
</body>
</html>

In the above example, three text fields, one text area and submit button with proper validation. Table and some CSS are used for formatting the form, you can use as per your need.

Some PHP code is also there for validation and error message, you can easily understand.

PHP send mail without registration

  

<?php

if (isset($_POST["sub"])) 
{
	$to = check($_POST["to"]); 	$subject = check($_POST["subject"]);
	$message = check($_POST["mess"]);
	$from = check($_POST["from"]);


if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/",$to))
{
$error= "Enter correct email ID"; 
}

elseif($subject=="" ) 
{
	$error1="Do not leave blank";
}
elseif(strlen($subject)<4)
{
$error1= "length must be gretar than four";
}
elseif(is_numeric($subject)==true)
{
$error1= "must be alphabetical";
}
elseif(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/",$from))
{
$error2= "Enter correct email ID"; 
}
elseif($message == "") 
{
$error3= "enter message first";
}

else
{	
mail($to,$subject,$message,"From: $from\n");
echo "Thank you for sending us feedback";
header('Location:php-send-mail.php');
}

}


function check( $value )
{
$temp = trim($value);
$temp = htmlspecialchars($value);
return $temp;
}

?>

PHP script for validating the form and send the e-mail to the given mail ID.

The form will not submit until the data will be properly entered and regular expression for email ID validation.

You can freely download above script by clicking the download link shown below.

Download source code


QUOTES OF THE DAY

The basis of all progress is self-reliance

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER