Using PEAR without installation & How to setup the SMTP PEAR mailer

There is a mobile optimized version of this page, view AMP Version.

PEAR (PHP Extension And Application Repository) is a framework and distribution system for reusable PHP components, it comes in “packages” depending on various functionality you may require and is installed into a server using PEAR’s own installer or PYRUS.

However you CAN “install” any/all of the PEAR packages without actually “installing”, the PEAR packages are just PHP scripts and can easily be dropped into a directory on a PHP Server and included into a website or system. Below is a method which I have used so that I can include a PEAR package dropped into my server.

The Basics Of PEAR…

The first thing you really need to understand is how the PEAR packages work together, the most important package is the PEAR package itself which is the “core” to everything else:

http://pear.php.net/package/PEAR

This package is needed by any other package from the pear packages, so you would normally download and upload this package first, so lets assume you have and uploaded it to your FTP under:

/pear/

The next step is to include any other packages you require, each package has a set of “dependencies” which you must also include, so make sure you follow the dependencies and copy those files into your /pear/ folder.

For this tutorial I will explain how to include the SMTP mailer and all dependencies.

SMTP PEAR Mailer Setup

Okay so you have the main PEAR package in /pear/ on your server, now we will explain how to include the NET_SMTP package and all dependencies so you can use the SMTP PEAR Mailer.

PEAR’s SMTP Mailer is one of the most useful packages on the internet for sending email using an SMTP server, I see many questions on the internet on how to set this up and use it on a server.

The first package we will require is the PEAR :: Mail Package:

http://pear.php.net/package/Mail/download

Put this package in: /pear/Mail/

Click “Download” and you will see that the dependencies are “Net_SMTP” and that this is Optional, of course if we wasn’t looking to send email using SMTP we would not need this package. However because this guide is for SMTP email we will also need to go and fetch this package:

http://pear.php.net/package/Net_SMTP/download

Put this package in: /pear/Net/

This package also has dependencies which you will need to fulfil, NET_SOCKET:

http://pear.php.net/package/Net_Socket/download

Put this package in: /pear/Net/

Congratulations! You should now have all dependencies fulfilled for the SMTP Mailer package, now we can move on and get the PEAR package available in our PHP solution…

Include PEAR package into a PHP solution

Okay so we have PEAR sitting in /pear/ but because it is not “installed” we cannot just use PEAR’s features like you can if it is installed in the server, however do not worry because it is easy to implement your PEAR system into a site and then you can use it just like those people are who have it “installed”.

All you need to do is set an include path for PHP so that the scripts are included just like installed scripts. You can do this like so:

$path = DIRECTORY_SEPARATOR .'pear';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

Make sure $path is the path from the ROOT of your server right up to the directory you have dropped PEAR, do not add an ending slash to the path.

Congratulations! PEAR should now be working on your system and the SMTP Mailer should be working! You can use the PEAR SMTP Mail Package as follows:

$host = 'host';
$username = 'username';
$password = 'password';

$from = 'to';
$to = 'from';
$subject = 'subject';
$body = 'body message (can use HTML if you set HTML header)';

$headers = array(
     'from' => $from,
     'to' => $to,
     'subject' => $subject 
); 

$smtp = Mail::factory('smtp', 
     array( 
          'host' => $host, 
          'auth' => true, 
          'debug' => false, 
          'username' => $username, 
          'password' => $password 
     ) 
); 
$mail = $smtp->send($to, $headers, $body);

Please feel free to ask any questions, I may need to improve this guide as it was done pretty much from memory and may have missing packages and/or mistakes, please also comment if this worked out for you!

Thanks

Author: Dean Williams

I'm a Web Developer, Graphics Designer and Gamer, this is my personal site which provides PHP programming advice, hints and tips


Comments For This Post:

  • Hey, your tutorial might just have saved my life

    Yet, I have one question
    If I wanted to install the Mime package, where should I put it?
    When I install it to /pear/Mail and moved "mailPart.php" to /pear/Mail/Mail and I get an "email sent" message but it's not sent, and I get the following error:
    Strict Standards: Non-static method Mail::factory() should not be called statically in /home/u407937005/public_html/key/email_send_pear.php on line 71

    Strict Standards: Non-static method Mail::factory() should not be called statically in /home/u407937005/public_html/key/email_send_pear.php on line 72

    Strict Standards: Non-static method PEAR::raiseError() should not be called statically, assuming $this from incompatible context in /home/u407937005/public_html/key/pear/Mail/Mail/mail.php on line 162

    Strict Standards: Non-static method PEAR::isError() should not be called statically in /home/u407937005/public_html/key/email_send_pear.php on line 75

  • I've had serious problems like this before, in this case it would be best to get pear package working properly, if you need premium support please visit my company site https://webdesires.co.uk and we could provide support.


Leave a Reply: