Ishaque Javed Ishaque Javed - 1 month ago 10
PHP Question

Getting trouble with SMTP connect with PhpMailer

I am trying to send SMTP secured mail with attachment using PHPMailer .

I made this function with PHPMailer library

public function sendCsv($subject,$body,$path,$mail_to,$from_name,$from_mail,$replyto){
require getcwd() .'/lib/PHPMailerAutoload.php';
$mail = new PHPMailer;

//$mail->SMTPDebug = 3; // Enable verbose debug output

$mail->isSMTP(); // Set mailer to use SMTP

$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'tester@mydomain.com';
$mail->Password = '**************';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;

$mail->setFrom($from_mail, $from_name);
$mail->addAddress($mail_to);
$mail->addReplyTo($replyto, 'no reply');

$mail->addAttachment($path);
$mail->isHTML(true);

$mail->Subject = $subject;

$mail->Body = $body;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
echo $error;
} else {
$sucess = 'Mail sent!';
echo $sucess;
}
}


Now if I comment the line $mail->isSMTP(); it's working fine . but I think it's not SMTP secured . Else I am getting this meesage : "Mail error: SMTP connect() failed. "

I searched for this problem but didn't get the proper answer what I am looking for . Please help me.

I am working in a dev server with HTACCESS protection

Answer

Well I have found the details to fix the issue here https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting .

I have just Turned on less secure apps https://www.google.com/settings/security/lesssecureapps from my gmail account.

This takes few minutes or an hour to active.

My current code :

public function sendCsv($subject,$body,$path,$mail_to,$from_name,$from_mail,$replyto){
            require getcwd() .'/lib/PHPMailerAutoload.php'; 
            $mail = new PHPMailer();

            $mail->isSMTP();   
            $mail->Host = 'smtp.gmail.com'; 
            //$mail->SMTPDebug = 2;                               
            $mail->SMTPAuth = true;   
            $mail->SMTPSecure = 'tls';                            
            $mail->Port = 587;  

            $mail->Username = 'mymail@gmail.com';                
            $mail->Password = 'mypass';                                     


            $mail->setFrom($from_mail, $from_name);
            $mail->addAddress($mail_to);  
            $mail->addReplyTo($replyto, 'no reply');

            $mail->addAttachment($path);        
            $mail->isHTML(true);                                  

            $mail->Subject = $subject;

            $mail->Body    = $body;
            //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

            if(!$mail->Send()) {
                $error = 'Mail error: '.$mail->ErrorInfo; 
                echo $error;
            } else {
                $sucess = 'Mail sent!';
                echo  $sucess;
            }
        }

Else you have to Setup a app from console.developers.google.com

Follow this guide : https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2