Pages

Sunday 9 August 2015

Mail from Localhost in php

Mail from localhost in php 


Download Phpmailer using composer or github

1) github link :-https://github.com/PHPMailer/PHPMailer

or

1) composer code

{
    "require": {
        "phpmailer/phpmailer": "^5.2"
    }
}
save this code as composer.json and run composer by right click on that folder and click on composer install 


 2)

require 'vendor/autoload.php';

$mail = new PHPMailer();

$mail->SMTPDebug = 3; 

$mail->IsSMTP();

$mail->Mailer = 'smtp';

$mail->SMTPAuth = true;

$mail->Host = 'smtp.gmail.com'; 

$mail->Port = 465;

$mail->SMTPSecure = 'ssl';


$mail->Username = "your email address";

$mail->Password = "your password";


$mail->IsHTML(true); // if you are going to send HTML formatted emails

$mail->SingleTo = true; // if you want to send a same email to multiple users. multiple emails will be sent one-by-one.


$mail->From = "your email Address";

$mail->FromName = "Me";


$mail->addAddress("To(email Address)","Name");

//$mail->addCC("user.3@ymail.com","User 3");

//$mail->addBCC("user.4@in.com","User 4");

$mail->Subject = "You have been registered";

$mail->Body = "Hi,<br /><br />Welcome <br />";


if(!$mail->Send()){

echo "<pre>";

   echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;

   echo "</pre>";

}

else

   echo "sent";

save this code in php and run this code ur mail will be sended 

If Any problem will occur then change this line 

$mail->SMTPSecure = 'ssl';

to 

$mail->SMTPSecure = 'tls';