Testing PHP mail locally with hMailServer and Outlook on Windows 7

Published on 24.05.2011, by Lubos Dzurik

Since Microsoft removed SMTP server service from Windows 7, it is not possible to use PHP mail() function to send emails via local SMTP service anymore.

This might be a problem if you need to test mailing functionalities or develop nice mail templates. A workaround for this is to setup third party SMTP server configured for local development. Here are steps how to configure such an environment.

Environment setup for local mail development and testing

Fig. 1: Environment setup for local mail development and testing

1. Configure PHP in php.ini

Find your php.ini file and set the section [mail function] as follows. Depending on where you want to install sendmail utility, adjust [sendmail_path] as needed.

[mail function]
; For Win32 only.
;SMTP = localhost
;smtp_port = 25

; For Win32 only.
sendmail_from = wamp@localhost.localdomain

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = c:\wamp\bin\sendmail\sendmail.exe -t

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =

2. Setup sendmail.exe for windows

Sendmail is a small executable utility written for windows to emulate linux sendmail daemon.

  • Download from http://www.glob.com.au/sendmail/sendmail.zip »
  • Unzip into some directory. In this scenario we will assume sendmail installation path:
    "c:\wamp\bin\sendmail\sendmail.exe"
  • Create manually directory "c:\wamp\bin\sendmail\log\"
  • Configure sendmail.ini supplied within unziped directory as follows>

    ; configuration for fake sendmail
    
    ; if this file doesn't exist, sendmail.exe will look for the settings in
    ; the registry, under HKLM\Software\Sendmail
    
    [sendmail]
    
    ; you must change mail.mydomain.com to your smtp server,
    ; or to IIS's "pickup" directory.  (generally C:\Inetpub\mailroot\Pickup)
    ; emails delivered via IIS's pickup directory cause sendmail to
    ; run quicker, but you won't get error messages back to the calling
    ; application.
    
    ; smtp_server=mail.mydomain.com
    ; Uncomment following to deliver emails directly into folder (create folder manually)
    ; smtp_server=c:\wamp\bin\sendmail\delivered-emails\
    smtp_server=localhost.localdomain
    
    ; smtp port (normally 25)
    
    smtp_port=25
    
    ; SMTPS (SSL) support
    ;   auto = use SSL for port 465, otherwise try to use TLS
    ;   ssl  = alway use SSL
    ;   tls  = always use TLS
    ;   none = never try to use SSL
    
    smtp_ssl=auto
    
    ; the default domain for this server will be read from the registry
    ; this will be appended to email addresses when one isn't provided
    ; if you want to override the value in the registry, uncomment and modify
    
    ; set default domain
    default_domain=localhost.localdomain
    
    ; log smtp errors to error.log (defaults to same directory as sendmail.exe)
    ; uncomment to enable logging
    ; ! create directory [c:\wamp\bin\sendmail\log] manually
    
    error_logfile=log/error.log
    
    ; create debug log as debug.log (defaults to same directory as sendmail.exe)
    ; uncomment to enable debugging
    
    debug_logfile=log/debug.log
    
    ; if your smtp server requires authentication, modify the following two lines
    
    auth_username=test@localhost.localdomain
    auth_password=admin
    
    ; if your smtp server uses pop3 before smtp authentication, modify the 
    ; following three lines.  do not enable unless it is required.
    
    pop3_server=
    pop3_username=
    pop3_password=
    
    ; force the sender to always be the following email address
    ; this will only affect the "MAIL FROM" command, it won't modify 
    ; the "From: " header of the message content
    
    force_sender=
    
    ; force the sender to always be the following email address
    ; this will only affect the "RCTP TO" command, it won't modify 
    ; the "To: " header of the message content
    
    force_recipient=test@localhost.localdomain
    
    ; sendmail will use your hostname and your default_domain in the ehlo/helo
    ; smtp greeting.  you can manually set the ehlo/helo name if required
    
    hostname=localhost.localdomain
    	

3. Setup hMailServer

