Sending Email with PHP mail() on a Windows Server

The Problem

Sending email using PHP on a Windows IIS server is not as simple as it is when using a Linux server. On a typical setup the mail() function probably won’t work without some tweaking.

Why?

When sending email using PHP’s mail() function on a Linux server, PHP uses sendmail to process the email, but the same is not possible on Windows IIS servers. On Windows servers you may be required to specify an SMTP server to connect to, the port and the sending email address.

The Solution

The settings outlined above may already be specified on your php.ini configuration file, but on a shared server that will probably not be the case. Instead you can specify them within your script using PHP’s ini_set() function.

The code below shows how the SMTP server, port and sender email address can be set:

ini_set('SMTP', 'mail.example.com');
ini_set('smtp_port', '25');
ini_set('sendmail_from', 'example@example.com');

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Note: if you want to post PHP or ASP code including the opening tags (<?php etc...) you must use the HTML entity for the left chevron (<) - &lt; - or your code will be stripped out!