During Web Development in Windows environment, we use XAMPP/WAMP or some other local web server environment to test the code. In many cases includes the sending of mail, so it’s lazy job to upload the file in the server and test. In this case we can set the SMTP (Simple Mail Transfer Protocol) in our local server located in php.ini file.
In case of XAMPP, edit the file located in \xampp\php\php.ini
When the SMTP is not set, error like this is displayed:
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:\xampp\htdocs\testmail.php on line 10
Search for – [mail function] and change the SMTP value.
[mail function] ; For Win32 only. ; http://php.net/smtp SMTP = localhost ; http://php.net/smtp-port smtp_port = 25
SMTP = smtp.domain.com
Where domain – Domain name of your Internet Service Provider e.g. smtp.ntc.net.np, smtp.mos.com.np, smtp.itechroom.com
In some case it may be – mail.ntc.net.np, mail.itechroom.com
While sending mail Headers need to be defined to specify sender, content type etc.
$headers = "From: firstname.lastname@example.org"."\r\n" . 'X-Mailer: PHP/' . phpversion() . "\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: text/html; charset=utf-8\r\n" . "Content-Transfer-Encoding: 8bit\r\n\r\n";
Now Restart the Apache and try sending the mail.
mail('email@example.com','Testing mail','Mail received from localhost',$headers);