Page 163 - Open Soource Technologies 304.indd
P. 163

Open Source Technologies



                   Notes         9.7.2 Creating the Script to Send the Mail

                                 This script is only slightly different in concept than the script in Listing 5, which simply printed
                                 form responses to the screen. In this script, in addition to printing the responses to the screen,
                                 you send them to an email address as well.

                                 Listing 12 Sending the Simple Feedback Form
                                    1.  <html>

                                    2.  <head>
                                    3.  <title>Listing 12 Sending mail from the form in Listing 11</title>

                                    4.  </head>
                                    5.  <body>

                                    6.  <?php
                                    7.  print “Thank you, <b>$_POST[name]</b>, for your message!<br><br>\n\n”;

                                    8.  print “Your e-mail address is: <b>$_POST[email]</b><br><br>\n\n”;
                                    9.  print “Your message was:<br><br>\n\n”;

                                   10.  print “$_POST[message] <br><br>”;
                                   11.  //start building the mail string

                                   12.  $msg = “Name: $_POST[name]\n”;
                                   13.  $msg .= “E-Mail: $_POST[email]\n”;

                                   14.  $msg .= “Message: $_POST[message]\n”;
                                   15.  //set up the mail

                                   16.  $recipient = “you@yourdomain.com”;
                                   17.  $subject = “Form Submission Results”;

                                   18.  $mailheaders = “From: My Web Site <defaultaddress@yourdomain.com> \n”;
                                   19.  $mailheaders .= “Reply-To: $_POST[email]”;

                                   20.  //send the mail
                                   21.  mail($recipient, $subject, $msg, $mailheaders);

                                   22.  ?>
                                   23.  </body>

                                   24.  </html>

                                 The variables you use in lines 7-9 are $_POST[name], $_POST[email], and $_POST[message]—the
                                 names of the fields in the form, as part of the $_POST superglobal. That’s all well and good for
                                 printing the information to the screen, but in this script, you also want to create a string that’s
                                 sent in email. For this task, you essentially build the email by concatenating strings to form one
                                 long message string, using the newline (\n) character to add line breaks where appropriate.




        158                               LOVELY PROFESSIONAL UNIVERSITY
   158   159   160   161   162   163   164   165   166   167   168