Search
Close this search box.

Sending email from PHP on Windows using IIS

The blog engine I’m writing for the WinPHP Challenge is really getting shape. At certain points in the application I would like to send an email. Luckily SMTP is supported by IIS. Here’s a small how-to to get it up and running and send an email from your php app using IIS.

Setting Up and Configuring IIS

Start by going to the ServerManager on you Server. Most often this is the server you’ll be running your php website on.

Go to Add features in the Features summary section

Check SMTP Services in the Add Features Wizard and hit install. Now wait for the installation to finish.

Open IIS6 Manager under Administrative Tools -> Internet Information Services 6.0 in the Start Menu. Note that although I’m running Windows Server 2008 with IIS7, the configuration of SMTP makes use of the management console from IIS6.

Under [SMTP Virtual Server], click your right mouse button and select properties from the context menu.

Go to the Access Tab and hit the Relay button.

Add a new entry to the list by clicking the Add button and enter 127.0.0.1 in the single computer entry field.

Hit Ok two times to apply the new settings.

For testing purposes I added the IP of my local machine to the list too. This way I can use the same server to send emails from the code I’m working on.

Configuring Php

To make Php send emails you need to make some configurations. Basically there are two options.

In case you’re only going to use the SMTP server in a single project project and do not want it to interfere with other projects, you can set the configuration from within your php code by adding the following lines anywhere before sending the email:

ini_set('SMTP','localhost' ); 
ini_set('sendmail_from', 'administrator@YourWebsite.com');

Another way, to make global use of these settings, add the next lines to your php.ini file.

[mail function] SMTP = localhost sendmail_from = me@example.com

Make sure you restart your server after making these changes, to be sure they’re loaded.

Sending an Email

The last part is sending an actual email. Sending an email uses the php mail function. The mail function takes at least three parameters:

bool mail(string $to, string $subject, string $message)

Finally, here’s a small example on how to combine it all and send the email:

<?php
ini_set('SMTP','localhost'); 
ini_set('sendmail_from', 'Your.Own@Address.com'); 

$to = 'Someone@somewhere.com';
$subject = 'Example subject';
$body = 'With an example body…'; 

mail($to, $subject , $body);
?>
This article is part of the GWB Archives. Original Author: Timmy Kokke

Related Posts