Installation and configuration of local Mail server for Windows (hMail Server)

Featured Image

This is not exactly Magento related post, but it might be useful when you are setting up a dev environment on your local machine. There are many good and free mail servers out there which are relatively easy to install on your Windows machine, but the problems may occur when you try to configure all the bits and pieces of your system so you can actually use your local mail server for something other than producing system errors 🙂 .

When I started working with PHP a long time ago, I was (naturally) looking for a mail server that could be used on my local machine. For some reason that is now long forgotten, I have decided to use hMail Server (freeware). Since it never failed me, it stayed in my standard set of must-have tools. In this post I’ll try to present a step-by-step manual on how to install and configure your system to use hMail Server.

Although this post might be a bit lengthy, please, don’t be scared :). These are all simple steps and if you have any experience as a Windows user, you should be able to skip reading the most of them.

System properties (listed just as a reference)

  • Windows 7 Professional with the latest updates
  • MySQL 5.0.51b
  • Apache 2.2.17

Part 1 – Installation and Database configuration

  • Download hMail Server from their download page. My suggestion is to get the latest production release as it is the most stable one;
  •  Start the installer and pick “Full installation”. That should include Administrative tools which are very useful;
  • When asked, set MySQL as your mail database. You don’t need to create the database manually, installer will do that for you;
  • When you complete the installation wizard, database configuration wizard will appear where you should pick “Create new mail database” option;
  • Copy libmysql.dll to /hMailServer/bin directory;
  • Enter the MySQL server and user info.  You can use localhost as server name and whatever port you used for MySQL (default is 3306);
  • Pick the database service from the dropdown so that hMail server can hook to it. It’s usually listed as “MySQL” but  can be listed as “WAMP Mysql” (or whatever name you used) if you have WAMP installed;
  • Database configuration should be completed now and you can proceed to run the hMail Server Administration tool.

Part 2 – Configuring Windows DNS, port and firewall settings

  • The first thing you need to do is add a couple of entries to the Windows’ hosts file. Click on the Windows Start button and search for Notepad. Right click Notepad.exe and choose “Run as Administrator”. This will ensure that you will be able to save the “hosts” file once you’ve finished adding the changes, since the file is protected by the operating system by default;
  • Open the hosts file in Notepad. It’s located in Windows\System32\drivers\etc\ folder;
  • add the following at the end of the file (you can put anything instead of “localserver”):
127.0.0.1	mail.localserver.com
127.0.0.1	localserver.com
127.0.0.1	smtp.mail.localserver.com
127.0.0.1	pop3.mail.localserver.com
  • Open “Windows Firewall” form Control Panel or by searching for it in the Start menu;
  • Go to “Inbound rules” in the left bar and create a new rule for a Port. Type the port numbers for SMTP, POP3 and optionally IMAP protocols in TCP > Selected local ports field. You don’t need to create 3 rules for each port, because you can add all three of them with a comma separator (for example: 25, 110, 143);
  • Enter the name of the rule. Something like “hMail open ports” should do;
  • If you have any custom firewall installed, either stand-alone or as part of your AV software, you might want to look at your software’s documentation and see how to open ports. The ports you need to open are 25, 110 and optionally 143 if you plan to use IMAP.

Part 3 – Configuring hMail server

  • Create a new Domain in the hMail Administrator and put “mail.localserver.com” in the Domain field;
  • Create one administrator mail account by going to “Domains > mail.localserver.com > Accounts > Add” and choosing “Server” in the “Administration level” field. We will use “master@mail.localserver.com” as an example;
  • Go to the “Delivery of e-mail” tab in the “Settings > Protocols > SMTP” and enter  “mail.localserver.com” in the “Local Host Name” field;
  • Go to “Settings > Advanced > TCP/IP Ports” and configure each of the sub-entries to use 127.0.0.1 as IP address.

That’s it! Your local mail server is now up and running. The only thing you need to do now is to connect your newly created local mail address to some e-mail application. I will post here a quick how-to tips for Thunderbird.

Configuring Thunderbird mail account

  • Go to “Tools > Account settings” in the Thunderbird main menu and create a new account (“Account actions” button, then “Add new account” option);
  • Use “master@mail.localserver.com” e-mail address (the one you have created in the hMail Administration) and set “Outgoing server” to “smtp.mail.localserver.com”;
  • Go to “Server settings” in your newly created mail account branch and use “pop3.mail.localserver.com” as “Server name”. “User name” field on this page should be the full name of your e-mail address, so, it would be “master@mail.localserver.com”

You are now ready to use your local e-mail address for your local Magento installation. If you run into any trouble, you can always use the Diagnostics tool from the Utilities menu  or check for error messages in the Status window of the hMail Administrator. I will also suggest you to explore the Administrator application and set up one of the many useful logging and debugging parameters as well as playing with the options for mail accounts. Of course, you should not forget to take a look at the security options too.

