How to Send Mail From Localhost-PHP


If  you are using wamp in a windows machine,its quite hard to send mail through native php mail function or sendmail or SMTP.But you can use PHP Mailer program to send mail from localhost.It bypasses native mail,sendmail methods.It works well on windows as well as *nix machines.

Download PHP Mailer from http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php5_6/ or from http://phpmailer.worxware.com/

Sample code

<?php
require($_SERVER['DOCUMENT_ROOT'].'/phpmailer/class.phpmailer.php');
require($_SERVER['DOCUMENT_ROOT'].'./phpmailer/class.smtp.php');
$mail  = new PHPMailer();
$body="<b>This mail is sent using PHP Mailer</b>";#HTML tags can be included
$mail->IsSMTP();
$mail->SMTPAuth  = true;                 #enable SMTP authentication
$mail->SMTPSecure = "ssl";               #sets the prefix to the server
$mail->Host  = "smtp.gmail.com";         #sets GMAIL as the SMTP server
$mail->Port       = 465;                 #set the SMTP port
$mail->Username   = "";                  #your gmail username
$mail->Password   = "";                  #Your gmail password
$mail->From       = "";                  #your gmail id
$mail->FromName   = "";                  #your name
$mail->Subject    = "Subject of the mail";
$mail->WordWrap   = 50;
$mail->AddAddress("recipient mail id","recipient name");
$mail->IsHTML(true); // send as HTML
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}

?>

You can also send mail with attachments.For more info refer the documentation of PHP Mailer.

Caution:

Change SMTP Host,port,authentication only if sure of what you are doing.

3 thoughts on “How to Send Mail From Localhost-PHP

  1. Thanks man, i got the solution for my 3 years search… i need a contact with u to learn more… mail me… again thanks bro

Leave a reply to Srivats Cancel reply