PHP Mail Send Using PHP Mailer - OneNightStudy.Com

Monday, 12 February 2018

PHP Mail Send Using PHP Mailer

Mail Send Using PHP Mailer


1.Upload the phpmailer src files in the phpmailer folder on the server.

2. Use the code to send mail in php using gmail email.

require './phpmailer/Exception.php';
require './phpmailer/PHPMailer.php';
require './phpmailer/SMTP.php';


$mail = new PHPMailer\PHPMailer\PHPMailer();   
$mail->IsSMTP(); // telling the class to use SMTP

// SMTP Configuration
// $mail->SMTPAuth = true;                  // enable SMTP authentication
// $mail->IsSMTP(); // enable SMTP
//$mail->SMTPDebug = 2;// debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // or 587 
$mail->Username = "test@gmail.com";
$mail->Password = "password";
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);


$mail->From = "test@gmail.com";
// $mail->FromName = "My Name";
$mail->Subject = "Subject" ;
// $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($message);

// Add as many as you want
$mail->AddAddress('toemail@gmail.com', $name);

// If you want to attach a file, relative path to it
//$mail->AddAttachment("images/phpmailer.gif");             // attachment

$response= NULL;
if(!$mail->Send()) {
 //   $response = "Mailer Error: " . $mail->ErrorInfo;
    print_r($mail->ErrorInfo);
} else {
     echo 'sent';
}
//die;


/****************/

//fix the google smtp issue using

2. If getting issue in the gmail authentication issue use this after login to gmail.
https://www.google.com/settings/u/1/security/lesssecureapps
https://accounts.google.com/b/0/DisplayUnlockCaptcha
https://security.google.com/settings/security/activity?hl=en&pli=1

No comments:

Post a Comment