PHP HelpPHP Help

Online Community that helps beginners learn PHP,
and webmasters solve PHP coding problems

since 1999





How To Generate and Send Mail From PHP

Sending Mail from a Form

Of course, any of these headers or the message body can be specified using variables. This is a full treatment of the previous script, expanding it into a simple form handler. It introduces variables and divides the code into a small configuration section and the familiar sendmail script. The first section of PHP code serves as brief configuration area for setting up the values of key variables. It is helpful to store the path to your mail program in a variable so you don't have to hunt through code when it comes time to change it. The second does the work of sending the mail. In true php fashion, it intermingles HTML and PHP code to conditionally prompt the user to fill out the form or mail information collected by the form to the site administrator. Including the email address of the person submitting the form in the "From:" field makes it easy for the person receiving the mail to reply to the submitter - they just have to hit 'Reply' in their Eudora or their favorite email application. Following the internet convention, the "From:" field is formatted by placing the email address angle brackets:

"From: $frmName <$frmEmail> "
 

Each form input has a variable name. When the form is submitted, PHP automatically creates variables using the names given in the NAME attribute in the form for each input using the values entered by the user. These variables are available from within the PHP script. For each variable a line is added to the sendmail output printing that value in the message.

<?php

/* Configuration -----------------------------------------*/
/* Mail results to this address                           */
/* Set this to the email address you wish to receive mail */
/* from the form submissions at.                          */
       
$TO "YourEmailId@YourISPDomain.SomeTopLevelDomain";
/* Specify system mail program                            */
/* Set this to the path to your mail program. Check with  */
/* your server administrator for the proper location.     */
       
$MP "/usr/sbin/sendmail -t";


/*-------------------------------------------------------*/
/* Decide if we should display a new form or send the    */
/* form data by email.                                   */
/* To make this decision, the script can check for the   */
/* existence of 1) the action variable defined by a      */
/* hidden field; 2) a required form field that you know  */
/* will always be set on submission; or you may set the  */
/* action variable to a particular value that can be     */
/* checked to determine the action to take. I chose      */
/* to simply check for the existence of the hidden       */
/* action variable (which is always set as long as we    */
/* give a value in the hidden field).                    */
/*-------------------------------------------------------*/

if ($frmAction)
{

/*-------------------------------------------------------*/
/* A thank you message (or other response) goes here. We */
/* switch to HTML mode to make it easy to include any    */
/* tags you wish without worrying about quoted           */
/* attributes.                                           */
/*-------------------------------------------------------*/

?>

<div align="center">
<table width="350" border="2">
<tr>
 <td bgcolor="#C0C0C0">
<p>Thank you for requesting a personalized quote for our
products and services.</p>
</td>
</tr>
</table>
</div>



<?php

/*-------------------------------------------------------*/
/* The real work gets done here by opening a pipe to     */
/* sendmail, which sends the contents of the submitted   */
/* form by email to the address specified in the         */
/* configuation section (which can acutally be an        */
/* an included initialization file if you want to get    */
/* fancy). For each variable we expect the form to       */
/* to submit, we output as part of the email.            */
/*-------------------------------------------------------*/


$fd popen($MP,"w");
fputs($fd"To: $TO ");
fputs($fd"From: $frmName <$frmEmail> ");
fputs($fd"Subject: Message from your web site ");
fputs($fd"X-Mailer: PHP3 ");
fputs($fd"Name: $frmName ");
fputs($fd"Phone: $frmPhone ");
fputs($fd"Fax: $frmFax ");
fputs($fd"Email: $frmEmail ");
fputs($fd"Address: $frmAddress ");
fputs($fd"Price range: $frmPriceRange ");
fputs($fd"Details: $frmFurther");
pclose($fd);

/*-------------------------------------------------------*/
/* Here the script must exit so we don't display the     */
/* form again once the thank you message has been        */
/* displayed and the mail sent.                          */
/*-------------------------------------------------------*/

exit;

} else {
// start else clause

?>

<div align="center">
<form action="mail.php" method="post">
<table>
<tr>
 <td colspan="2"><p>Please enter your information for a personal quote.</p></td>
</tr>
<tr>
 <td>Name:</td>
 <td><input type="text" name="frmName" size="24"></td>
</tr>
<tr>
 <td>Phone:</td>
 <td><input type="text" name="frmPhone" size="24"></td>
</tr>
<tr>
 <td>Fax:</td>
 <td><input type="text" name="frmFax" size="24"></td>
</tr>
<tr>
 <td>Email:</td>
 <td> <input type="text" name="frmEmail" size="24"></td>
</tr>
<tr>
 <td>Address:</td>
 <td><input type="text" name="frmAddress" size="24"></td>
</tr>
<tr>
 <td>Price Range:</td>
 <td><input type="text" name="frmPriceRange" size="24"></td>
</tr>
<tr>
 <td>Details:</td>
 <td><input type="text" name="frmFurther" size="24">
 </td>
</tr>
<tr>
 <td>
<!-- To determine whether the script should display the form or
mail the data, you can check for existence of a required field
or this special action variable. -->
<input type="hidden" name="frmAction" value="formmail">
<input type="submit" value="Submit">
 </td>
 <td></td>
</table>
</div>

</form>

<?php
// end else clause
?>
 
Pages:  1   2   3   4   5Related Forum Topics   |   More Tutorials »




Copyright © 2017 PHPHelp.com