Send Mail Through Localhost(WAMP) in PHP

Posted: April 21, 2011 in Stay Hungry Stay Foolish..
Tags: , ,

Hi All “Cntrl+C“iers 🙂 (I am Among you :-)),

I am back with something new to me and willing to share what I learned.

I was looking for sending the mail from my localhost and I found a blog By Mr. CrazyCoder.  And I was delighted to find the way to do this using PEAR Library.

There few dancing DOS COMMANDS which would do your work. At last it will  Make You Shout “EUREKA!!!”

It Goes Like This:

We will be installing PEAR MAIL Package.

  • Move to the directory where WAMP Server or PHP is installed using the command prompt. For example C:\wamp\bin\php\php5.3.0\>
  • Then run “go-pear.bat” file for complete installation of PEAR
  • Edit php.ini file and include the path to pear(include_path = “.;e:\wamp\bin\php\php5.3.0\includes;e:\wamp\bin\php\php5.3.0\PEAR;e:\wamp\bin\php\php5.3.0\PEAR\Mail”) and also edit ini file extension and set extension=php_openssl.dll to enable openssl
  • ph5.3.0\>pear list (this command will list all the installed pear packages)
  • pear download Mail (A zip file will be downloaded)
  • pear install Mail
  • pear install -a Net_SMTP(-a switch for installing all dependencies)
  • Then go to the mail.php file in e:\wamp\bin\php\php5.3.0\PEAR\Mail\mail.php and add this line require_once “e:\wamp\bin\php\php5.3.0\PEAR\Mail.php”;(If not added it shows error that class Mail cannot be found )
  • A note: If you are using php 5.3 then you might get a problem when installing pear. It will say that the command cannot be found… To overcome this you have to download go-pear.phar file and save in with that name in the php5.3.0 directory

Once done installing you can go straight away with the program. Here is the simple mail program that sends mail from php using SMTP

< ?php
include "e:\wamp\bin\php\php5.3.0\PEAR\Mail\mail.php";
$from = " ";//Sender Mail Address
$to =  " ";// Recipient mail address
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://";
$port = "465";
$username = "";
$password = "";// Type your Password Here
$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(“Shout Eureka!! Message Sent successfully !!!!”);
Try The Magic Provided By PEAR PHP…… 🙂
And Yes Do read My Previously Posted Articles…..
Courtesy: Mr. Crazy Coder
  1. bahar says:

    hi , where should i add the line require_once in mail.php file?? i am confused .. i have installed pear and php 5.4.3 , i am using wampserver but i cant send an email , lots of errors …:(( plz help me

  2. Rahul Shinde says:

    Fatal error: Class ‘Mail’ not found in C:\wamp\bin\php\php5.4.3\PEAR\Mail\mail.php on line 51

  3. Rahul Shinde says:

    I sort out the problem Thank for such good tutorial bro….Thanks Once again

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s