How to set up SMTP server on Localhost-Configure php.ini

In this tutorial, I will explain how to set up SMTP server on Localhost and how to configure php.ini file.It is very easy to send Email from localhost.I was trying to send a mail via php mail function.The following error comes

Warning: 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() in C:\wamp\www\php\send_email.php on line 11.

Solution:

You need to configure php.ini file to send email via php send mail function.

1. Open php.ini file.

Default php.ini file settings

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25


; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = you@yourdomain


; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =

these entries are required to send the mail to any mail client from mail server.

2.The following two lines don't exist:

auth_username
auth_password

you need to add them to send mail from a server that requires authentication.

3. change SMTP settings

smtp_server = mail.example.com ///your SMTP server name.
smtp_port = 26 // SMTP port number
auth_username = username@example.com
auth_password = password
sendmail_from = you@example.com // from email address.

4.SAVE php.ini file.

5. Restart Server.

6. DONE.

It's working for me on WAMPSERVER.

Example:

Send_email.php
<php
$to = "rajesh@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "test@example.com";
$headers = "From:" . $from;

if (mail($to,$subject,$message,$headers)) {
   echo("
Message successfully sent!
");
  } else {
   echo("
Message delivery failed...
");
  }


?> 

I hope, this will help you to Send Email from Localhost.