13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Implementing Administrative Functions<br />

739<br />

The parameters you pass to addHTMLImage() are the name of the image file (or you<br />

could also pass the image data), the MIME type of the image, the filename again, <strong>and</strong> true<br />

to signify that the first parameter is a filename rather than file data. (If you wanted to<br />

pass raw image data, you would pass the data, the MIME type, an empty parameter, <strong>and</strong><br />

false.) These parameters are a little cumbersome.<br />

At this stage, you need to create the message body before you can set up the message<br />

headers.You create the body as follows:<br />

// create message body<br />

$body = $message->get();<br />

You can then create the message headers with a call to the Mail_mime class’s headers()<br />

function:<br />

// create message headers<br />

$from = '"'.get_real_name($admin_user).'" ';<br />

$hdrarray = array(<br />

'From' => $from,<br />

'Subject' => $subject);<br />

Finally, having set up the message, you can send it.To do this, you need to instantiate the<br />

PEAR Mail class <strong>and</strong> pass to it the message you have created.You begin by instantiating<br />

the class, as follows:<br />

// create the actual sending object<br />

$sender =& Mail::factory('mail');<br />

(The parameter ‘mail’ here just tells the Mail class to use <strong>PHP</strong>’s mail() function to<br />

send messages.You could also use ‘sendmail’ or ‘smtp’ as the value for this parameter<br />

for the obvious results.)<br />

Next, you send the mail to each of your subscribers.You do this by retrieving <strong>and</strong><br />

looping through each of the users subscribed to this list <strong>and</strong> using either the Mail<br />

send() or regular mail() depending on the user’s MIME type preference:<br />

if($subscriber[2]=='H') {<br />

//send HTML version to people who want it<br />

$sender->send($subscriber[1], $hdrs, $body);<br />

} else {<br />

//send text version to people who don't want HTML mail<br />

mail($subscriber[1], $subject, $text,<br />

'From: "'.get_real_name($admin_user).'"<br />

');<br />

}<br />

The first parameter of $sender->send() should be the user’s email address; the second,<br />

the headers; <strong>and</strong> the third, the message body.<br />

That’s it! You have now completed building the mailing list application.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!