Over time I used all sorts of solutions to test the email sending functions of my PHP scripts during development on my local Windows machine. They all had some pros and many cons and kept me searching for a better way of testing.
For quite some time my preferred solution was to point my mailserver settings in php.ini to some external email server. But I had to make sure that the outgoing emails were not send to the real-life recipient addresses (jamming my customers email inboxes during my development). So this required script changes to make sure the scripts would recognize the local development system and send emails only to special testing addresses while on that system.
Meanwhile I found a better solution that does not require any changes to the scripts that I am testing. I now use a very small and simple freeware email server running on my Windows development machine. In my php.ini I set up localhost as the standard SMTP server to send emails to. The effect is that all emails sent from my local PHP scripts are transmitted to my little local email server, from where they can be polled locally with any email client program. Relaying is switched off in this server, so the emails can never reach the outside world.
Here are some step by step instructions to reproduce my solution:
Set the PHP SMTP server address to localhost, so PHP does not send out of your local environment. I also prefer to set the SMTP port to something other than 25 to hide the local SMTP port from any outside access:
SMTP = localhost smtp_port = 10025
I use “ArGoSoft Mail Server .NET Freeware”. Although this freeware version is quite limited in its functionality, it is ideal for our purposes.
Go get it from ArGoSoft’s website, install and start it.
[UPDATE 20111123] Unfortunately the software is not available on the ArGoSoft website anymore. But you can easily find it on the web (search for “MailServerFreeSetup.msi”). Here is a working source:
Download, install and start it.
[UPDATE 20121114] After multiple users reported viruses in the download file, I re-visited and found that the above source had tampered with the original file and started to spread an infected version!!! Please use this source instead (thanks to mrbellek for the hint!). And please make sure that the file is of type .msi (not .exe) and that the file size is below 700kb. Please be careful!
Download, install and start it.
[UPDATE 20120813] When the server is up and running you can open the mail server console window from the Windows tray at the right (or bottom) end of the task bar. Look out for this icon: . As the mail server is a simple Windows service, the software does not appear in the list of installed programs under the Windows Start button.
From the console window choose the Options tool and set the SMTP port to the same value that you used in php.ini – you probably want to do the same with the POP3 port:
SMTP Port: 10025 POP3 Port: 10110
Activate “Load Server at Windows Startup” and “Start Automatically when Launched”. Without the second option the mailserver software will be run but the process listening on the mail port will not be started. Leave all other input fields and checkboxes in the Options dialog empty and unchecked. In particular do not allow Relaying, and do not activate SMTP Authentication (if you don’t want to make your life more complicated than necessary).
From the console window choose the “Users and Domains” tool and add all domains and all users that you expect your PHP scripts to send emails to. The ArGoSoft Freeware Mailserver does not distinguish between several domains that you define, so for each user you create all domains will be active:
Example Domains: digilog.de, digilog.info, digilog.biz Users: myself
This setup will accept incoming emails to…
firstname.lastname@example.org, email@example.com, firstname.lastname@example.org
…and all emails will go into the same email account that can be polled by accessing localhost with login data myself and the password that you defined for that user.
Now to poll your emails in your favorite email client, you simply set up a new email account retrieving mail from localhost with the above user data and the special ports that we defined above.
The nice thing about this solution is that the limitations of the freeware version actually turn into an advantage for us. Whatever email addresses your scripts will try to send to, simply set them up in ArGoSoft’s domain and user section and any emails will be intercepted and collected in just a few central accounts. When you later push your scripts to the live server, they will work without any changes and will really send out their emails into the world.