PHP E-Mail
E-Mail is an abbreviation of Electronic Mail, it is a method of passing, exchanging of thoughts. With the help of E-mail we can send various types of attachments, like .doc, .xls. .pdf etc. file
With the help of PHP we can send mails to another person. Given example will
illustrates the way we can send mail, in the current example we use, PHP,
JavaScript, and html scripts.
(Note: The given example is just a program to send mail, but it is not sufficient enough to send mail to another mail address, we need mail server and various other application to send a mail successfully )
Example:
<?php
if(isset($_REQUEST['user']))
{
$to=$_REQUEST['user'];
$subject=$_REQUEST['subject'];
$msg=$_REQUEST['msg'];
$from="[email protected]";
$headers="From:$from";
mail($to,$subject, $msg,$headers);
echo"Mail successfully sent";
}
else{
?>
<html>
<head>
<title>
My Email
</title>
<script type="text/javascript">
function validation()
{
var to=document.form.user.value;
var filter=/^.+@.+\..{2,3}$/
var sub=document.form.subject.value; 0
if (filter.test(to) )
testresults=true
else { 1
alert("Please input a valid email address!")
testresults=false
} 2
if(sub=="")
{ 3
alert("subject is blank");
return false;
} 4
return (testresults)
5
}
</script>
</head> 6
<body>
<center>
<form name="form" onsubmit="return(validation())" action="mailPhp.php"> 7
<table>
<tr>
<td>To:</td><td><input type="text" name="user" ></td> 8
</tr>
<tr>
<td>Subject:</td><td><input type="text" name="subject"></td> 9
</tr>
<tr>
<td>Message</td> 0
<td><textarea name='msg' rows="8" cols="12"></textarea></td>
</tr>
<tr> 1
<td><input type ="submit" value="submit"></td>
<td><input type="reset" value="Reset"></input></td>
</tr> 2
</table>
</form>
</center> 3
</body>
</html>
<?php }?> 4
Output:
5
If we left the subject box and to box blank or the email address is wrong, then alert box will be generated.
6
Since we did not install the mail server, output will be as follows:
Mail successfully sent