PHP : Form to Email
With the help of this tutorial you can send mails to a user using a form, as we have discussed in our earlier tutorial, it is very easy to send mails using PHP and any mail server like argosoft mail server.
This tutorial will help you to develop a form which looks like a mail form, with the help of this tutorial you can send mails to any valid user. As the mail function needs the address of the receiver, subject and message as primary requirement, if you miss to enter any of these mail will not be sent and an error message will be generated on the next page. If the address of the recipient is valid and every textbox is filled with proper value mail will be sent.
Code:
<?php
$to=$sub=$msg=$hdr="helo";
if(isset($_POST['to']))
{
$to=$_POST['to'];
$sub=$_POST['sub'];
$msg=$_POST['msg'];
$hdr="from:basu@localhost";
$mail=mail($to,$sub,$msg,$hdr);
if($mail)
{
echo "Email has been sent";
}
else
echo "Sorry";
}
else
{
?>
<form name="mail" action="<?php $PHP_SELF;?>" method="post">
<table>
<tr><td><b>To:</b></td><td><input type="text" name="to"></input></td></tr>
<tr><td><b>Subject:</b></td><td><input type="text" name="sub"></input></td></tr> 0
<tr><td><b>Message:</b></td><td><textarea name="msg"></textarea></td></tr>
<tr><td><input type="submit" value="Submit"></input></td>
<td><input type="reset" value="Reset"></input></td></tr> 1
</table>
</form>
<?php } ?> 2
Output:
(If every field is valid and filled then output will be:) 3
Email has been sent
(If every field is not valid or filled then an error message will be generated as follows:)
Warning: mail() [function.mail]:
SMTP server response: 503 No recipient(s). in C:\wamp\www\phpBasics\PHPmail.php
on line 10
Sorry
4