9 August 2011

On using PEAR to send emails.

The PHP mail command is kind of outdated -- it does NOT support SMTP
authentication. Thus, some other package is required.

One such package is PEAR.

However, PEAR is not installed with WAMP.  

It's not real hard to install PEAR, and there are instructions on line on how to 
install PEAR. But, par for course, there are missing details in the various
on-line instructions I have encountered. So below is an outline of what I 
dealt with.

1) Use the GO-PEAR.BAT to install. 
    * I installed WAMP to d:\wamp2011, which means go-pear.bat was in 
       c:\wamp\php\php5.3.5.  
    * Before running go-pear, you might need to edit php.ini (in the 
      php5.3.5 directory).
      Change:
	   ;phar.require_hash = On
       to
	   phar.require_hash = Off
    
2) You will be asked to install system or local. No idea which is better, 
   but most instructions say local. I just accepted the default (hit enter)

3) You will be given the chance to change ten or so default locations.
   I accepted the defaults

   caution: you might see the following output (on one line)
      ERROR: unable to unpack phar://C:/wamp/bin/php/php5.3.5/EAR/go-pear.phar/
           PEAR/go-pear-tarballs/Structures_Graph-1.0.2.tar
    You will have to provide this file. See step 8 below.

4) You will be asked if you want to modify PHP.INI.  Say yes.
   Something like this will be added::
	;***** Added by go-pear
	include_path=".;C:\wamp\bin\php\php5.3.5\PEAR"
	;*****
   (the actual path depends on where you installed  Wamp).
   Note: for me, this modified  C:\wamp\php\php5.3.5\php.ini

  * When go-pear completes, you should copy these lines to the PHP.INI file
    in the APACHE directory of WAMP (you can use  the PHP > PHP.INI option in
    the WAMP panel) ... for me, it was in C:\wamp\bin\apache\Apache2.2.17\bin
    Note: you might also want to change the APACHE php.ini file's
          phar.require_hash line.

5) Pear will also create PEAR_ENV.REG file, that should be loaded into the
   windows registry (i.e.; click on its icon).
  * One of the lines is:
	"PHP_PEAR_PHP_BIN"=".\\php.exe"
    I opened PEAR_ENV.REG with a text editor, and made the reference absolute:
    changing it to: "PHP_PEAR_PHP_BIN"="C:\\wamp\\bin\\php\\php5.3.5\php.exe"

6) Copy pear.bat (it was in C:\wamp\bin\php\php5.3.5) to your path (ie.;
   to C:\WINDOWS\SYSTEM32). You might need to do this as an administrator.

7) PEAR, when run from a command prompt, should work.

8) Now install the email package. I used:

   pear install --alldeps mail

   However, if you got the 'unable to unpack phar' error message above, you 
   will need
   to install a directory or two. The following worked for me:
      a) goto the  PEAR directory under php -- for me it was
         C:\wamp\bin\php\php5.3.5\PEAR
      b) if a STRUCTURES subdirectory doesn't exist, create it
      c) copy the dsurvey\tools\pearGraph.zip to the structures subdirectory
      d) unzip pearGraph.zip (in this structures subdirectory)
         Note: pearGraph.zip contains version 1.0.4.

  * The important thing is that a mail.php file will be created in the
    pear directory --  which is why
	include_path=".;c:\wamp\bin\php\php5.3.5\PEAR"
    is needed!
    Note: the include_path seems to be case sensitve (\PEAR should be used 
         rather then \pear) -- make sure it matches that actual name of the PEAR
         sub-directory of php.

You can now write a php file to send an email. For example, you can use the
pearEmailTest.php and pearEmailTest.htm files in dsurvey\tools (after modifying
username and other variables in pearEmailTest.php).

Here is a real simple one
(change the variable values to point to your smtp provider,and your username etc).

.end of document
