我想在注册用户时发送电子邮件。我正在使用xampp,codeigniter和phpMailer。当我提交注册表格时,这是一个名为
的错误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 = "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');
}
}
}
这是库中的php文件
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class My_PHPMailer {
public function My_PHPMailer() {
require_once('PHPMailer/PHPMailerAutoload.php');
}
}
答案 0 :(得分: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