Using PEAR

The PHP Extension and Application Repository (PEAR) is an open source structured library of packages for PHP developers. These packages provide routines which solve problems PHP developers regularly face: sending structured e-mail (such as an HTML attachment), interacting with different databases from a single script, error handling, recovery and logging.

Python vs. PHP: Choosing your next project's language

The base installation of PEAR is shipped with PHP itself. The programmers working on PEAR have developed a method of installing new packages and keeping your existing installation up-to-date, called the PEAR package manager.

PEAR package manager

If you are using a version of PHP prior to 4.3, you will need to install the PEAR package manager. UNIX users can run the following command:

lynx -source http://go-pear.org/ | /path/to/php

This downloads the source of the page at http://go-pear.org and runs it with the PHP binary (replace /path/to with the path to PHP on your system).

Windows users should go to the specified URL and save the page as 'pear.php' then run the following in DOS:

\path\to\php pear.php

The installation process creates a program called pear (this program is shipped with PHP 4.3), which is the PEAR package manager. Once installation is complete, you are ready to interact with the PEAR package manager.

Using the package manager

The PEAR package manager requires Internet access to download and install new packages. Make sure you are connected to the Net before proceeding. Before downloading new packages, it is useful to see which PEAR packages are installed. To do so, run:

pear list

This prints a formatted list of the package names, their version, and their status (whether the release is stable, in beta or otherwise). For basic installations, this list is quite short. To see all available PEAR packages, run:

pear remote-list

To install a package on the remote list, type:

pear install <package name>

For example, a package which is not installed by default is Mail_Mime. To install Mail_Mime type:

pear install Mail_Mime

You can find out more about Mail_Mime with the following command:

pear remote-info Mail_Mime

Using PEAR

PEAR uses classes and objects to provide a unified interface to the extensions provided. The following code shows how easy it is to use.

01 <?
02 require_once("Mail.php");
03 require_once("Mail/mime.php");
04
05 $html = "<HTML><BODY><p>Hello World, in HTML</p></BODY></HTML>";
06 $mime = new Mail_mime();
07
08 $mime->setTXTBody(strip_tags($html));
09 $mime->addAttachment($html,"text/html","test.html",false);
10
11 $body = $mime->get();
12 $headers = $mime->headers(array("From" => "test@myhost.com.au", "Subject" => "Test"));
13
14 $mail =& Mail::factory("mail");
15 if(!$mail->send("you@yourhost.com.au",$headers,$body)) {
16 echo "Could not send email";
17 }
18 ?>

This short script generates a MIME-formatted e-mail with plain text and HTML parts. Lines two and three include the files Mail.php and mime.php, where the PEAR classes to be used are stored. We use require_once() instead of include(), since PEAR packages might also call include(), causing an error.

On line six, we create an object, $mime, from the Mail_mime class. Mail_mime methods allow developers to create multi-part MIME e-mails. On line eight, we add a text-based MIME section using the setTXTBody() method. On line 9, we attach the HTML version using the addAttachment() method. The first argument to this method is the string to attach, $html. The next argument specifies the MIME content type - in this case, text/html. The third argument specifies a file name for the attachment to be called and the fourth tells addAttachment() whether the string passed as the first argument is a file name or not. We tell it that it is not a file name (and that, therefore, it is the data which should be attached). On line 11, we generate the multi-part MIME body of the e-mail. On line 12, we generate the e-mail headers (the from address, and the e-mail subject); change these to suit your own situation.

Line 14 generates our mailer object, which is used on the following line to dispatch the e-mail, using the send() method.

Join the PC World newsletter!

Error: Please check your email address.

Our Back to Business guide highlights the best products for you to boost your productivity at home, on the road, at the office, or in the classroom.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Essentials

Lexar® JumpDrive® S57 USB 3.0 flash drive

Learn more >

Microsoft L5V-00027 Sculpt Ergonomic Keyboard Desktop

Learn more >

Mobile

Lexar® JumpDrive® S45 USB 3.0 flash drive 

Learn more >

Exec

HD Pan/Tilt Wi-Fi Camera with Night Vision NC450

Learn more >

Lexar® Professional 1800x microSDHC™/microSDXC™ UHS-II cards 

Learn more >

Lexar® JumpDrive® C20c USB Type-C flash drive 

Learn more >

Audio-Technica ATH-ANC70 Noise Cancelling Headphones

Learn more >

Budget

Back To Business Guide

Click for more ›

Most Popular Reviews

Latest News Articles

Resources

GGG Evaluation Team

Michael Hargreaves

Windows 10 for Business / Dell XPS

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Aysha Strobbe

Windows 10 / HP Spectre

Ultimately, I think the Windows 10 environment is excellent for me as it caters for so many different uses. The inclusion of the Xbox app is also great for when you need some downtime too!

Mark Escubio

Windows 10 / Lenovo Yoga

For me, the Xbox Play Anywhere is a great new feature as it allows you to play your current Xbox games with higher resolutions and better graphics without forking out extra cash for another copy. Although available titles are still scarce, but I’m sure it will grow in time.

Kathy Cassidy

STYLISTIC Q702

First impression on unpacking the Q702 test unit was the solid feel and clean, minimalist styling.

Anthony Grifoni

STYLISTIC Q572

For work use, Microsoft Word and Excel programs pre-installed on the device are adequate for preparing short documents.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?