Specifications

This means each piece of mail must essentially be sent twice: once in test mode and once
in real mode.
The function also sends two different kinds of email: the text version, which it sends using
PHPs mail() function; and the HTML kind, which it sends using the HTML MIME Mail
class. Weve used mail() many times in this book, so lets look at how we use the HTML
MIME Mail class. We will not cover this class comprehensively, but instead explain how we
have used it in this fairly typical application.
We begin by including the class file and creating an instance of the class:
include(‘class.html.mime.mail.inc’);
$mail = new html_mime_mail();
We then load the image details from the database and loop through them, adding each image to
the piece of mail we want to send:
$mail->add_html_image($image,
mysql_result($result, $i, 0),
mysql_result($result, $i, 1));
The three parameters we pass to add_html_image() are the actual image content as read from
the file, the filename, and the files MIME type.
We also need to add the body text, in both HTML and text formats:
$mail->add_html($html, $text);
Then we create the actual body of the email:
$mail->build_message();
Finally, having built the message body, we can send it. We do this by retrieving and looping
through each of the users subscribed to this list, and using either the HTML MIMEMIME Mail
send() or regular mail() depending on the users MIME type preference:
if($subscriber[2]==’H’)
//send HTML version to people who want it
$mail->send($subscriber[0], $subscriber[1], $from_name,
$from_address, $subject);
else
//send text version to people who don’t want HTML mail
mail($subscriber[0].” <”.$subscriber[1].”>”, $subject,
$text, “From: $from_name <$from_address>”);
The first parameter of $mail->send() should be the users actual name, and the second
parameter should be his email address.
Thats it! We have now completed building the mailing list application.
Building Practical PHP and MySQL Projects
P
ART V
708
34 7842 CH28 3/6/01 3:46 PM Page 708