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 = 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
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:-)
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 .].
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
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.
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???
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.
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.
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
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
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
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?