Custom Search
 


How to use PHP and Microsoft SMTP Virtual Server to send emails



IIS has a built-in SMTP Virtual Server that can be used by PHP mail() function to send emails.

If you are connected to Internet, emails will be sent out.

If your computer doesn't have Internet connection, you can still use it to send emails but instead actually sending out emails, they will be stored in C:\Inetpub\mailroot\Queue folder. The email filename would be in a format like NTFS_2b15732801c54a4f00000002.EML

The good thing about this practice is that you can test your PHP mail() function locally and offline. For example, you can double click the email filename and examine subject, body text, To and From address, etc.

The default mail function setup in php.ini looks like this:

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

; For Win32 only.
;sendmail_from = me@example.com


The above code can be left as is. "SMTP = localhost" means you will use local SMTP Virtual Server that came with IIS for email message relay. However, in order for PHP mail() function to work locally, you must do two things:

  1. Always include From address in your PHP mail() function. This is because the sendmail_from was commented out.

    If you left out the From address in mail() function, you will get error because Apache will try to find the default sendmail_from address specified in php.ini file. If found one, it will be used as the From address. If not, error message will be displayed.

    For development, you can make sendmail_from = me@example.com as the default sender address or you can change it to your real email address.

    Comment in the sendmail_from. Remember, always restart Apache server every time you change php.ini file.

  2. Configure SMTP server to allow relay from the local machine. If you don't, you will receive error message similar to this:

    Warning: mail(): SMTP server response: 550 5.7.1 Unable to relay for recipient@test.com in c:documents and settingsadministratormy documentstesttest.php on line 3

Steps to enable SMTP server for local relay

1. Open IIS, right click SMTP Virtual Server. Select Properties from the dropdown menu.
Microsoft SMTP Virtual Server properties

2. Select Access tab and click the Relay button.
Microsoft SMTP Virtual Server Relay

3. The Relay Restrictions window is opened. Click the Add button.
Microsoft SMTP Virtual Server Relay Restrictions

4. When the window opens, type in 127.0.0.1 in IP address field. Then click OK.
Microsoft SMTP Virtual Server Relay Restrictions for 127.0.0.1 only

5. Now local IP address is granted for relaying. Then click OK.
Microsoft SMTP Virtual Server Relay Granted

Note: You can also use your ISP's SMTP server. But for development, testing, and debugging purpose, use localhost should be fine.

[mail function]
; For Win32 only.
SMTP = your ISP's SMTP server
smtp_port = 25

; For Win32 only.
sendmail_from = your email address

How to fix a common error

Here is a common error you may enconter:

mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

Many people have tried to fix this error by changing the default mail function setting in php.ini file.

The default mail function setup in php.ini looks like this:

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

; For Win32 only.
;sendmail_from = me@example.com

The default setting does not have to be changed. Instead, check your SMTP server availability and configuration.

1. Check if SMTP server has been installed.

Microsoft SMTP Virtual Server is bundled with Internet Information Server (IIS). So, if you don't have IIS installed, you don't have SMTP server.

To look for IIS, follow this path from Start menu.

Start -> Settings -> Control Panel -> Administrative Tools -> Internet Services Manager

Internet Services Manager has three basic components: Default FTP Site, Default Web Site, and Default SMTP Virtual Server.

Internet Information Services

If you can't find Internet Services Manager, you need to install it.

Start -> Settings -> Control Panel -> Add/Remove Programs

When the window opens, click Add/Remove Windows Components on the left hand side navigation bar. Next, click Components button to bring up Windows Components Wizard. Find Internet Information Services (IIS) and install it.

2. If SMTP server has already been installed, check if it is running.

SMTP server is stopped

3. If SMTP server is running, check if it has been configured to use TCP port 25.

3.1 Open IIS, right click on SMTP Virtual Server. Select Properties from the dropdown menu. Then, click Advanced button on the General tab.

SMTP server Advanced Properties

3.2 When the new window opens, check the TCP port number.

Port 25 in SMTP server

3.3 Next, click Connection button.

SMTP server connection

3.4 When the new window opens, check the TCP port number. Change it to 25 if it is not.

SMTP server port 25


Copyright© GeeksEngine.com



Related Articles:

1.Steps to install PHP 5.x on Windows as a development machine
2.How to install Apache 2.x web server on Windows
3.How to connect two different versions of MySQL server on the same computer
4.How to configure MySQL server 5.1 on Windows
5.How to install MySQL server 5.1 on Windows with screenshots
6.Five ways to create include path for PHP
7.How to use Date and Time data as integer value in PHP and MySQL
8.How to use Apache Virtual Host to run multiple local websites on Windows
9.Install all PEAR packages by yourself
10.How to install PEAR on Windows
11.How to install PHP server-side scripting language on Windows
12.How to install Apache 1.3 web server on Windows
13.How to install two different versions of MySQL server on the same PC
14.How to configure MySQL server 4.1 on Windows
15.How to install MySQL server 4.1 on Windows with screenshots
16.Export Northwind Access database to MySQL via ODBC


Other Recent Articles from the WAMP & LAMP category:

1.How to resolve Apache web server port 80 access problem
2.The free tools I use to build PHP and MySQL websites
3.Steps to install PHP 5.x on Windows as a development machine
4.How to install Apache 2.x web server on Windows
5.How to connect two different versions of MySQL server on the same computer
6.How to configure MySQL server 5.1 on Windows
7.How to install MySQL server 5.1 on Windows with screenshots
8.How to upgrade from PHP4 to PHP5
9.How to load time zone data for MySQL on Windows
10.How to use Apache Virtual Host to run multiple local websites on Windows
11.Install all PEAR packages by yourself
12.How to install PEAR on Windows
13.How to install PHP server-side scripting language on Windows
14.How to install Apache 1.3 web server on Windows
15.How to install two different versions of MySQL server on the same PC
16.How to configure MySQL server 4.1 on Windows
17.How to install MySQL server 4.1 on Windows with screenshots
18.How to set up DSN in ODBC Data Source Administrator on Windows

Copyright © 2017 GeeksEngine.com. All Rights Reserved.

This website is hosted by HostGator.

No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it.

 
Home | Feedback | Terms of Use | Privacy Policy