Friday, January 29, 2010

How to send mail from Localhost with XAMPP and mercury/32

Had tried to make php mail() work on localhost. But had failed. So gave up. This was around 8 months back. Now again some days back had to use the Php mail function on one project. This time I had the liberty to test my code on an external web server, but i tried again to make it work on my localhost.

A lot of google searching and some simple steps, I could make it work.So here sharing my experience with you. Hope this helps someone like me.

I use XAMPP as my local web server on my Windows machine. For php mail() function to work on the localhost, you need a SMTP mail server.

Requirements

A Web server : Here we use the xampp package. You can download it from here. It is very easy to install and does not require any expertise.
A Mail server : Mercury/32 Mail Transport System is an excellent mail server bundled with XAMPP. So no need to search for any other mail server.
A Mail Client : You need to have a mail client to receive mails sent from localhost or basically from your Mercury/32 mail server. Mail clients include outlook express, thunderbird, Eudoramail etc. For this tutorial I will be using the default mail client available with Windows system , ie the outlook express.

The Steps to Configure Mercury/32 mail server


The following steps will guide you through the working of mail function on localhost.
1) You need to start the localhost web server. The XAMPP in this case.




2) By default XAMPP will not start the mercury mail server. So you need to click on start to start the mercury/32 on the XAMPP control panel.



3) Click on the Admin Button next to mercury on XAMPP control panel. This will open the Mercury/32 Admin Panel. Click on Configuration -> Manage Local users.




4) Create a New User. Let
Username : root
Password : root



5) Next, from the Mercury/32 Admin panel, Go to Configurations -> Mercury SMTP server and make the following Changes.
IP Interface to use : 127.0.0.1
Listen to TCP/IP port : 25
Announce Myself as : 127.0.0.1



6)Next, from the Mercury/32 Admin Panel , Go to Configurations -> Mercury POP 3 Server and fill in the following details.
Listen to TCP port : 110
IP Interface to use : 127.0.0.1




7) Next, from the Mercury/32 Admin Panel, Go to Configurations - > MercuryE SMTP client Configuration , and make the following changes
Identify Myself as : 127.0.0.1
Name server : 127.0.0.1



8) Next step is from the Mercury/32 panel go to Configuration -> Mercury D pop3 client and there create a new Account. Click on “ADD” and then enter the following details for “Edit POP3 mailbox definition” dialog box.
POP3 Host : 127.0.0.1
Username : root
Password : root
And then click on save.




This finishes Configuration of your Mercury/32 Mail transport system.

We now have to configure a mail client, so as to receive mails. We will use the windows default mail client, the outlook express. The following steps will guide you to create a user account on outlook express.

Steps to set up OUTLOOK EXPRESS




1)Open Outlook express and then click on Tools -> Accounts -> Add, and then click on Mail




2)For new Internet account on outlook express , create a new user
Enter display name: root



Enter email Id: root@localhost.com



Then click Next. On the Internet Connection wizard,
Select POP3 for my incoming mail server.
For Incoming mail (POP3, IMAP, HTTP) server, enter
127.0.0.1
For Outgoing (SMTP) server, enter
127.0.0.1



Click on Next and then , enter the following fields
Account name : root
Password : root
Click on finish and you are ready to Go.

Once you have finished creating a account on Outlook express , send a test mail to yourself.




You can now test your php code to check if your script is working successfully or not.You can try this script. Copy the code and save it in a file for eg mail.php. Save this file in your root folder , for xampp it will be in your htdocs folder.

//php code to send mail,
//author : idrish laxmidhar
//Use this code to send a test mail from your localhost.

$to = "root@localhost.com";
$subject = "Hi!";
$body="test".PHP_EOL;
$body.="Hello World. If all went well then you can see this mail in your Inbox".PHP_EOL;
$body.="Regards".PHP_EOL;
$body.="Idrish Laxmidhar".PHP_EOL;
$body.="http://i-tech-life.blogspot.com".PHP_EOL;

$headers = "From: root@localhost.com";

if (mail($to, $subject, $body, $headers)) {
echo("Message successfully sent!
");

} else {
echo("Message delivery failed...
");

}
?>



Run the script. If all went well, the script should execute successfully. Check your email. You should receive an e-mail.




Well this finishes the tutorial. Hope it helps someone out there.
P.S This is my first attempt to write a tutorial. your queries or Suggestions are highly appreciated. Thanks

19 comments:

  1. Good tutorial but the code section looks bigger and seems to need changes. It will be nicer if the screenshots look closer. Material is good. Congratz for your first ever tutorial.

    ReplyDelete
  2. interesting.....!!
    Representation is nice & friendly. I will try with it. Carry on, maaan.

    ReplyDelete
  3. Bravo Sir! I needed to test XAMPP's PHP mail and this explained very well how to configure Mercury.

    I used IncrediMail as a client and it was set up flawlessly.

    Thank you.

    ReplyDelete
  4. that really worked! :) i wasted lots of time to find something useful until i find this - very useful work thank you :)

    ReplyDelete
  5. Great tutorial - nice and simple.

    ReplyDelete
  6. Step 5, there are multiple options for Mercurial SMTP server (ie MercuryS SMTP server). Care to elaborate?

    ReplyDelete
  7. i have configured everything, how you have explained above,also it displays email sent successfully.but when i check my outlook inbox it is empty.and i am not able to sent in other emails also.what is the problem in my settings?

    ReplyDelete
    Replies
    1. Try clicking on the "Send/Receive" button on MS outlook...it should be just below your menus....

      Delete
  8. Bravo bravo! Ngyabonga kakhulu ingisizile...kudala ngicingana nalento ngingazi ukuthi ngizoyithola kuphi nakanjani. ngyabonga mfwethu!

    ReplyDelete
  9. How if i want to send out to yahoo mail or gmail?
    would it still be work?

    i follow those step to set up the SMTP server but still cant send out email to my yahoo mail nor gmail

    ReplyDelete
  10. why i am not able to send email to any other domain like gmail or yahoo?

    ReplyDelete
  11. Thanks for the great tutorial.
    I got a problem I can send a email from AccountA@localhost to AccountB@localhost. But I can't send mail to my gmail account from either AccountA@localhost OR AccountB@localhost.
    Please kindly advice. Many thanks!

    ReplyDelete
  12. Do I have to set up Outlook Express? I only use hotmail and I can't find out where to add the new user..

    ReplyDelete
  13. thanks.
    http://www.dutarentalmedan.com
    http://www.medan88rentcar.com

    ReplyDelete
  14. Good tutorial, had no idea what it was you had me do, but it worked... well almost.

    If you follow this tut, and end up with a failed delivery and the response...
    "SMTP server response: 421 Service not available"

    Check this for this directory > "C:\xampp\MercuryMail\QUEUE"

    Assuming you're xampp install is on C:

    My xampp install never created this directory, if yours is missing after that error, simply create the directory and viola

    Thanks for the tut

    ReplyDelete