Приглашаем посетить
Островский (ostrovskiy.lit-info.ru)

13.5 Sending and Receiving Mail

Previous Table of Contents Next

13.5 Sending and Receiving Mail

The mail( ) function (which you saw briefly in Example 6-30) sends an email message. To use mail( ), pass it a destination address, a message subject, and a message body. Example 13-6 sends a message with mail( ).

Example 13-6. Sending a message with mail( )
$mail_body=<<<_TXT_

Your order is:

* 2 Fried Bean Curd

* 1 Eggplant with Chili Sauce

* 3 Pineapple with Yu Fungus

_TXT_;

mail('hungry@example.com','Your Order',$mail_body);

To handle more complicated messages, such as an HTML message or a message with an attachment, use the PEAR Mail and Mail_Mime modules. Example 13-7 shows how to use Mail_Mime to send a multipart message that has a text part and an HTML part.

Example 13-7. Sending a message with text and HTML bodies
require 'Mail.php';

require 'Mail/mime.php';



$headers = array('From' => 'orders@example.com',

                 'Subject' => 'Your Order');



$text_body = <<<_TXT_

Your order is:

* 2 Fried Bean Curd

* 1 Eggplant with Chili Sauce

* 3 Pineapple with Yu Fungus

_TXT_;



$html_body = <<<_HTML_

<p>Your order is:</p>

<ul>

<li><b>2</b> Fried Bean Curd</li>

<li><b>1</b> Eggplant with Chili Sauce</li>

<li><b>3</b> Pineapple with Yu Fungus</li>

</ul>

_HTML_;



$mime = new Mail_mime( );

$mime->setTXTBody($text_body);

$mime->setHTMLBody($html_body);



$msg_body = $mime->get( );

$msg_headers = $mime->headers($headers);



$mailer = Mail::factory('mail');



$mailer->send('hungry@example.com', $msg_headers, $msg_body);

When hungry@example.com reads the message sent in Example 13-7, his mail-reading program displays the HTML body or the text body, depending on its capabilities and how it is configured.

Read more about PEAR Mail and Mail_Mime in PHP Cookbook (O'Reilly), Recipes 17.1 and 17.2; in Chapter 9 of Essential PHP Tools by David Sklar (APress); and at http://pear.php.net/manual/en/package.mail.mail-mime.php.

    Previous Table of Contents Next