This tutorial adds a couple of minor tweaks to the official documentation for installing PEAR to make sure that you can install and use PEAR packages. The original instructions can be found here: https://help.opalstack.com/article/132/installing-and-using-pear-packages
After some trial and error, these are the steps that worked for me:
PART ONE: Installing the Pear Package Manager
Log into a SSH session as your application's shell user.
Execute the following commands to download and launch the PEAR installer:
cd ~
wget https://pear.php.net/go-pear.phar
php go-pear.phar
You will be prompted to review the file layout for the PEAR installation:
Below is a suggested file layout for your new PEAR installation. To
change individual locations, type the number in front of the
directory. Type 'all' to change all of them or simply press Enter to
accept these locations.
1. Installation base ($prefix) : /home/shelluser/pear
2. Temporary directory for processing : /tmp/pear/install
3. Temporary directory for downloads : /tmp/pear/install
4. Binaries directory : /home/shelluser/pear/bin
5. PHP code directory ($php_dir) : /home/shelluser/pear/share/pear
6. Documentation directory : /home/shelluser/pear/docs
7. Data directory : /home/shelluser/pear/data
8. User-modifiable configuration files directory : /home/shelluser/pear/cfg
9. Public Web Files directory : /home/shelluser/pear/www
10. System manual pages directory : /home/shelluser/pear/man
11. Tests directory : /home/shelluser/pear/tests
12. Name of configuration file : /home/shelluser/.pearrc
1-12, 'all' or Enter to continue:
- Type in
2
and hit Enter
to change your temporary directory for processing.
- Type in the following:
/home/shelluser/pear/tmp/pear/install
and hit Enter
- The same review will pop up. This time type in
3
and hit Enter
to change your temporary directory for downloads.
- Type in the following:
/home/shelluser/pear/tmp/pear/install
and hit Enter
- The same review will pop up. this time hit
Enter
to apply.
- You will get an error with the following prompt:
Would you like to alter php.ini </etc/php.ini>? [Y/n]
. Select n
and hit Enter
.
Pear will now get installed to the /home/shelluser/pear directory.
PART TWO: Adding Pear to commands to your SSH sessions
- Update your .bash_profile PATH to include the /pear/bin and /pear/share/pear paths.
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$HOME/pear/bin:$HOME/pear/share/pear
- Reload your bash profile in the SSH session:
. ~/.bash_profile
- Confirm this worked by trying the following pear command:
pear list
You should see a return of installed packages.
PART THREE: Change the path directory for caching
You already changed the tmp directory for downloading and processing installation packages, but you still need to update the cache directory. You can do so with the following command line:
pear config-set cache_dir /home/shelluser/pear/tmp/
PART FOUR: Update Pear Channel Settings
Just to be on the safe side, run the command to update your channel settings so that packages can be properly downloaded and installed.
pear channel-update pear.php.net
PART FIVE: Confirm Config Settings and FOLDERS
- Use the following command to show all the configuration settings:
pear config-show
- Pay attention to the following, since these are the ones you changed from the defaults:
PEAR Installer cache directory cache_dir /home/shelluser/pear/tmp/
PEAR Installer download download_dir /home/shelluser/pear/tmp/pear/install
PEAR Installer temp directory temp_dir /home/shelluser/pear/tmp/pear/install
- Then confirm that the /tmp/pear/install folder actually exists. If not, create it.
PART SIX: Install the PEAR Mail package
- Enter the following command:
pear install --alldeps Mail
- After installation, you can confirm the pacakge was installed, enter in the following:
pear list
A list of installed pear packages will be displayed, and should include Mail and its dependencies.
PART SEVEN: Including / Calling Pear Mail
Now that you have Pear Mail installed and available, here's an example of how to incorporate it into your website.
- Create an
php_includes
directory at your home/shelluser/
root directory.
- Create a new
smtpvars.php
file in that directory.
- Edit the
smtpvars.php
file to set the following global variables:
<?php
session_start();
define("MAILBOX_ADDY","name@domain.ext");
define("MAILBOX_SMTP","smtp.us.opalstack.com");
define("MAILBOX_NAME","boxname");
define("MAILBOX_PW","boxpassword");
?>
- To test, create a
testmail.php
file in your website application that you can call by typing: https://domain.ext/testmail.php
- Have the testmail.php be the following:
<?php
set_include_path('.:/home/shelluser/php_includes');
require_once "smtpvars.php";
set_include_path('.:/home/shelluser/pear/share/pear');
require_once "Mail.php";
$from = 'Website Email Name <'.MAILBOX_ADDY.'>';
$to = 'Your Personal Name<yourpersonalemail@address.ext>';
$subject = 'Test Email - Subject Line';
$body = 'Test email message body.';
$host = 'ssl://'.MAILBOX_SMTP;
$port = "465";
$username = MAILBOX_NAME;
$password = MAILBOX_PW;
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
$message = '<p>'. $mail->getMessage() . '</p>';
} else {
$message = '<p>Message sent!</p>';
}
echo $message;
?>
- Go to the web browser and type in :
https://domain.ext/testmail.php
and see what happens!
That's it! Good luck. And if you run into problems or have some suggested variations, feel free to let me know.