PHP Sendmail Setup with SMTP IIS and Windows Servers

Did you had problems setting up PHP on a Windows server which is running IIS and also running a SMTP server inside IIS? Have you faced problems sending mail from PHP scripts running on IIS server with IIS SMTP engine? By default IIS doesn’t allow relaying SMTP mails if it comes from 3rd party products. So this problem is related to IIS SMTP “Relay Restrictions”. Sometimes some of my clients are using PHP on windows servers using IIS 6 as the web server. In linux PHP supports native sendmail system from the OS itself. But in windows you need to tweak your way to make PHP able to send out mail from localhost. If you are a .Net developer, you might already know that from ASP.Net codes or even Classic Asp code it’s just as simple as 1,2,3 to send out email or use the IIS local SMTP server. Let me make it simple for you, so that you guys can make use of the localhost server from PHP.

Here are the stuff needed to do :

1 - Install and Configure IIS with PHP:

First of all install and configure PHP 5 as described in the PHP documentation. Try to avoid the windows based binary installer. Download the full package and install it manually.

2 - Test out PHP Pages:

Once you’ve done with the installation. Test it out with a sample php info page. If PHP is able to execute the pages on the server then we are ready for the next step.

3 - Test email sending from PHP:

Now we need a test script to check whether we are able to send email using PHP. Here is a sample script that will aid you out in this situation. Copy the content from this code and save it as email_test.php at your server root. Then try to run the file online (eg. mydomain.com/email_test.php) if it’s returns “not ok” means you are not able to send email out. You can change the ‘user@mydomain.com’ into your email address so that you can receive the mail if it’s a success.

Contents of email_test.php:

<?php
if(mail('user@mydomain.com','test subject','test message')){
      echo('ok');
    }
else{
      echo('not ok');
    }
?>

4 - Configure PHP.INI for SMTP:

Lets stop IIS from the IIS Manager. Now open your PHP.INI file. It could be on your C:\PHP folder or C:\WINDOWS folder. Depends on how you’ve configured IIS and PHP on your system. Once you’ve opened the PHP.INI file with notepad or something, search for the entry called “[mail function]” and set it as below.
php-sendmail-setup-php-ini-iis-01
Save and close the PHP.INI file.

5 - Configure IIS SMTP Relay Restrictions:

Once you’re done with PHP.INI, head over to Internet Information Services Manager. Assume that the IIS is running SMTP server also to send out email. You should be able to see the entry inside IIS manager stated “Default SMTP Virtual Server” or similar items. Now right click and access the properties page from “Default SMTP Virtual Server” as it’s shown below.

php-sendmail-iis-windows-smtp-setup-00

on the properties page move to “Access” tab and click on “Connection” and you will be able to see which server/IP’s are allowed to make connection to the server. If “127.0.0.1” IP is not there on the list, add it using the “Add” button, additionally you may also insert your server IP on the list.

php-sendmail-iis-windows-smtp-setup-02

Once you are done at the “Connections” window, click on OK button to accept the information. Now Click on “Relay” button at the “Access” tab and setup the relay options for the server. Grant the IP’s allowed to relay out mail for the mail server. Make sure “localhost” and IP “127.0.0.1” are granted for relay, addition to that you can also insert your domain name and IP.

php-sendmail-iis-windows-smtp-setup-03

Once you are done with “Relay Restrictions” window, click ok to accept the options and then click “Apply” and “Ok” button respectively to go back to the main screen of IIS Manager.
Now you can restart IIS from IIS manager.

php-sendmail-iis-windows-smtp-setup-04

6 - Try sending email again:

Now for your testing run the email_test.php file again. It should give you the response “ok“. If it’s not please check your SMTP configuration one more time along with PHP server settings and PHP.INI file.

Rate this:
2.5
Like This post? Bookmark it:
  • Digg
  • del.icio.us
  • StumbleUpon
  • BlinkList
  • Facebook
  • Technorati
  • Google
  • Live

Related Topics

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

20 Responses to “PHP Sendmail Setup with SMTP IIS and Windows Servers”

Pages: [4] 3 2 1 » Show All

  1. 20
    Ruhani Rabin Says:

    @Jake, read the SMTP relay options i described above .. you must grant access to the IP’s able to relay.. Usually if you send mail using 127.0.0.1 or localhost.. IIS grants the mail sending without authentication..

  2. 19
    Jake Says:

    My mail server is using authentication. That was one of my previous problems that I just solved. I saw online that somebody was talking about mail relay. Is there anyway to do that?

    I know I would be open to spammers but for now I just need to be able to be send an email from my web server!

  3. 18
    Ruhani Rabin Says:

    Hi @jake, most probably its an authentication issue as @chuck said in the comments.. Is your mail server using authentication?

  4. 17
    Jake Says:

    Hi, I followed everything and I am getting all the same problems. Everything is authenticated, the form is saying “mail sent” but nothing appears in my inbox. When I send the message comes up in C:\Inetpub\mailroot\Queue and when I look in it it just says the to and from and sent and no errors.

    I opened port 25. Followed tutorials diligently but nothing seems to work. I am dead-set on setting this up with IIS SMTP.

    Any ideas?

  5. 16
    chuck Says:

    Well I figured the problem out using your instructions provided. Never the less I have been using MailEnable software to begin with. With that I was able to set my user name and password for the remote mail server there.
    I completely done away with the IIS smtp long ago. Everything seems to be working very well.

    Thankyou for posting such detailed instruction in that it brought my attention to the area that I needed to fix. ;>)

Pages: [4] 3 2 1 » Show All

Leave a Reply