hMailServer is a fully featured open source SMTP service for windows.

  • Download from http://www.hmailserver.com/index.php?page=download »
  • Install hMailServer. During installation, choose:
    • Full installation
    • Use built-in database engine
    • Run hMailServer Administrator
  • Once installed, run hMailServer Administrator console and set:
    • Add a domain localhost.localdomain.
    • Add a catch-all address (Domain -> localhost.localdomain -> Advanced -> Catch-all address) , e.g. test@localhost.localdomain. If an email is sent to this domain but the address of the email does not match any existing account under this domain, the catch-all address will get this email.
    • Add an account, e.g. test@localhost.localdomain.
    • Under Settings -> Protocols -> SMTP -> Delivery of e-mail, specify the host name to be localhost.
    • Under Advanced -> IP Ranges, delete Internet. This step is a safety measure to ensure that your email server will only be used locally.

Note: Parts of the hMailServer installation have been originally published at http://csns.calstatela.edu/wiki/content/cysun/course_materials/hmailserver

hMailServer Administration console - setup up for localhost.localdomain

Fig. 2: hMailServer Administration console - setup up for localhost.localdomain

4. Configure Outlook to download emails from hMailServer

Logon password: the same as login into hMailServer Administrator console.

Microsoft Outlook account setting for connecting to local hMailServer at localhost.localdomain

Fig. 3: Microsoft Outlook account setting for connecting to local hMailServer at localhost.localdomain

Alternatives to hMailServer

» smtp4dev - from project documentation:

Windows 7/Vista/XP/2003/2010 compatible dummy SMTP server. Sits in the system tray and does not deliver the received messages. The received messages can be quickly viewed, saved and the source/structure inspected. Useful for testing/debugging software that generates email.

Sounds great... Unfortunatelly, smtp4dev always crashed when sendmail tried to send out an email (Windows 7). I am not sure why, but I cannot consider this service for stable, since it was able to run 4 instances at the same time after 4 crashes. Of course, you may get luckier than me:-)

Troubleshooting ...

1. Port 25 is not free - cannot start hMailServer service

This is common problem - How to find out what port is taken by what application? Solution:


- execute command line: C:\netstat -a -o -n -b |findstr 0.0:25

This will output status of the port like this:
==========================================================================
  TCP    0.0.0.0:25             0.0.0.0:0              LISTENING       436
==========================================================================
Last number [436] is the PID (process ID) of the service occupying port 25.

Find out what application is that:

- execute command line: C:\tasklist /FI "PID eq 436" /FO TABLE

will output something like:

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
Smtp4dev.exe                   436 Console                    1     22 700 K

meaning that service Smtp4dev is the one blocking out port 25.

You can free up port 25 by ending the process via task console [CTRL+ALT+DEL].

2. removed leading dot character at the begin of the line

This is most likely bug on sendmail.exe side. When connecting to hMailServer from PHP, sendmail may somehow incorrectly prepare binary stream. It has been confirmed by hMailServer staff at [suport forum], that hMailServer does not change received binary stream in any way.

