php_mysql_apache [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

php_mysql_apache [Electronic resources] - نسخه متنی

Julie C. Meloni

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید









Sending Mail on Form Submission




You''ve already seen how to take form responses and print the results to the screen. You''re only one step away from sending those responses in an email message, as you''ll soon see. Before learning about sending mail, however, read through the next section to make sure that your system is properly configured.


System Configuration for the mail() Function



Before you can use the mail() function to send mail, a few directives must be set up in the php.ini file so that the function works properly. Open php.ini with a text editor and look for these lines:




[mail function]
; For Win32 only.
SMTP = localhost
; For Win32 only.
sendmail_from = me@localhost.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =


If you''re using Windows as your Web server platform, the first two directives apply to you. For the mail() function to send mail, it must be able to access a valid outgoing mail server. If you plan to use the outgoing mail server of your ISP (in the following example, we use EarthLink), the entry in php.ini should look like this:




SMTP = mail.earthlink.net


The second configuration directive is sendmail_from, which is the email address used in the From header of the outgoing email. It can be overwritten in the mail script itself, but normally operates as the default value. For example:




sendmail_from = youraddress@yourdomain.com


A good rule of thumb for Windows users is that whatever outgoing mail server you''ve set up in your email client on that machine, you should also use as the value of SMTP in php.ini.


If your Web server is running on a Linux/Unix platform, you use the sendmail functionality of that particular machine. In this case, only the last directive applies to you: sendmail_path. The default is sendmail -t -i, but if sendmail is in an odd place or if you need to specify different arguments, feel free to do so, as in the following example, which does not use real values:




sendmail_path = /opt/sendmail -odd arguments


After making any changes to php.ini on any platform, you must restart the Web server process for the changes to take effect.


Creating the Form



In Listing 9.10, you see the basic HTML for creating a simple feedback form. This form has an action of listing9.11.php, which we create in the next section. The fields are very simple: Line 7 contains a name field, line 8 contains the return email address field, and line 10 contains the text area for the user''s message.


Listing 9.10 Creating a Simple Feedback Form


1: <HTML>
2: <HEAD>
3: <TITLE>E-Mail Form</TITLE>
4: </HEAD>
5: <BODY>
6: <FORM action="listing9.11.php" method="POST">
7: <p><strong>Your Name:</strong><br> <INPUT type="text" size="25"
name="name"></p>
8: <p><strong>Your E-Mail Address:</strong><br> <INPUT type="text" size="25"
name="email"></p>
9: <p><strong>Message:</strong><br>
10: <textarea name="message" cols=30 rows=5></textarea></p>
11: <p><INPUT type="submit" value="send"></p>
12: </FORM>
13: </BODY>
14: </HTML>


Put these lines into a text file called listing9.10l, and place this file in your Web server document root. Now access the script with your Web browser, and you should see something like Figure 9.5.


Figure 9.5. Form created in Listing 9.10.





In the next section, you create the script that sends this form to a recipient.


Creating the Script to Send the Mail



The script in Listing 9.4, 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 9.11 Sending the Simple Feedback Form


1: <html>
2: <head>
3: <title>Listing 9.11 Sending mail from the form in Listing 9.10</title>
4: </head>
5: <body>
6: <?php
7: echo "<p>Thank you, <b>$_POST[name]</b>, for your message!</p>";
8: echo "<p>Your e-mail address is: <b>$_POST[email]</b></p>";
9: echo "<p>Your message was:<br>";
10: echo "$_POST[message] </p>";
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 79 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.


Lines 12 through 14 create the $msg string, which contains the values typed by the user in the form fields. This string is the one sent in the email. Note the use of the concatenation operator (.=) when adding to the variable $msg, in lines 13 and 14.


Lines 16 and 17 are hard-coded variables for the email recipient and the subject of the email message. Replace you@yourdomain.com with your own email address, obviously. If you want to change the subject, feel free!


Lines 18 and 19 set up some mail headers, namely From: and Reply-to: headers. You could put any value in the From: header; this is the information that displays in the From or Sender column of your email application when you receive this mail.



If your outbound mail server is a Windows machine, the \n newline character should be replaced with \r\n.



The mail() function takes four parameters: the recipient, the subject, the message, and any additional mail headers. The order of these parameters is shown in line 21, and your script is complete after you close up your PHP block and your HTML elements in lines 2224.


Put these lines into a text file called listing9.11.php, and place that file in your Web server document root. Use your Web browser and go back to the form, enter some information, and click the submission button. You should see something like Figure 9.6 in your browser.


Figure 9.6. Sample results from Listing 9.11.





If you then check your email, you should have a message waiting for you. It might look something like Figure 9.7.


Figure 9.7. Email sent from Listing 9.11.




Formatting Your Mail with HTML



The "trick" to sending HTML-formatted email is not a trick at all. In fact, it only involves writing the actual HTML and modifying the headers sent by the mail() function. In Listing 9.12, a variation of Listing 9.11, you can see the changes made in lines 1214 and lines 1819.


Listing 9.12 Sending the Simple Feedback Form HTML Version


1: <html>
2: <head>
3: <title>Listing 9.12 Sending the Simple Feedback Form HTML Version</title>
4: </head>
5: <body>
6: <?php
7: echo "<p>Thank you, <b>$_POST[name]</b>, for your message!</p>";
8: echo "<p>Your e-mail address is: <b>$_POST[email]</b></p>";
9: echo "<p>Your message was:<br>";
10: echo "$_POST[message] </p>";
11: //start building the mail string
12: $msg = "<p><strong>Name:</strong> $_POST[name]</p>";
13: $msg .= "<p><strong>E-Mail:</strong> $_POST[email]</p>";
14: $msg .= "<p><strong>Message:</strong> $_POST[message]</p>";
15: //set up the mail
16: $recipient = "you@yourdomain.com";
17: $subject = "Form Submission Results";
18: $mailheaders = "MIME-Version: 1.0\r\n";
19: $mailheaders .= "Content-type: text/html; charset=ISO-8859-1\r\n";
20: $mailheaders .= "From: My Web Site <defaultaddress@yourdomain.com> \n";
21: $mailheaders .= "Reply-To: $_POST[email]";
22: //send the mail
23: mail($recipient, $subject, $msg, $mailheaders);
24: ?>
25: </body>
26: </html>


In lines 1214, the message string now contains HTML code. Additional headers are created in lines 1819, which set the Mime Version header to 1.0 and the Content-type header to text/html with a character set of ISO-8859-1. When opened in an HTML-enabled mail client, the HTML in the message string will appear as intended, as shown in Figure 9.8.


Figure 9.8. Email sent from Listing 9.12.





/ 323