2015-08-27 3 views
0

Я хочу отправить электронное письмо при регистрации пользователя. Я использую xampp, codeigniter и phpMailer. когда я представить регистрационную форму, это была ошибка называетсяPhp mailer в codeigniter

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

вот мой код (контроллер)

<?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 = "[email protected]"; 
      $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 = "[email protected]"; 
      $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'); 
} 
} 
} 

это файл PHP в библиотеке

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

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

вы хотите отправить почту с локального или живого сервера –

+0

я хочу отправить mail from localhost – ashik

+0

Прочтите руководство по устранению неполадок, на которое указывает сообщение об ошибке. Он точно описывает, как это исправить, как и большинство сотен дубликатов этого вопроса. Вы также основывали свой код на устаревшем примере. – Synchro

ответ

1

Этот код дает ошибка об ошибке аутентификации на сервере gmail. Это связано с тем, что gmail предполагает, что компьютер/местоположение для входа в систему изменен, и поэтому он благоприятный. Если вы входите в свой gmail и смотрите в безопасности, недавно зарегистрированные устройства, вы можете увидеть всплывающее окно, показывающее, что логин заблокирован с вашего адреса сервера. Check your access status here

Если вы включите его, это сработает. Go to this link for unlocking

см. Ниже нить. Вы также можете ссылаться на это. PHPMailer - SMTP ERROR: Password command failed when send mail from my server

+0

Для отладки проблемы с phpmailer используйте $ mail-> SMTPDebug = 2; , это даст подробные ошибки/предупреждения. Это поможет легко найти проблему. –

+0

хорошая ссылка ... – Linus

0

сделать файл email.php в приложение/Config/папке:

И это содержание в нем:

$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";