Solution:
Simply never start new line with DOT character [.].
The sequence [\n.] will be converted into [\n] loosing the dot character. You have to put at least one single space (ASCII character #32) before the dot character like this: [\n .].


Comments...

abhijit AT zaloni DOT com

28.07.2011 09:58
# 1 Reply to abhijit AT zaloni DOT com    
 

I have setup everything as per this article. While testing the mail() function, it does not show any error or warning. However I don't get the mails. Will I be able to send mails from localhost to external mail addresses, like GMail, using this method?

abhijit AT zaloni DOT com

28.07.2011 14:51
# 2 Reply to abhijit AT zaloni DOT com    
 

abhijit@zaloni.com wrote on 28.07.2011 09:58:
I have setup everything as per this article. While testing the mail() function, it does not show any error or warning. However I don't get the mails. Will I be able to send mails from localhost to external mail addresses, like GMail, using this method?
Checking the sendmail error log, I saw the following error -
Code:
Invalid recipient <abhijit.coder@gmail.com> No valid recipients were found
Does this mean, some setting is not working?

Lubos

29.07.2011 00:27
# 3 Reply to Lubos    
 

Hi abhijit,

sendmail can connect to any SMTP server if such a server allows for him to log in. It could be even external SMTP service, like Gmail, but in this case you must allow it in Gmail settings and configure sendmail to authenticate with Gmail's credentials.

Regarding the "Invalid recipient" message, my guess is that you may have set invalid authentication credentials for sendmail to connect to hMailServer. To make it work, you must set in sendmail.ini directive "auth_username" to be exactly the name of your account created previously on hMailServer and also "auth_password" to be exactly the same password as for logging into admin console on hMailServer.

You can also turn on logging mode in Administrator console of hMailServer (Status->Logging->Start) to see if your server is actually receiving any messages.

Cheers,
Lubos

abhijit AT zaloni DOT com

03.08.2011 08:01
# 4 Reply to abhijit AT zaloni DOT com    
 

Hi Lubos,

I was busy with works and had not checked your reply. First, thank you. Due to time constraints I have not been able to check it again. I will do it by the weekend and let you know. By the way, this article is the most detailed and useful one I have found about setting up mail on Windows.

Thai Nguyen - tqnguyen333 AT gmail DOT com

18.02.2012 16:53
# 5 Reply to Thai Nguyen - tqnguyen333 AT gmail DOT com    
 

I followed the steps laid out in this tutorial but am still not able to send mails out from my local server. The mail() function is executed without any errors or warnings. I checked the sendmail error log but there was no error log.

In your above response to abhijit, you stated that the "auth_username" must be exactly the name of your account created previously on hMailServer. Is this the account I created in Domains -> Accounts? It seems odd if the answer is yes since I can create multiple accounts under one domain.

I did some googling and saw another post where php.ini was edited a little differently. It had the following:

sendmail_path = "D :\hmailserver\sendmail\sendmail.exe -t -i"

The D drive is capitalized, double quotes are added, and -i is appended at the end of the line. This doesn't work either.

Please tell me what I'm doing wrong???

Thai Nguyen - tqnguyen333 AT gmail DOT com

18.02.2012 17:20
# 6 Reply to Thai Nguyen - tqnguyen333 AT gmail DOT com    
 

By the way I'm using Windows XP. I realized thye line:

sendmail_path = "D :\hmailserver\sendmail\sendmail.exe -t -i"

doesn't affect my case since it's for UNIX.

Thai Nguyen - tqnguyen333 AT gmail DOT com

18.02.2012 17:50
# 7 Reply to Thai Nguyen - tqnguyen333 AT gmail DOT com    
 

Problem is resolved! I commented out the line:

sendmail_path = "D :\hmailserver\sendmail\sendmail.exe -t -i"

and it works nicely. Thanks for this tutorial. It's one of the best ones I've seen.

lubosdz

19.02.2012 22:25
# 8 Reply to lubosdz    
 

Thai Nguyen - tqnguyen333@gmail.com wrote on 18.02.2012 16:53:

In your above response to abhijit, you stated that the "auth_username" must be exactly the name of your account created previously on hMailServer. Is this the account I created in Domains -> Accounts? It seems odd if the answer is yes since I can create multiple accounts under one domain.

Hi Nguyen,

I am glad you managed setting up local hMailServer.

Regarding your question above - "auth_username" must authenticate to the very same account you will be downloading into outlook. Of course, you can authenticate to any other account (under any domain you create on hMailServer), however as far as I know PHP using sendmail.exe is only capable of sending contents to only one single account.

Also please bear in mind that there are more ways leading to the same goal - the setting above worked well for my environment (Windows). For your environment (linux) there might be slightly different settings needed - please consult with appropriate documentation (php mail() function etc.).

Cheers, Lubos

maninder DOT dhiman09 AT yahoo DOT com

07.04.2012 08:56
# 9 Reply to maninder DOT dhiman09 AT yahoo DOT com    
 

HI, as per you above guidelines I installed hMailServer @ my Windows7 Operating System, i am using wampserver, and i also installed sendmail service on wamp, and make changes in php.ini and sendmail.ini as you explained, but still i am not able to test sendmail functionaliy in PHP .

When i check the error loh it shows :
Socket Error # 11001 Host not found

Please help me to resolve me this issue.

Thanks
Maninder Dhiman

lubosdz

09.04.2012 21:24
# 10 Reply to lubosdz    
 

maninder.dhiman09@yahoo.com wrote on 07.04.2012 08:56:
HI, as per you above guidelines I installed hMailServer @ my Windows7 Operating System, i am using wampserver, and i also installed sendmail service on wamp, and make changes in php.ini and sendmail.ini as you explained, but still i am not able to test sendmail functionaliy in PHP .

When i check the error loh it shows :
Socket Error # 11001 Host not found

Please help me to resolve me this issue.

Thanks
Maninder Dhiman

Hi Maninder,

it is quite hard to help - at least you should specify in which step or which application is throwing the error. I never experienced it. However, little of googling produced following link - may be it helps:

http://www.pcmmc.com/socket-error-11001-host-not-found.php

nK0de

11.08.2012 12:11
# 11 Reply to nK0de    
 

After struggling for hours, scouring the internet on how to do this, finally got it working after reading your article. Thank you very much : )

