Home » PHP/MySQL, Programming Techniques, Web 3.0 » Sending Mails using PHP in localhost on Windows/IIS using smtp4dev


The mail function in PHP doesn’t work in Windows/IIS environment unless you set up an SMTP server and configure it. SMTP server now doesn’t come built in with Windows 7 either.

So here is a quick way for you to be able to send mails from your applications and test the mails you send out from PHP or any other applications using SMTP on your localhost. There are two advantages here. You can verify that the mails you are sending out from your applications are correct in format and content. It also stops sending real emails from your development or local servers avoiding unwanted delivery of mails during development and testing.

Follow the below steps:

  1. Install smtp4dev 2.0.9. Download it from http://smtp4dev.codeplex.com/releases/view/71614
  2. Configure it to run on localhost or or on all hosts and on a Port of your choice. Default port is 25, but change it to something else if mails are not sending.
  3. Run smtp4dev and make sure that it is listening on the port you configured.
  4. Add the following configurations in PHP.INI
    [mail function]
    ; For Win32 only.
    ; http://php.net/smtp
    SMTP = (or the host you configured in smtp4dev); http://php.net/smtp-port
    smtp_port = 25 (or the port you configured in smtpdev) ;
    For Win32 only.
    ; http://php.net/sendmail-from
    sendmail_from = you@yourserver.com

    These settings will be existing in the PHP INI file, so you just have to just modify them.

Now try sending an email from PHP. The mail will be caught by this application and will allow you to open it in Outlook to read the content.

If you don’t want to modify the PHP.INI file, you can define these configuration values runtime as shown below:

ini_set(‘smtp_port’, 25);
ini_set(‘sendmail_from’, ‘test@localhost’);

Hope it helps.

Tags: , , ,

Leave a Reply

Page optimized by WP Minify WordPress Plugin