Send email from localhost/WAMP server using PEAR Mail and GMail

This article is for those who want to send email using either using WAMP Server 2.0c or older or using actual online server.

While reading and following this article, replace the Apache’s and PHP’s version numbers with what your system have.

I have already mentioned 2 methods for sending email, using PHPMailer and sendmail. PEAR Mail is another method which you can use too. PEAR Mail is similar to PHPMailer but you can use it without downloading and configuring anything if you want to send email from an actual online server because on most of servers, PEAR Mail is already installed.

You can try the following PHP code on actual online server
Send email from localhost/WAMP Server

PEAR and PEAR Mail package are two different things. On an actual online server, PEAR and PEAR Mail are mostly pre-configured and ready-to-use but in WAMP/localhost, we need to install and configure them.

First you need to install PEAR. Go to “c:\wamp\bin\php\php5.2.6\PEAR” and ensure that “go-pear.phar” file exist there; else you will not be able to install PEAR Updated on 9-Mar-2013: follow these instructions to install PEAR. Open command prompt with Administrator rights otherwise some error could occure when installing PEAR. Now follow the steps mentioned on PHP Hints WordPress blog to install and configure PEAR on WAMP.

After installing PEAR, you will need to set Windows’ “path” environment variable value to include the path to the PHP’s folder:

  1. Press Windows+Pause/Break keys to open “System Properties” dialog box/window.
  2. On Windows 7, click on the 4th “Advanced system settings” link from left pane; and in other OS version (XP!), click on “Advanced” tab in the “System Properties” dialog box.
  3. Click on “Environment Variables…” button. Another dialog box should appear.
  4. In the bottom box (System Variables), select “Path” from the list and click on the “Edit…” button. Another dialog box should appear.
  5. Append “;c:\wamp\bin\php\php5.2.6″ at the end in “Variable Value” textbox’ text.

Now you will need to install PEAR Mail package. Write pear install Mail in command prompt. Some TGZ files will be downloaded from http://pear.php.net and uncompressed/installed in appropriate folder. The output of this command would be similar as following:

From the above output of installation of PEAR Mail, you might have also noticed “Net_SMTP” dependency. We will require to install it too. Write pear install Net_SMTP in command prompt. With Net_SMTP, another package Net_Socket will also get installed. This command would output something similar to the following:

Don’t worry about the warning in the above output; we don’t need the Auth_SASL package.

To send HTML formatted email, we can use the Mail_Mime package of PEAR. To install it, write pear install Mail_Mime in command prompt. The output would as following:

 

Now, ensure the following settings from this article:

  1. IMAP Access is enabled in your GMail’s Settings -> Forwarding and POP/IMAP -> IMAP Access.
  2. “ssl_module” module in Apache server is enabled.
  3. “php_openssl”, “php_smtp” and “php_sockets” extensions for PHP compiler are enabled.

You will also need to provide the “include_path” in the “php.ini” file. So, open these 2 files “C:\wamp\bin\php\php5.2.6\php.ini” and “C:\wamp\bin\apache\apache2.2.8\bin\php.ini”. Search for “include_path” and provide path to the PEAR folder as following (see only the last line):

Phew…

Now you can try the following code on localhost/WAMP server:

It must work buddy!

You can find more options/documentation of PEAR Mail at here, and PEAR Mail_Mime at there.

The above example (localhost/WAMP) will send the email in HTML format. To send email in plain text format, remove all lines containing the word “mime”. There are 5 lines containing the word “mime” in the above PHP code.

Update (2-Dec-2012)
Please ensure that 2-steps-verification is disabled in GMail if you are using GMail’s SMTP server to send email (sending email using GMail), otherwise you might get “Email sent” message but the email won’t get sent actually. (If you don’t know about 2-steps-verification, you don’t need to worry about it, it is disabled by default.) Thanks to Naveen.
 

Rate this article

Send email from localhost/WAMP server using PEAR Mail and GMail
6 votes, 4.00 avg. rating (81% score)

5 Comments.

  1. thanks for your help….

  2. hello sir, I have xamp server not wamp , and when I send mail than this mail is send successfully send but I cann’t receive any mail on check in my email id . SO please send all process for xamp server.
    Thank you
    Santosh kumar mittal

    • I haven’t used XAMPP much so I don’t have any idea if there is a simple way similar to WAMP to enable/disable PHP extensions. So, what I can suggest is to directly edit the php.ini (to enable php_openssl, php_sockets and php_smtp extensions of PHP) files.

      If PEAR and/or PEAR Mail and related packages are not installed then install them by following the steps mentioned on our blog article on Install PEAR and PEAR Packages.

  3. Saurav Ranjan Subudhi

    I am getting this error.
    Message was not sent
    PHPMailer Error: SMTP connect() failed.
    What to do :-/

    • You are getting error of PHPMailer and you commented on an article about PEAR Mail. These both things are different, please don’t merge these two.

      Please check the article regarding PHPMailer for your issue.