Sunday, 10 April 2016

Php mailer in codeigniter

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)




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
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



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

class My_PHPMailer {
public function My_PHPMailer() {
require_once('PHPMailer/PHPMailerAutoload.php');

}
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...