Installing PEAR Mail Package on MAMP and XAMPP

I

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. 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.

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.

 

1. Installing on MAMP

In MAMP, the PEAR framework is located in sub-directories of the /Applications/MAMP/bin/php directory. Each sub-directory represents a different PHP version. Which PHP version MAMP uses is set in MAMP’s preferences and can be retrieved from the command-line using the defaults tool:

Dummy Content
defaults read de.appsolute.MAMP phpVersion
7.3.1

 

 

If – for example – MAMP is configured to use PHP 7.3.1, the PEAR framework is located in the /Applications/MAMP/bin/php/php7.3.1/bin directory with PEAR packages installed in the /Applications/MAMP/bin/php/php7.3.1/lib/php directory. To install the Mail package in the correct PHP directory without knowing that directory in advance, open Terminal and type:

Dummy Content
/Applications/MAMP/bin/php/php$(defaults read de.appsolute.MAMP phpVersion)/bin/pear install -a Mail

 

 

The -a flag instructs the installer to install all dependencies. For Mail these are currently Auth_SASL, Net_SMTP and Net_Socket. Three new sub-directories will be created in /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

Dummy Content
/Applications/MAMP/bin/php/php$(defaults read de.appsolute.MAMP phpVersion)/bin/pear uninstall Auth_SASL Mail Net_SMTP Net_Socket

 

 

2. Installing on XAMPP

In XAMPP, the PEAR framework is located in the /Applications/XAMPP/xamppfiles/bin directory and the current version ships with Mail – along with many other PEAR packages – already installed in the /Applications/XAMPP/xamppfiles/lib/php directory. To upgrade Mail and its dependencies, open Terminal and type:

Dummy Content
sudo /Applications/XAMPP/xamppfiles/bin/pear upgrade -a Mail

Note that we need to prefix the command with sudo. Not doing so will result in the upgrade failing.

The -a flag ensures that Mail‘s dependencies are also upgraded. These are currently Auth_SASL, Net_SMTP and Net_Socket. To force the upgrade of Mail and its dependencies change the -a flag to -af.

The relevant directories are located in the/Applications/XAMPP/xamppfiles/lib/php/pear/ directory:  Auth, Mail and Net. To ensure the installation was successful and you can send mail see Sending a Test eMail.

Unlike the package installation in MAMP, I’d not recommend uninstalling Mail and its dependencies in XAMPP because of the other PEAR packages that are pre-installed. For example, the Mail_Queue package requires Mail, and Net_Socket is also required by the Net_POP3 and HTTP_Request packages to name but two.

 

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 $sender, $recipient, $server, $username, $password and optionally $port to match your email configuration.

Dummy Content
<?php

	// Include the Mail package
	require_once "Mail.php";

	// Identify the sender, recipient, mail subject, and body
	$sender    = "Name <user@domain.com>";
	$recipient = "Name <user@domain.com>";
	$subject = "[Site Message]";
	$body = "PEAR Mail successfully sent this email.";

	// Identify the mail server, username, password, and port
	$server   = "outgoing.mailserver.net"
	$username = "user@domain.com";
	$password = "password";
	$port     = "465";

	// Set up the mail headers
	$headers = array(
		"From"    => $sender,
		"To"      => $recipient,
		"Subject" => $subject
	);

	// Configure the mailer mechanism
	$smtp = Mail::factory("smtp",
		array(
			"host"     => $server,
			"username" => $username,
			"password" => $password,
			"auth"     => true,
			"port"     => $port
		)
	);

	// Send the message
	$mail = $smtp->send($recipient, $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 1 in your browser. If successful, you should receive an email.

1 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 responses

Leave a Reply to Spike Cancel reply

23 Comments

  • 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 🙂

  • 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

  • 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

    • 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.

  • 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

  • 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.

  • 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

    • 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.

  • 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

    • 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.

  • 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 🙁

    • 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.

    • 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.

  • 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!

    • 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.

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

    • 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.

  • 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

Steve

Recent Comments

Recent Posts