Home arrow PHP arrow Page 4 - Getting Intimate With PHP's Mail() Function
PHP

Getting Intimate With PHP's Mail() Function


Interested in sending advanced email using PHP's mail() function, or even by piping output directly to sendmail? If so, make sure you take a look at Steve's comprehensive tutorial on sending email with PHP.

Author Info:
By: Steve Knoblock
Rating: 4 stars4 stars4 stars4 stars4 stars / 70
January 02, 2003
TABLE OF CONTENTS:
  1. · Getting Intimate With PHP's Mail() Function
  2. · The Mail() Function Explained
  3. · Talking to Sendmail
  4. · Getting Mail From a Form
  5. · Conclusion

print this article
SEARCH DEVARTICLES

Getting Intimate With PHP's Mail() Function - Getting Mail From a Form
(Page 4 of 5 )

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 section of PHP codes 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.

The PHP code will include the email address of the person submitting the form in the "From:" field, which makes it easy for the person receiving the mail to reply to the submitter - they just have to hit 'Reply' in Eudora or their favorite email application. Following the Internet convention, the "From:" field is formatted by placing the email address in angled brackets, such as:

From: $frmName <$frmEmail>\n

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. 

I am using PHP 4.1 with register_globals turned on. You may need to explicitly call form variables through the $_FORM array if you're running PHP 4.1 or above.

<?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";

?>

<?php

/*-------------------------------------------------------*/
/* 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\n");
fputs($fd, "From: $frmName <$frmEmail>\n");
fputs($fd, "Subject: Message from your web site\n");
fputs($fd, "X-Mailer: PHP3\n");
fputs($fd, "Name: $frmName\n");
fputs($fd, "Phone: $frmPhone\n");
fputs($fd, "Fax: $frmFax\n");
fputs($fd, "Email: $frmEmail\n");
fputs($fd, "Address: $frmAddress\n");
fputs($fd, "Price range: $frmPriceRange\n");
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.php3" 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"><br>
</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>&nbsp;
</td>
</table>
</div>
<br>

</form>

<?php
} // end else clause
?>


This script is an example of how a web script can be used to generate a HTML form which can then be used to send mail. This is possible because the first time that we go through the script, we send the HTML for the form to the client's browser. The script then sits idle on the server until we call it again to accept the form values. The HTML form's action parameter is set to call the same script that generated it. The script looks at the values in its environment to see if it has been called by the same form. The presence of a key value required in the form may be used to signify form submission, or a special "action" value can be specified in a hidden input field in the form.

Of course, this simple example is just to teach you how to setup a form that mails data back to you. Unless you're making a quick form for a small project, you would not want to go through the process of writing the code to print out each variable. It's easy to spell a variable name incorrectly so the input does not show up in the output.

What is needed is a general form handling script that accepts any number of inputs without worrying about how many inputs there are or what they are called. Well, that just about takes the script as far as possible without writing a generalized form mail handler.
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials