0

i want to send a email when register a user. i'm using the xampp, codeigniter and phpMailer. when i submit the registration form it was an error called

Message was not sent 
PHPMailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Registration Successfully !

here is my code(controller)

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to start session in order to access it through CI

//The controller class is extends CI_Contoller
Class User_Authentication extends CI_Controller {

public function __construct() {
parent::__construct();

// Load form helper library
$this->load->helper('form');

// Load form validation library
$this->load->library('form_validation');
$this->load->library('my_phpmailer');

// Load session library
$this->load->library('session');

// Load database
$this->load->model('login_database');
}

// Show login page
public function index() {
$this->load->view('user_site/login_form');
}

// Show registration page
public function user_registration_show() {
$this->load->view('user_site/registration_form');
}

// Validate and store registration data in database
public function new_user_registration() {

// Check validation for user input in SignUp form
$this->form_validation->set_rules('firstname', 'First Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('lastname', 'Last Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|md5');
if ($this->form_validation->run() == FALSE) {
$this->load->view('user_site/registration_form');
//    $this->load->view('register');
} else {
$data = array(
'firstname' => $this->input->post('firstname'),
'lastname' => $this->input->post('lastname'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'address' => $this->input->post('address'),
'contact_no' => $this->input->post('contact_no')
);
$result = $this->login_database->registration_insert($data);
if ($result == TRUE) {
$data['message_display'] = 'Registration Successfully !';

$mail = new PHPMailer();
            $mail->IsSMTP();
            $mail->Mailer = 'smtp';
            $mail->SMTPAuth = true;
            $mail->Host = 'smtp.gmail.com'; // "ssl://smtp.gmail.com" didn't worked
            $mail->Port = 465;
            $mail->SMTPSecure = 'ssl';
            // or try these settings (worked on XAMPP and WAMP):
            // $mail->Port = 587;
            // $mail->SMTPSecure = 'tls';


            $mail->Username = "ashik@gmail.com";
            $mail->Password = "zswedfr";

            $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 = "ashik@gmail.com";
            $mail->FromName = "ABC";

                $address = $_POST['email'];
                $mail->AddAddress($address, "Guest");


            $mail->Subject = "ABC Email validation ";

            $mail->Body ="ABC Email validation";

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

                echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
            }else{
                echo "sucfdt  " ;
            }
$this->load->view('user_site/login_form', $data);
} else {
$data['message_display'] = 'Email already exist!';
//$this->load->view('user_site/registration_form', $data);
$this->load->view('register');
}
}
}

this is the php file in library

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class My_PHPMailer {
    public function My_PHPMailer() {
        require_once('PHPMailer/PHPMailerAutoload.php');
    }
}
ashik
  • 121
  • 6
  • 20
  • you want to send mail from localhost or live server – Sorav Garg Aug 27 '15 at 04:44
  • i want to send mail from localhost – ashik Aug 27 '15 at 05:04
  • Read the troubleshooting guide that the error message points at. It tells you exactly how do fix this, as do most of the hundreds of duplicates of this question. You've also based your code on an obsolete example. – Synchro Aug 27 '15 at 06:05

2 Answers2

1

This code give the error that authentication failure in gmail server. It is because, gmail is assuming that the login computer/location is changed, and so it is auspicious. If you login to your gmail and look in security, recently logged devices, you can see a popup showing that login blocked from your server address. Check your access status here

If you enable it, this will work. Go to this link for unlocking

see this below thread. You can refer this too. PHPMailer - SMTP ERROR: Password command failed when send mail from my server

Community
  • 1
  • 1
Subin Thomas
  • 1,408
  • 10
  • 19
  • For debugging phpmailer issue, use $mail->SMTPDebug = 2; , this will give detailed error/warnings. It will help to find the problem easily. – Subin Thomas Aug 27 '15 at 04:58
0

Make a file email.php in application/config/ folder :

And these contents into it :

$config['protocol'] = 'smtp';

$config['smtp_host'] = 'ssl://smtp.gmail.com'; //change this

$config['smtp_port'] = '465';

$config['smtp_user'] = ''; //change this

$config['smtp_pass'] = ''; //change this

$config['mailtype'] = 'html';

$config['charset'] = 'iso-8859-1';

$config['wordwrap'] = TRUE;

$config['newline'] = "\r\n";