Published on May 1, 2010

Installing PHP mail() function under Apache on Windows using IIS SMTP service

Making a PHP mail() function work under Windows might be a tricky thing. Even if you install PHP correctly, you still may not be able to send email by using native php mail() function. You are here in the right place, if following code produces "failed" message on your windows PHP installation:

// sample #1 - sendmail test
echo 'starting mail #1 ....';
if(mail('myemail@mydomain.com','test subject','test message')){
	echo('ok');
}else{
	echo('failed');
}

If you experience error message like "SMTP server response: 550 5.7.1 Unable to relay for myemail@mydomain.local in D:\_projects\mailtest.php", then you need to tell SMTP server running on your local machine, that it can accept messages for delivery from PHP mail() function. In another words, the message above says, that PHP is trying to connect to SMTP service on Windows installed at localhost (127.0.0.1) at port 25 (the default port from php.ini), but SMTP declines this request.

NOTE:

SMTP service is installed by default as part of Internet Information Server (IIS) along with FTP service (server) and default websites. In order to run Apache on Windows, the default websites must be manually disabled (Administrative Tools -> Internet Information Services -> Web Sites -> Default Web Site -> STOP) to release PORT 80 in favour of the Apache server.

Default PHP configuration assumes that SMTP server is available at localhost (IP = 127.0.0.1) at port 25 as declared in default php.ini configuration:

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

; For Win32 only.
sendmail_from = phpAdmin@mydomain.local

All settings above are correct. So what causes failure when testing PHP mail() function?

Answer is simple - it's the default security restrictions of the IIS SMTP server! IIS SMTP server will only accept recognized clients. PHP is for SMTP unknown mail client and therefore it rejects all mails PHP will try to post. To fix this, you need to add relay hosts into SMTP properties as follows:

1. Add relay restriction records in SMTP properties

Open Start -> Settings -> Control Panel -> Administrative Tools -> Internet Information Services (IIS). If u don't have IIS installed, follow installation instructions here ». Make sure, you have a SMTP server running and Default Website disabled (Apache should be running on port 80). Open Properties of Default SMTP Virtual Server and active panel ACCESS as follows:

2. Add IP/DNS/Domain records into Relay Restrictions of virtual SMTP IIS server

Go into "Relay restrictions" and add following IP/DNS/Domain records:

  • 127.0.01
  • localhost
  • XLHOME (or to whatever domain computer assigns)

Now testing the PHP mail function from Sample #1 above should produce "OK" message. Now, PHP can connect to IIS SMTP at localhost at port 25 and SMTP shall recognize PHP as mail client with granted access.

3. Setting immediate delivery interval for IIS SMTP

You can also do a little tuning for SMTP IIS server - adjust mail send interval, logging location, etc.

In order to check where your sent emails are going - go to [c:\Inetpub\mailroot\Queue\] and you should see all accepted messages for delivery by IIS SMTP there. If you dont see any messages, they might have been already sent. However, default settings of IIS SMTP will queue emails in a queue directory and send them in a batch process once per 5 - 15 minutes (default installation settings). To make sure, emails are being sent immediatelly, you need to adjust the "Delivery Tab" in SMTP Virtual Server Properties:

  • First retry interval (minutes) - set to 1
  • Second retry interval (minutes) - set to 2
  • Third retry interval (minutes) - set to 3

Now you should have full control over where your PHP mails are going.

Related links:

Got a question?

Synet.sk

Professional development of web applications and custom solutions. Consultancy services.

Demo

Contact


https://synet.sk