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

7 Comments

  1. Ritesh

    when i used the above code the got the following error…..

    Warning: mail() [function.mail]: Failed to connect to mailserver at "testewebvision.com@gmail.com" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\inetpub\vhosts\happyenvironment.org\httpdocs\contect_mail.php on line 36
    FailurePHP Warning: Unknown(): Unable to load dynamic library 'C:\Program Files (x86)\Parallels\Plesk\Additional\Php\extensions\php_mcrypt.dll' - The specified module could not be found. in Unknown on line 0

  2. Barnaby Knowles

    Hi Ritesh,

    The first error looks like you’ve set the SMTP mail server address incorrectly. The error message indicates that you’ve set it to an email address rather than a host name. You need a functioning SMTP server for this to work.

    The second error looks like a PHP module is missing. You can either disable the MCrypt library in php.ini or download and instal the module to fix this error.

    Regards,

    Barnaby

  3. Harish

    Getting error. – Warning: mail() [function.mail]: SMTP server response: 550 Access denied – Invalid HELO name (See RFC2821 4.1.1.1) in

  4. Barnaby Knowles

    Hi Harish,

    It sounds like your mail server is not setup with a FQDN (fully qualified domain name), as required by RFC2821.

    Check your mail log and there should be the initial HELO initiation, which will list the hostname that your mail server is connecting as. Look for something like “localhost” or a hostname with no .com, .net extension to confirm that this is the issue.

    You need to set the hostname to something like “mail.example.com” rather than an IP address or “localhost”, for example.

    Regards,

    Barnaby

  5. Nadimuthu

    Nice post..I have one doubt in mail() function. will i send a mail without headers parameter in mail() function? please explain to me.

  6. Barnaby Knowles

    Hi Nadimuthu,

    I’m not sure I understand what you’re asking.

    You can add headers to the email using the mail() function’s additional_headers optional parameter. You must include the From header in emails, and this can either be set in php.ini or using the additional_headers parameter. Other than that you do not have to add any other headers if you do not want to.

    See http://www.php.net/manual/en/function.mail.php for details.

    Regards,

    Barnaby

  7. how to send email in php using windows server

Leave a Reply

Your email address will not be published. Required fields are marked *