PHP Sendmail Setup with SMTP IIS and Windows Servers

iis php sendmail

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.

Advertisement

Here are the stuff needed to do :

1 – Install and Configure IIS with PHP:

Advertisement

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.

  • Christian

    Thank you!!!!!! Bravo!!!!
    I spent a lot of time to find this simple, but perfect solution :D

  • Marquez

    Que porqueria, has un manual completo o lo mejor sara que ho hagas nada…

  • Gorselkiras

    hi there!
    I can run php mail function and take 1 for return value. But no mail has come to defined mail address. What do you think about the problem?

    • Abhijeet

      Hello Gorselkiras the same problem i am getting. Did you get any solution 

    • Vahantpc

      I have the same problem 

  • Abc

    This article is awesome..Thanx for help..I needed it alot.

  • Blahblah

    these are like the mine bombs .that are hidden and blast suddenly.i just hate those ads on the internet.fuck that guy who invented those ads .

  • Blahblah

    what the fucking fuck are those ads that pop up and dont go away .please remove those ads .i just fucking hate them

  • Super_Duper

    Hi,
    I dit everything above and the SMTP server is working with a telnet connection (like here: http://support.microsoft.com/kb/153119)

    but not with PHP.. Anyone?

    • Guest

      I also tried the same and my my php function is working fine…please check your settings again

  • Navin_ragul

    Hi all i did actually what u have mention i the fourms,

    but i persist an new problem,the emails are kept under the queue folder and not moving out of it.

    can u please let me know y this happening and if any solutions will be appreciable

    im using IIS7 winddows server 2008 R2 and i have installed the SMTP.

    Im not receving any error messages but the emails are kept stored in the QUEUE FOLDER.

    Thanks in Advance,
    D.Naveen Rahul

    • Feng_freeman

      i got same situation like you. anyone can help us? thanks a lot.

  • Rich Miller

    Worked great. Right to the point and helpful. I had everything in order except the relay access.

  • http://zixmailencryption.com/ zixmail

    well, sendmail.exe is a simple windows console application that emulates sendmail's … it doesn't support deferred delivery!

  • Mohambry Appavoo

    Thank you very much Ruhani, your information was very helpful.

    Could help to be able to save a stored email in the sent items.

  • http://twitter.com/m1k3ry4n Mike Ryan

    Worked like a charm. Thank you.

  • http://twitter.com/alaModest Rachel E Dahl : RED

    Hi Ruhani, I found this solution to be the only thing that worked for me to run my PHP website on IIS using sendmail. I plugged the link on my Twitter page to show how much I appreciated it!

    When I got this to work, I noticed that it's been keeping data on the mailroot folder of InetPub. Is there a way for IIS not to store this information? The folder has expanded to more than a gig in just a couple of weeks. I've been reading the contents of the folders, and I've been reading unknown and sketchy looking e-mail addresses. Instead of allowing only my domain and IP relay info, I've allowed all. Could this be a reason why I'm getting them? Is there a way for IIS not to keep this data on the server because I really do not need them.

    Thanks! I'd appreciate your response!!

  • randy

    If the damn google ad wasnt on top of the text, It might be the correct solution.

  • http://twitter.com/alaModest Rachel E Dahl : RED

    Thanks! I've searched around and nothing worked except your solution!

  • http://insurances-loans.com/ aks

    thank u for sharing. this one the interesting article which i read.i need to know about smtp and i got this. thanks again for this.

  • http://insurances-loans.com/ aks

    thank u for sharing. this one the interesting article which i read.i need to know about smtp and i got this. thanks again for this.

  • sswach

    I have tried the instructions but it does not work. The test email comes back not ok. I have IIS setup and smpt it works for ASP pages but the PHP it does not. I do not even get an error in the log files.

  • Jason

    I have this same setup but I'm unable to send email using PHP's mail() function to outside domains, such as yahoo, hotmail, gmail, etc. I can only send emails to accounts within my domain OR using a mail program such as outlook. How do to send emails to external domains like the ones I named above using php mail?

  • James

    Hiya mate, having minor difficulty, i have managed to configure IIS and PHP to make the simple script return “ok”, yet I am not recieving any mail as a result of.

    Double checked relay, and php.ini etc, do you have any other recomendations?

    James

  • James

    Hiya mate, having minor difficulty, i have managed to configure IIS and PHP to make the simple script return “ok”, yet I am not recieving any mail as a result of.

    Double checked relay, and php.ini etc, do you have any other recomendations?

    James

  • dsafds

    afdsfsfa

  • Steve Messer

    Okay I will let you know if I figure something out. I look forword to your response

    Thanks