Installing PEAR Mail Package on MAMP and XAMPP

Steve September 22, 2009 Local Server , , , , with 23 and 0
Home/Local Server/Installing PEAR Mail Package on MAMP and XAMPP

If you want your visitors to be able to send you an email from your site there are several options including mailto, sendmail or Postfix. There are also numerous sites offering contact forms for free. For a while I was using the excellent swiftmailer which performed perfectly on both my local and remote servers for a while, but subsequently ceased working on my remote server (presumably because of some configuration change by my hosting provider which I never did get to the bottom of).

UPDATE 21st Feb 11: If you have a problem installing PEAR Mail please check the comments below for a possible solution.

A great alternative is the PEAR Mail package. Both MAMP and XAMPP come with the PEAR framework pre-configured, so installing the Mail package is simple. As of this writing the current PEAR Mail package is Mail-1.1.14, but a newer version maybe available here.

 

1. Installing on MAMP

In MAMP, the PEAR framework resides in the /Applications/MAMP/bin/php5/lib/php/PEAR/ folder. Packages are installed in the /Applications/MAMP/bin/php5/lib/php/ folder. To install the Mail package, open Terminal and type

/Applications/MAMP/bin/php5/bin/pear install -a Mail-1.1.14

The -a flag instructs the installer to install all dependencies. For Mail these are currently Net_Socket-1.0.9, Auth_SASL-1.0.3 and Net_SMTP-1.3.3. Three new folders will be created in the /Applications/MAMP/bin/php5/lib/php/:  Auth/, Mail/ and Net/. To ensure the installation was successful and you can send mail see Sending a Test eMail.

If you later want to uninstall the Mail package, open Terminal and type

/Applications/MAMP/bin/php5/bin/pear uninstall Mail-1.1.14 Net_Socket-1.0.9 Auth_SASL-1.0.3 Net_SMTP-1.3.3

 

2. Installing on XAMPP

For XAMPP the installation process is virtually identical but with two notable differences. First, the PEAR framework resides in the /Applications/XAMPP/xamppfiles/lib/php/pear/PEAR/ folder. Additional packages are installed in the /Applications/XAMPP/xamppfiles/lib/php/pear/ folder. Secondly, we need root access to install the Mail package so we have to prefix our Terminal commands with sudo. To install the Mail package, open Terminal and type

sudo /Applications/XAMPP/xamppfiles/bin/pear install -a Mail-1.1.14

The -a flag instructs the installer to install all dependencies. For Mail these are currently Net_Socket-1.0.9, Auth_SASL-1.0.3 and Net_SMTP-1.3.3. Three new folders will be created in the /Applications/XAMPP/xamppfiles/lib/php/pear/:  Auth/, Mail/ and Net/. To ensure the installation was successful and you can send mail see Sending a Test eMail.

If you later want to uninstall the Mail package, open Terminal and type

sudo /Applications/XAMPP/xamppfiles/bin/pear uninstall  Mail-1.1.14 Net_Socket-1.0.9 Auth_SASL-1.0.3 Net_SMTP-1.3.3

 

3. Sending a Test eMail

To ensure you can send an email copy the code below and save it as a file in the root directory of your site. In this example I’ll save the file as PEARMailTest.php in the root directory of mysite. You’ll need to change the values for $from, $to, $host, $username, $password and optionally $port to match your email configuration.

<?php
require_once "Mail.php";

$from = "Name <user@domain.com>";
$to = "Name <user@domain.com>";
$subject = "[Site Message]";
$body = "PEAR Mail successfully sent this email.";

$host = "outgoing.mailserver.net";
$username = "user@domain.com";
$password = "password";
$port = "80";

$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)) {
  echo("<p>" . $mail->getMessage() . "</p>");
 } else {
  echo("<p>Message successfully sent!</p>");
 }
?>

Next, enter the URL http://local.mysite/PEARMailTest.php* in your browser. If successful, you should receive an email.

