Testing PHP mail locally with hMailServer and Outlook on Windows 7
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.
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 = firstname.lastname@example.org ; 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:
- 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 email@example.com 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 firstname.lastname@example.org ; 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. email@example.com. 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. firstname.lastname@example.org.
- 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
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.
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:-)
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  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.
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 .].