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:

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.

Advertisement

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 [email protected] into your email address so that you can receive the mail if it’s a success.

Contents of email_test.php:

<?php
if(mail([email protected]','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.

In Category: Articles
Show 77 Comments
  • Christian January 31, 2013, 1:32 am

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

  • Marquez September 8, 2012, 12:08 am

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

  • Gorselkiras November 23, 2011, 7:33 pm

    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 May 3, 2012, 7:41 pm

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

    • Vahantpc May 17, 2012, 9:43 am

      I have the same problem 

  • Abc March 6, 2011, 8:35 am

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

  • Blahblah February 4, 2011, 2:57 pm

    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 February 4, 2011, 2:56 pm

    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 October 21, 2010, 10:51 am

    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 March 6, 2011, 8:38 am

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

  • Navin_ragul October 6, 2010, 3:58 pm

    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 October 11, 2010, 2:11 pm

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

  • Rich Miller July 28, 2010, 9:41 pm

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

  • zixmail June 18, 2010, 6:45 am

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

  • Mohambry Appavoo May 7, 2010, 11:00 pm

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

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

  • Mike Ryan February 22, 2010, 6:37 pm

    Worked like a charm. Thank you.

  • Rachel E Dahl : RED January 28, 2010, 3:56 pm

    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 January 25, 2010, 11:00 pm

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

  • Rachel E Dahl : RED January 20, 2010, 12:51 am

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

  • aks December 15, 2009, 5:03 am

    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.

  • aks December 15, 2009, 12:03 am

    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 December 4, 2009, 1:25 pm

    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 October 12, 2009, 11:07 am

    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 May 22, 2009, 6:48 pm

    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 May 22, 2009, 4:48 pm

    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 May 6, 2009, 1:19 am

    afdsfsfa

  • Steve Messer March 17, 2009, 9:50 am

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

    Thanks

  • Ruhani Rabin March 17, 2009, 9:06 am

    Hi steve, I tried with my office email account and it worked and its pop. But I know some mail server might reject the mail most probably because of the mail header. I got some sample script about preparing the header. I will try to hook it up by tomorrow.

    And please if you find a solution for that by googled, feel free to share it here or send me an email using the contact page. I would be happy to include that in this post.

  • Steve Messer March 17, 2009, 8:22 am

    I have a question if you don't mind. I have set this up and it works sort of. In my test script I email three different accounts. I get no errors and I looked into the smtp log files as well.
    Of the three accounts that I send mail to the only one that receives the email is my gmail account.

    Do pop accounts not allow for this type of email? Is so what are the alternates. I am about googled out.

    Thanks

  • Greg February 11, 2009, 10:33 am

    These instructions worked perfectly. Thanks for making this available on the Internet

    — Greg

  • Ruhani Rabin February 5, 2009, 10:43 am

    It could be the problem regarding your relay setup of IIS .. this article was tested on windows 2003 with IIS 7 using default smtp server.

  • Stuck February 5, 2009, 3:40 am
  • Shaunak February 5, 2009, 1:01 am

    Very useful article.
    Thanks for writing it!

  • Ruhani Rabin January 31, 2009, 10:31 pm

    @vani, 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..

  • Ruhani Rabin January 31, 2009, 2:31 pm

    @vani, 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..

  • vani January 31, 2009, 9:29 pm

    Hi all,

    I installed PHP in windows server 2003. All the files are working fine except the mail issue. There is no problem in SMTP. Because I run the JSP project in the same server.In that mail function working fine. So where is the problem? And how can i manage? I set the SMTP server in PHP as

    ; For Win32 only.
    SMTP = localhost
    smtp_port = 25

    Please help me about this?

    Thanks in advance.

  • vani January 31, 2009, 1:29 pm

    Hi all,

    I installed PHP in windows server 2003. All the files are working fine except the mail issue. There is no problem in SMTP. Because I run the JSP project in the same server.In that mail function working fine. So where is the problem? And how can i manage? I set the SMTP server in PHP as

    ; For Win32 only.
    SMTP = localhost
    smtp_port = 25

    Please help me about this?

    Thanks in advance.

  • Nisheeth January 9, 2009, 6:47 pm

    Nice one……..thanks a lot.

  • Nisheeth January 9, 2009, 10:47 am

    Nice one……..thanks a lot.

  • Ruhani Rabin November 24, 2008, 10:33 am

    @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..

  • Ruhani Rabin November 24, 2008, 2:33 am

    @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..

  • Jake November 24, 2008, 10:08 am

    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!

  • Jake November 24, 2008, 2:08 am

    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!

  • Ruhani Rabin November 24, 2008, 9:56 am

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

  • Ruhani Rabin November 24, 2008, 1:56 am

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

  • Jake November 24, 2008, 9:38 am

    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:InetpubmailrootQueue 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?

  • Jake November 24, 2008, 1:38 am

    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:InetpubmailrootQueue 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?

  • chuck October 11, 2008, 1:36 pm

    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. ;>)

  • chuck October 11, 2008, 5:36 am

    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. ;>)

  • chuck October 11, 2008, 8:52 am

    The instruction provided was very good but it still left out what most important. Authentication of the mail server.

    Example:

    user name: [email protected]
    password : abcdefg

    This is whats missing and why I can not send mail
    Other php scripts provide a mail config page but the one I am currently working with does not. It relys on the PHP.ini file.

  • chuck October 11, 2008, 12:52 am

    The instruction provided was very good but it still left out what most important. Authentication of the mail server.

    Example:

    user name: [email protected]
    password : abcdefg

    This is whats missing and why I can not send mail
    Other php scripts provide a mail config page but the one I am currently working with does not. It relys on the PHP.ini file.

  • TG September 29, 2008, 2:50 am

    thanks for the response. I’ve checked event viewer and IIS logs (keep in mind, I’m not very experienced with running my own server yet) but I didn’t see anything that stood out as being related.

    Also, yes, I am using the default smtp server from IIS. I’m not sure where else to look for the failure since the mail fu nction is completing successfully, but I’m just not getting the email… My server is behind a router, do I need any special set up with that?

  • TG September 28, 2008, 6:50 pm

    thanks for the response. I’ve checked event viewer and IIS logs (keep in mind, I’m not very experienced with running my own server yet) but I didn’t see anything that stood out as being related.

    Also, yes, I am using the default smtp server from IIS. I’m not sure where else to look for the failure since the mail fu nction is completing successfully, but I’m just not getting the email… My server is behind a router, do I need any special set up with that?

  • Ruhani Rabin September 29, 2008, 12:51 am

    @chiraajeet and @TG , guys did you check your windows event log and IIS logs? also are you guys using default smtp server from IIS?

  • Ruhani Rabin September 28, 2008, 4:51 pm

    @chiraajeet and @TG , guys did you check your windows event log and IIS logs? also are you guys using default smtp server from IIS?

  • TG September 28, 2008, 6:15 pm

    I am seeing having the same problem as some other people here that are saying the ‘sent successfully’ message is showing, but the email never shows up. Does the ‘from’ email address have to be an address set up on the local SMTP server? I’m not even clear on how you set up your own email for a domain if so…

    Should it matter what email service the from or to email addresses are used with as long as your SMTP server is set up correctly and the php.ini file is set up correctly? I’ve been trying this for a while now with no luck…

  • TG September 28, 2008, 10:15 am

    I am seeing having the same problem as some other people here that are saying the ‘sent successfully’ message is showing, but the email never shows up. Does the ‘from’ email address have to be an address set up on the local SMTP server? I’m not even clear on how you set up your own email for a domain if so…

    Should it matter what email service the from or to email addresses are used with as long as your SMTP server is set up correctly and the php.ini file is set up correctly? I’ve been trying this for a while now with no luck…

  • Chiraajeet September 27, 2008, 6:36 pm

    My php code

    if (mail(“[email protected]”,”Hello”, “testing”))
    {
    echo “mail sent successfully”;
    } else
    {
    die(“mail could not be sent”);
    }

  • Chiraajeet September 27, 2008, 10:36 am

    My php code

    if (mail(“[email protected]”,”Hello”, “testing”))
    {
    echo “mail sent successfully”;
    } else
    {
    die(“mail could not be sent”);
    }

  • Chiraajeet September 27, 2008, 6:32 pm

    Hi, Ruhani Rabin
    Well I had done the same configuration before in my machine with xp the mail is delivered perfectly and I am receiving the mail in my email. But when i used this same configuration(as you explained) on another of my machine with windows server 2003, the mail is delivered, buut i am not receiving the mail in my email address not even in the spam. here is my php code.

    in 2003 “mail sent successfully is showing” but i am not receiving the mail.

    Can you hel me why. this is happening with server 2003?

  • Chiraajeet September 27, 2008, 10:32 am

    Hi, Ruhani Rabin
    Well I had done the same configuration before in my machine with xp the mail is delivered perfectly and I am receiving the mail in my email. But when i used this same configuration(as you explained) on another of my machine with windows server 2003, the mail is delivered, buut i am not receiving the mail in my email address not even in the spam. here is my php code.

    in 2003 “mail sent successfully is showing” but i am not receiving the mail.

    Can you hel me why. this is happening with server 2003?

  • Nightshifter September 2, 2008, 2:58 am

    Okay, now what if you are using a static and not dynamic address on the computer IIS is hosted. I’m using 192.168.1.33 port 8080 ( i must use that because comcast blocks 80 so people can’t host their own website :/)

    so how would you configure your PHP.INI and IIS if IIS is assigned 192.168.1.33 port 8080

    localhost doesn’t work on my machine or 127.0.0.1

  • Nightshifter September 1, 2008, 6:58 pm

    Okay, now what if you are using a static and not dynamic address on the computer IIS is hosted. I’m using 192.168.1.33 port 8080 ( i must use that because comcast blocks 80 so people can’t host their own website :/)

    so how would you configure your PHP.INI and IIS if IIS is assigned 192.168.1.33 port 8080

    localhost doesn’t work on my machine or 127.0.0.1

  • Cam August 25, 2008, 8:20 am

    Hi,
    I’ve been trying to work through configuring local SMTP mail delivery for several weeks. It’s often the smallest point that gets overlooked. Your end to end solution helped me verify where I had gone wrong – thanks a million!

  • Cam August 25, 2008, 4:20 pm

    Hi,
    I’ve been trying to work through configuring local SMTP mail delivery for several weeks. It’s often the smallest point that gets overlooked. Your end to end solution helped me verify where I had gone wrong – thanks a million!

  • Vinod August 5, 2008, 6:08 am

    Hi Ruhani Rabin,
    Million thanks to you.. Your description so good and clear that I, at last got the SMTP server to work with PHP perfectly…

    Thanks again,
    Vinod
    http://emails4all.blogspot.com

  • Vinod August 4, 2008, 10:08 pm

    Hi Ruhani Rabin,
    Million thanks to you.. Your description so good and clear that I, at last got the SMTP server to work with PHP perfectly…

    Thanks again,
    Vinod
    http://emails4all.blogspot.com

  • Eddie Law July 6, 2008, 2:52 am

    Nice to meet you, Ruhani

  • Eddie Law July 5, 2008, 6:52 pm

    Nice to meet you, Ruhani

  • Ruhani Rabin June 19, 2008, 12:41 am

    hi @Mali, if you want to use the example from this article then you will need IIS with SMTP virtual server installed and properly configured. If you want to check whether your server is properly configured check the links below… there are some sample asp code that you can copy paste in a sample.asp file and test out your IIS SMTP server. Also note your server need to have a public IP and most probably it also needs MX record pointers to point mail dispatch request from that server(this thing i’m not too sure) .. well here are some links to .asp code to aid you out for testing out…

    http://quadcomm.com/tips/sendmail.asp
    http://quadcomm.com/tips/sendmail.txt
    http://quadcomm.com/tips/sendCDOSYSmail.asp
    http://www.motobit.com/tips/detpg_send-email-from-asp/

    these examples at least help you to determine your SMTP engine is working properly..

  • Ruhani Rabin June 18, 2008, 4:41 pm

    hi @Mali, if you want to use the example from this article then you will need IIS with SMTP virtual server installed and properly configured. If you want to check whether your server is properly configured check the links below… there are some sample asp code that you can copy paste in a sample.asp file and test out your IIS SMTP server. Also note your server need to have a public IP and most probably it also needs MX record pointers to point mail dispatch request from that server(this thing i’m not too sure) .. well here are some links to .asp code to aid you out for testing out…

    http://quadcomm.com/tips/sendmail.asp
    http://quadcomm.com/tips/sendmail.txt
    http://quadcomm.com/tips/sendCDOSYSmail.asp
    http://www.motobit.com/tips/detpg_send-email-from-asp/

    these examples at least help you to determine your SMTP engine is working properly..

  • Mali June 18, 2008, 9:48 pm

    No, I don’t have any asp page for sending mail. Even so, this machine hosts a web application which use .Net framework, but there is no files in C:Inetpubmailroot directory. I tried to setup the virtual SMTP in IIS, but found out that this Windows 2003 server does not have email service installed. I’m a new bee in open source, and Windows OS, my network admin did not setup a web server with pop3/SMTP services on my machine. Do I need SMTP component to implement PHP Sendmail?

    Mali

  • Mali June 18, 2008, 1:48 pm

    No, I don’t have any asp page for sending mail. Even so, this machine hosts a web application which use .Net framework, but there is no files in C:Inetpubmailroot directory. I tried to setup the virtual SMTP in IIS, but found out that this Windows 2003 server does not have email service installed. I’m a new bee in open source, and Windows OS, my network admin did not setup a web server with pop3/SMTP services on my machine. Do I need SMTP component to implement PHP Sendmail?

    Mali

  • Ruhani Rabin June 18, 2008, 3:49 am

    @Mali, Do you have any asp sample page that can send out email using ASP.Net or asp? If not, you’ve problems with your localhost SMTP configuration, also sometimes some ISP’s block port 25. I didn’t check with WAMP server but maybe I will do a test about WAMP.

  • Ruhani Rabin June 17, 2008, 7:49 pm

    @Mali, Do you have any asp sample page that can send out email using ASP.Net or asp? If not, you’ve problems with your localhost SMTP configuration, also sometimes some ISP’s block port 25. I didn’t check with WAMP server but maybe I will do a test about WAMP.

  • Mali June 18, 2008, 3:12 am

    I have been testing a php script sending e-mail on WAMP server. This same server also runs on IIS, and I made some tweak per your recommendation, restarted IIS and test the script again – it doesn’t work.

    Anything in the network that forgotten to configure? We also have a separate smtp server used in the same network, but I want to test out from the localhost first. Any experience on using WAMP5 server to send mail? Thank you.

  • Mali June 17, 2008, 7:12 pm

    I have been testing a php script sending e-mail on WAMP server. This same server also runs on IIS, and I made some tweak per your recommendation, restarted IIS and test the script again – it doesn’t work.

    Anything in the network that forgotten to configure? We also have a separate smtp server used in the same network, but I want to test out from the localhost first. Any experience on using WAMP5 server to send mail? Thank you.