Home Php PHP : Form to Email



PHP : Form to Email
Posted on: November 20, 2009 at 12:00 AM
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

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>

<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>

</table>

</form>

<?php } ?>

Output:

(If every field is valid and filled then output will be:)

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

Related Tags for PHP : Form to Email:


More Tutorials from this section

Ask Questions?    Discuss: PHP : Form to Email   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.