Have fun!


15 comments

  1. hi nikola, im failed setting up thunderbird at the another client, on server side there is going well, but at the client no username or email can be added. dont you have any clue? thx

  2. Thank you for the useful article! As a client, I used mailbird which was enough for my requirement.

  3. Thanks for that. Am I correct that your instructions are for setting up email for example just within your own office. It never goes out to the web. I am trying to setup my server so all my email comes and goes through my own server. I have a domain. and I think I have it configured properly. mail.mywebsite.com is the domain but I can’t figure out what I am supposed to put in my MX record. My current MX Record is inbound.mywebsite.com.netsolmail.net. Am I supposed to use the same name “mail.mywebsite.com” or is that one just going to network solutions? Should I put “mail2.mywebsite.com?(and in hmailserver) in the MX Record and then what am I supposed to put after that instead of networksolutions? How do I point it to my specific server in the MX Record? Do I put “mail2.mywebsite.com.hmailserver.com”? or is there a ip address I am supposed to add to an A record first? When I do the diagnostics in hmailserver, it gives me an external IP address but I dont know what to do with it? Does this make any sense? I hope so. Thanks in advance if you could figure it out. James

  4. Thank you, Nikola. Your instructions are very clear and helped me set up and run my hmailserver. I really appreciate it. I am planning to set up a second hmailserver on another pc as a backup in case the pc that I have my server on has a problem. I have one static ip and a router with multiple internet connections (ethernet ports). How can I configure the second hmailserver? Can you explain with examples, please. if you can. Thanks a lot! Sazze.

  5. Thank you! I needed to setup a mail server on a standalone server for showing demos and your post finally got me up and running.

  6. IM JUST CONFUSED ABOUT CREATNG MX RECORDS IN 2003 SERVER CAN U PLS TELL HOW TO DO THAT FOR HMAIL SERVER

  7. Interesting, but a little bit complicated for such task as collecting mails from dev environment. Agree?

    You try to replace standard unix sendmail with mail server which needs mysql and additional firewall rules. What does every developer really need when testing mail functionality of the web-site? Just send email, store it somewhere and be able to check mail headers for some debug purposes. Your solution with hMail Server allowes all of this, but…

    I prefer just to emulate sandmail on a windows machine.

    <?php
    
    // get email body
    $mail = '';
    
    // where to store our emails
    $fileNameBase = dirName(__FILE__) . '/mails/' . date('Ymd_His');
    $fileName = $fileNameBase . '.eml';
    
    // get email body from the stdin
    $fp = fopen('php://stdin', 'rb');
    while (!feof($fp)) {
        $mail .= fread($fp, 512);
    }
    
    // parse and format headers
    $e = preg_split('~(\r\n\r\n)|(\n\r\n\r)|(\n\n)|(\r\r)~', $mail, 2);
    $headersS = preg_split('~(\r\n)|(\n\r)|\n|\r~', $e[0]);
    $headers = array();
    $body = isset($e[1]) ? $e[1] : '';
    foreach ($headersS as $header) {
        $e = explode(':', $header, 2);
        $name = strtolower(trim($e[0]));
        $value = trim(isset($e[1]) ? $e[1] : '');
        $headers[$name] = $value;
    }
    $headersS = implode("\r\n", $headersS);
    if (!isset($headers['date'])) {
        $headersS = 'Date: ' . gmdate('D, j M Y H:i:s') . "\r\n" . $headersS;
    }
    if (!isset($headers['content-type'])) {
        $headersS = "Content-Type: text/plain; charset=utf-8\r\n" . $headersS;
    }
    if (!isset($headers['mime-version'])) {
        $headersS = "MIME-Version: 1.0\r\n" . $headersS;
    }
    $mail = $headersS . "\r\n\r\n" . $body;
    
    // make sure we store every email during one mail session
    $i = 1;
    while (file_exists($fileName)) {
        $i++;
        $fileName = $fileNameBase . '_' . $i . '.eml';
    }
    
    // save file
    file_put_contents($fileName, $mail);

    Edit php.ini file:

    sendmail_path = "D:\Server\php\php.exe -f D:\Server\mail\sendmail.php"   ; edit for your php installation

    Reload the web server. And now we have all our emails stored at one directory and ability to open them in any mail agent.

  8. Other than HMail, there are Mailenable and SmarterMail free edition which could be an alternate options.

    1. As I said, there are a lot of free Windows mail servers out there. Some of them might even try to do some things listed in this guide for you, but if anything with the installation process goes wrong, you will end up configuring DNS, ports, and mail server rules anyway. This guide was made to help you see the basic steps of local mail server configuration on a practical example 😉

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.