Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Php PHP : Form to Email

 
 

Share on Google+Share on Google+

PHP : Form to Email

Advertisement
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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 20, 2009

Related Tutorials

Discuss: PHP : Form to Email   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com