*This example assumes you have configured a virtual host in either MAMP or XAMPP for mysite called local.mysite. If you don’t use virtual hosts, simply point your browser to http://localhost/PEARMailTest.php.

 

About the Author

A native Brit exiled in Japan, Steve spends too much of his time struggling with the Japanese language, dreaming of Fish & Chips and writing the occasional blog post he hopes others will find helpful.

23 Comments

  • James Rylance
    on November 12, 2010Reply

    I found that stating a version in install failed, and the following allowed the latest version to be install using MAMP. Thanks very much for leading me in the right direction.

    /Applications/MAMP/bin/php5/bin/pear install -a Mail

  • Spike
    on January 26, 2011Reply

    I don’t understand a thing… What values should I enter for $from, $to, $host, $username, $password and $port?
    Thanks a lot.

    • Steve
      on January 27, 2011Reply

      Hi Spike

      What values should I enter for $from, $to, $host, $username, $password and $port?

      $from is the sender’s name and email address.
      $to is the recipient’s name and email address.
      $host is your SMTP (outgoing) mail server.
      $username is the account name used to login into the SMTP mail server.
      $password is the password associated with $username.
      $port is the port number used by the SMTP mail server. The default port is 25 but this may vary depending on your ISP.

      If you’ve already configured a mail client like Outlook in Windows or Mail in OS X you should already have this info. If not you’ll need to contact your ISP.

      Regards, Steve.

  • Rui Dias
    on January 29, 2011Reply

    Hello Steve, first i want to thank you for this little tutorial that almost save me life!
    But i still have a problem that i need your help!

    First i have MAMP 1.9.4 Installed, then i install MAIL from Pear like you said but 1.2.0 ver.
    In the test i pick up your code and change it to my needs but when i run it i don’t have an erro or the successfully echo, and don’t receive any mail! :(
    i will show you my code so you can see whats wrong if could off course!

    <?php
    require_once "Mail.php";

    $from = "Exemplo “;
    $to = “Name “;
    $subject = “[Site Message]“;
    $body = “PEAR Mail successfully sent this email.”;

    $host = “ssl://smtp.gmail.com”;
    $username = “xequemate@gmail.com”;
    $password = “password”;
    $port = “465”;

    $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)) {
    echo(“” . $mail->getMessage() . “”);
    } else {
    echo(“Message successfully sent!”);
    }
    ?>

    P.S.
    I have to change something in php.ini or have a mail.php on the same folder of the test file?
    And where is the mail file after installed?

    Thanks a lot!

    • Steve
      on January 30, 2011Reply

      Hi Rui,

      I haven’t gotten to the bottom of this yet, but MAMP’s folder structure has changed sometime between v1.8.4 and v1.9.4.

      The /Applications/MAMP/bin/php5/ folder no longer exists and has been replaced with two other folders: /Applications/MAMP/bin/php5.2/ and /Applications/MAMP/bin/php5.3/. So, the /Applications/MAMP/bin/php5/bin/pear install -a Mail command throws a No such file or directory error in Terminal.

      PHP 5.3 is the default in MAMP 1.9.4 so changing the command to /Applications/MAMP/bin/php5.3/bin/pear install -a Mail-1.2.0 works, but installs the package in /Applications/MAMP/bin/php5/lib/php/.

      If I now run the PEARMailTest.php file I get the same results you did: no email or error message. But, if I check the /Applications/MAMP/logs/php_error.log file I see this fatal error:

      require_once() : Failed opening required ‘PEAR.php’ (include_path=’.:/Applications/MAMP/bin/php5/lib/php’) in /Applications/MAMP/bin/php5/lib/php/Mail.php on line 46.

      I’m not sure if this is simply a problem with paths and file locations or more involved. When I have time, I’ll do some more checking.

      Regards, Steve.

  • Rui Dias
    on February 1, 2011Reply

    Thanks for the fast reply Steve! I will wait for your review of the problem!
    Thanks
    Best Regards

    • Steve
      on February 2, 2011Reply

      Hi Rui,

      MAMP 1.9.4 comes with PHP 5.3.2 as the default. So, to install PEAR Mail type the following in Terminal.

      /Applications/MAMP/bin/php5.3/bin/pear install -a Mail

      This will create the directory /Applications/MAMP/bin/php5/lib/php/ with the sub folders Auth, doc, Mail, Net and Test and the Mail.php file.

      Next, open the /Applications/MAMP/conf/php5.3/php.ini file and locate the include_path directive on or around line 405:

      ; UNIX: "/path1:/path2"  
      include_path = ".:/Applications/MAMP/bin/php5.3/lib/php"
      

      Add the /Applications/MAMP/bin/php5/lib/php directory to it so it becomes

      ; UNIX: "/path1:/path2"  
      include_path = ".:/Applications/MAMP/bin/php5/lib/php:/Applications/MAMP/bin/php5.3/lib/php"
      

      You may also want to make a similar change to the /Applications/MAMP/conf/php5.2/php.ini file so that the include_path directive is changed to:

      ; UNIX: "/path1:/path2"  
      include_path = ".:/Applications/MAMP/bin/php5/lib/php:/Applications/MAMP/bin/php5.2/lib/php"
      

      Restart MAMP’s Apache server and try running the test mail again.

      Regards, Steve.

  • Rui Dias
    on February 3, 2011Reply

    Hello Steve, one more time thanks!
    But unfortunately the problem continues!
    When i execute “/Applications/MAMP/bin/php5.3/bin/pear install -a Mail” the terminal tell me that mail is all ready install with the same version and there is no update to it, and i don’t have this directory “/Applications/MAMP/bin/php5/lib/php/” or the another folders like mail or NET…
    i all ready deleted de MAMP and install it again but is the same thing over and over again:(

    Best Regards
    This is so strange that give migraines :(

    • Steve
      on February 3, 2011Reply

      Hi Rui,

      When i execute “/Applications/MAMP/bin/php5.3/bin/pear install -a Mail” the terminal tell me that mail is all ready install with the same version and there is no update to it…

      I would first move your existing MAMP folder in /Applications to the trash and install a fresh copy of MAMP 1.9.4.

      Before doing anything else, open Terminal and type these 3 seperate commands:

      pear list -a
      /Applications/MAMP/bin/php5.2/bin/pear list -a
      /Applications/MAMP/bin/php5.3/bin/pear list -a

      This will show which, if any PEAR packages are installed. At this stage the output from all 3 commands should be

      Installed packages, channel __uri:
      ==================================
      (no packages installed)
      
      Installed packages, channel doc.php.net:
      ========================================
      (no packages installed)
      
      Installed packages, channel pear.php.net:
      =========================================
      (no packages installed)
      
      Installed packages, channel pecl.php.net:
      =========================================
      (no packages installed)
      

      Let me know what results you get and we’ll take it from there.

      Regards, Steve.

  • Rui
    on February 10, 2011Reply

    Hi Steve,

    this is what appears,

    Installed packages, channel __uri:
    ==================================
    (no packages installed)

    Installed packages, channel doc.php.net:
    ========================================
    (no packages installed)

    Installed packages, channel pear.php.net:
    =========================================
    Package Version State
    Archive_Tar 1.3.7 stable
    Auth 1.6.4 stable
    Auth_SASL 1.0.4 stable
    Config 1.10.12 stable
    Console_Getopt 1.3.0 stable
    HTML_Template_IT 1.3.0 stable
    MDB2 2.4.1 stable
    MIME_Type 1.2.1 stable
    Mail 1.2.0 stable
    Mail_Mime 1.8.1 stable
    Mail_mimeDecode 1.5.5 stable
    Net_SMTP 1.4.4 stable
    Net_Socket 1.0.10 stable
    PEAR 1.9.1 stable
    PEAR_Frontend_Web 0.7.5 beta
    Structures_Graph 1.0.4 stable

    Installed packages, channel pecl.php.net:
    =========================================
    (no packages installed)

    Regards

    • Steve
      on February 12, 2011Reply

      Hi Ruis,

      Type these individual commands in Terminal:

      pear config-get php_dir
      /Applications/MAMP/bin/php5.2/bin/pear config-get php_dir
      /Applications/MAMP/bin/php5.3/bin/pear config-get php_dir
      sudo /Applications/XAMPP/xamppfiles/bin/pear config-get php_dir

      They’ll return the directory where PEAR installs .php files. They should be different, but I expect they’re the same.

      Let me know.

      Regards, Steve.

  • Rui Dias
    on February 16, 2011Reply

    Helo Steve,

    They are all the same, but you can see:

    Last login: Tue Feb 15 18:01:55 on ttys000
    MacBook-Pro-15-de-Rui-Dias:~ RuiDias$ pear config-get php_dir
    /usr/local/PEAR
    MacBook-Pro-15-de-Rui-Dias:~ RuiDias$ /Applications/MAMP/bin/php5.2/bin/pear config-get php_dir
    /usr/local/PEAR
    MacBook-Pro-15-de-Rui-Dias:~ RuiDias$ /Applications/MAMP/bin/php5.3/bin/pear config-get php_dir
    /usr/local/PEAR
    MacBook-Pro-15-de-Rui-Dias:~ RuiDias$ sudo /Applications/XAMPP/xamppfiles/bin/pear config-get php_dir
    Password:
    /usr/local/PEAR
    MacBook-Pro-15-de-Rui-Dias:~ RuiDias$

    Best Regards

    • Steve
      on February 16, 2011Reply

      Hi Rui,

      You have three PEAR installs: Mac OS X, MAMP and XAMPP. Each install has its own individual PEAR system configuration file named pear.conf. This file contains the php_dir setting which is different for each install. However, all three installs share a single PEAR user configuration file named ~/.pearrc which also contains a php_dir setting. In your case this setting is /usr/local/PEAR which corresponds to the Mac OS X install.

      The php_dir setting in ~/.pearrc appears to always take priority over that in the pear.conf file. Consequently, when you try to install PEAR Mail for MAMP with the command /Applications/MAMP/bin/php5.3/bin/pear install -a Mail it is trying to install Mail in /usr/local/PEAR. As this directory already contains a PEAR Mail install you’ll get the message similar to pear/Mail is already installed and is the same as the released version 1.2.0 install failed.

      There are two solutions. The first is to delete the ~/.pearrc file forcing the correct php_dir setting in the individual pear.conf file to be used.

      However, I would recommend the second solution for now:

      In Terminal type:

      pear config-set php_dir '/Applications/MAMP/bin/php5/lib/php'
      

      You’ll get the message config-set succeeded. Hereafter all PEAR packages will be installed in the /Applications/MAMP/bin/php5/lib/php directory.

      Now follow the instructions in one of my previous comments posted on February 2nd that begins ‘MAMP 1.9.4 comes with PHP 5.3.2 as the default. So, to install PEAR Mail type the following in Terminal…’ to install PEAR Mail in MAMP.

      Regards, Steve.

  • Rui Dias
    on February 18, 2011Reply

    Hi Steve, i want to thank you from the hart for the help and patience that you had all this time :)

    Thanks

    Best Regards, Rui Dias

    P.s.:I will continuing following your Blog.

    • Steve
      on February 18, 2011Reply

      You’re welcome Rui.

      Regards, Steve.

  • Leo
    on March 16, 2011Reply

    Sr Rui Dias, poderia me explicar o que está tentando fazer com MEU endereço de email???

  • Stan
    on May 6, 2011Reply

    THERE IS MORE TO THE STORY…

    OK… Steve got close but with his help I was able to figure this out. Indeed, following Steve’s advice in his Feb 16 post I reset the path so these terminal commands…

    pear config-get php_dir
    /Applications/MAMP/bin/php5.2/bin/pear config-get php_dir
    /Applications/MAMP/bin/php5.3/bin/pear config-get php_dir

    … all presented the correct path…

    /Applications/MAMP/bin/php5/lib/php

    At this point one would think, with the correct path… things would work correctly.

    NOT SO… there seems to be THREE more issues.

    First, Mail.php is dependent on Net_SMTP, which is really a file called SMTP.php located here…

    /Applications/MAMP/bin/php5/lib/php/Net/SMTP.php

    So, BEFORE you try to “require_once” the Mail.php file you must first require_once the SMTP.php file but you MUST USE THE EXACT PATH…

    require_once “/Applications/MAMP/bin/php5/lib/php/Net/SMTP.php”;

    Second, SMTP.php itself is requiring Socket.php as a dependency but the relative path is wrong so you must EDIT that SMTP.php file by changing the line…

    require_once ‘Net/Socket.php';

    to read…

    require_once ‘Socket.php';

    Finally, you can now require your Mail.php file BUT IT ALSO MUST SPECIFY THE EXACT PATH…

    require_once “/Applications/MAMP/bin/php5/lib/php/Mail.php”;

    Now, everything will work fine.

    It’s enough to drive you insane, isn’t it!

    Cheers, Stan

  • Stan
    on May 6, 2011Reply

    One More Time…

    In my reply above I provided a solution that worked for me but have since truly figured it out. That solution WILL work but this is the correct solution…

    First… I am looking for a solution for MAMP PRO… not plain MAMP.

    MAMP PRO generates it’s php.ini files dynamically from templates. So, run MAMP PRO and under the File > Edit Template choose the template you want to edit. I’m using php5.3 so I chose that template. It will open in MAMP PRO’s own editor.

    Find line 406 (or there abouts) and make it look like this…

    include_path = “.:/Applications/MAMP/bin/php5.3/lib/php:/Applications/MAMP/bin/php5/lib/php”

    Do a save, restart your apache server and that’s it.

    The path to the Mail is now included and all works fine. No need to specifically include the SMTP.php file (or modify it).

    All should work as advertised.

    Regards, Stan

    • Steve
      on May 6, 2011Reply

      Hi Stan,

      Glad you finally got it working and thanks for all the additional info which I know will prove invaluable for others.

      Regards, Steve.

  • Chris
    on March 28, 2012Reply

    Thanks a lot for the tutorial Steve; I’d spent nearly a whole day banging my head against the desk before finding this site :)

    Just in case anyone else finds themselves in the same situation, I had problems getting PEAR mail to install on MAMP 2.0.2, and finally sorted it by deleting the pear.conf file from /Applications/MAMP/bin/php/php5.3.6/conf directory before running the /Applications/MAMP/bin/php/php5.3.6/bin/pear install -a Mail-1.2.0 command in terminal. Then, as advised above, I made sure that the include_path included :/Applications/MAMP/bin/php/php5.3.6/lib/php

    I haven’t got it all up and running yet, but at least the package is there and Mail.php is being found OK. Phew!

    Cheers,

    Chris

    • Steve
      on March 28, 2012Reply

      You’re welcome Chris. And thanks for the additional information.

      Regards, Steve.

  • John
    on April 24, 2013Reply

    Hi Steve,

    I have been trying to install pear to MAMP 2.1 to PHP 5.4.4 folder for more than six hours, I was just about the give up then I just saw Chris’ answer “deleting the pear.conf” file worked for me too.

    Thanks for tutorial please update your tutorail and include Chris’ answer :)

    • Steve
      on April 24, 2013Reply

      @John,

      Thanks for the reminder. The entire article needs a bit of an update.

      Regards, Steve.

Add comment

©Tech Otaku 2009 - 2014