lubosdz

19.08.2013 17:53
# 12 Reply to lubosdz    
 

maninder.dhiman09@yahoo.com wrote on 07.04.2012 08:56:
HI, as per you above guidelines I installed hMailServer @ my Windows7 Operating System, i am using wampserver, and i also installed sendmail service on wamp, and make changes in php.ini and sendmail.ini as you explained, but still i am not able to test sendmail functionaliy in PHP .

When i check the error loh it shows :
Socket Error # 11001 Host not found

Please help me to resolve me this issue.

Thanks
Maninder Dhiman

You need to add one more record into c:\Windows\system32\drivers\etc\hosts like so:
127.0.0.1 localhost.localdomain

This will tell windows, that domain "localhost.localdomain" should point to your local server 127.0.0.1 and does not need to look up for any DNS server to resolve such a domain's IP.

wakhid_nuddin AT yahoo DOT com

19.01.2014 06:42
# 13 Reply to wakhid_nuddin AT yahoo DOT com    
 

I am interesting your article
Thank you for it

test AT localhost DOT localdomain

15.08.2014 12:25
# 14 Reply to test AT localhost DOT localdomain    
 

(Raven)
If you are using MS Outlook as a client to hMailServer for testing purposes, I found it easier to create an email account in hMailServer as per the instructions on this website and give the email account a password in hMailServer. When you setup the Outlook client follow the instructions on this webpage but enter the password you created for the email account not the hMailServer Admin password. When testing the connection make sure the localhost ip range has not been automatically blocked by Auto-Ban (Settings -> Advanced -> Auto-Ban).
You can also check if the localhost IP range has been blocked by hMailServer by looking at the status screen.

test AT localhost DOT localdomain

15.08.2014 15:54
# 15 Reply to test AT localhost DOT localdomain    
 

(Raven)
If you just want to use hMailServe to test PHP mail(), you don't need to download the send mail utility. I sent a mail to a gmail account using the instructions on this website and the following PHP code below. PHP config file is:
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = test@localhost.localdomain

and the PHP code is:


<!DOCTYPE html>
<html>
<body>

<?php
$to = 'your_email_address@gmail.com';
$subject = 'Test Mail';
$message = 'Hello World!';
$headers = 'From: test@localhost.localdomain' . "\r\n" .
'Reply-To: test@localhost.localdomain' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$result_mail = mail($to, $subject, $message, $headers);

IF ($result_mail == TRUE){

echo 'email ok';

}else{

echo 'email not ok';
}

?>

</body>
</html>

lubosdz

16.08.2014 23:29
# 16 Reply to lubosdz    
 

For Windows 7 users, there is possibility to install SMTP server via installing Remote Server Administration Tools for Windows 7 with Service Pack 1:

Quote:
It enables IT administrators to manage roles and features that are installed on remote computers that are running Windows Server 2008 R2 but it can be installed ONLY on computers that are running the Enterprise, Professional, or Ultimate editions of Windows 7 (both 32 or 64).

Read more at how to install and configure smtp server in windows .

Whitney Rogers

02.03.2015 03:02
# 17 Reply to Whitney Rogers    
 

You rock!! Thank you so much for posting this simple tutorial!

Leave your comment..
Email will be converted into something like [michael AT gmail DOT com]
Note: Offensive and unrelated comments will be deleted.
Please enter result from the picture above.