Mail send using smtp in php

Configure smtp using my mail id and password but i am getting


“SMTP Error: Could not authenticate”


Junior Developer Asked on February 22, 2017 in PHP.
Add Comment
2 Answer(s)

Please try using the below code



require 'class.smtp.php';

require 'class.phpmailer.php';
function smtpmailSend($to, $from, $from_name, $subject, $body) {

global $error;

$mail = new PHPMailer();  // create a new object

$mail->IsSMTP(); // enable SMTP

$mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only

$mail->SMTPAuth = true;  // authentication enabled

$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail

$mail->SMTPAutoTLS = false;

$mail->Host = '';

$mail->Port = 465;
$mail->Username = '';

$mail->Password = 'xxxxxxxxx';   // Your Gmail Password

$mail->SetFrom($from, $from_name);

$mail->Subject = $subject;

$mail->Body = $body;


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

$error = 'Mail error: '.$mail->ErrorInfo;

return false;

} else {

$error = 'Message sent!';

return true;


if (smtpmailSend('', '', 'aviance', 'test mail', 'Hi! This is Bikash..')) {

//echo "Send!";


if (!empty($error)) echo $error;

You will get  ‘class.smtp.php’  & ‘class.phpmailer.php’  file from  here

Team Leader Answered on February 22, 2017.
Add Comment

If you are using gmail id you have to turned on the access for less secure apps.Otherwise you’ll not get the email.

Please follow the below steps.

RE: Mail send using smtp in php


RE: Mail send using smtp in php

Senior Developer Answered on March 6, 2017.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.