-1
<?php

class EmailModel extends CI_Model{
    public function email(){

        require 'Email Files/PHPMailerAutoload.php';


        $mail = new PHPMailer;


        $mail->isSMTP();                               
        $mail->Host = 'smtp.gmail.com'; 
        $mail->SMTPAuth = true;                               
        $mail->Username = 'Google account username';              
        $mail->Password = 'Google account Password';  
        $mail->SMTPSecure = 'ssl';
        $mail->Port = 465;  
        $mail->addAddress('abc@gmail.com');
        $mail->setFrom('abc@gmail.com');
        $mail->Subject = 'Test';
        $mail->Body    = 'Testing';

        if($mail->send()) {
            echo 'Message has been sent';

        } else {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
    }

}
}

?>

I am using Github library for php email. when I var_dump($mail) ; its showing all the data in the array. now, this code is showing error: "SMTP connect() failed"

GiamPy
  • 3,543
  • 3
  • 30
  • 51
Apoorva Agarwal
  • 183
  • 1
  • 1
  • 9
  • can you just check detail using show_error($mail->print_debugger()); – Preetham Hegde Feb 07 '17 at 07:37
  • Could you please revise your question and format the code such that it is readable? Thanks. If you ask for help then the minimum effort you put into our question is to write it such that people can understand it. – arkascha Feb 07 '17 at 07:41
  • Codeigniter has own email library https://www.codeigniter.com/user_guide/libraries/email.html –  Feb 07 '17 at 07:49
  • Possible duplicate of [PHPMailer to use Gmail as SMTP server.Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host](http://stackoverflow.com/questions/6168882/phpmailer-to-use-gmail-as-smtp-server-could-not-connect-to-smtp-host-mailer-err) – Spiny Norman Feb 07 '17 at 08:00

2 Answers2

1

I also had this problem.Do the following steps

  • Go to myaccount.google.com
  • Click "connected apps & sites",
  • Turn "Allow less secure apps" to "ON" (bottom of the page).
affaz
  • 1,191
  • 9
  • 23
0

Just I checked up with your code no error in that. you've to enable https://www.google.com/settings/security/lesssecureapps

<?php

include "PHPMailer_5.2.4/class.phpmailer.php";
   $mail = new PHPMailer;


        $mail->isSMTP();                               
        $mail->Host = 'smtp.gmail.com'; 
        $mail->SMTPAuth = true;                               
        $mail->Username = 'test@gmail.com';              
        $mail->Password = 'test@12123#';  
        $mail->SMTPSecure = 'ssl';
        $mail->Port = 465;  
        $mail->addAddress('abc@gmail.com');
        $mail->setFrom('abc@gmail.com');
        $mail->Subject = 'Test';
        $mail->Body    = 'Testing';

        if($mail->send()) {
            echo 'Message has been sent';

        } else {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
    }



?>

sample test mail

  • I enabled less secure apps and "include class.phpmailer.php" and still having an error : "SMTP not found" . – Apoorva Agarwal Feb 08 '17 at 05:59
  • and I am using this library : " https://github.com/PHPMailer/PHPMailer " – Apoorva Agarwal Feb 08 '17 at 06:03
  • actually, you're using the lastet version of PHPmailer include correct path to execute or else download https://github.com/shalinshah1993/ImPatho/tree/master/server%20code/Ic2014/PHPMailer_5.2.4 –  Feb 08 '17 at 11:15