我想在用户注册时验证电子邮件。我正在使用codeigner3。
当我在本地主机上运行时,我不会收到任何邮件。我收到这个消息";无法使用PHP邮件((发送电子邮件。您的服务器可能未配置为使用此方法发送邮件">
这是我的代码:controllers/User.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('users_model');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->library('session');
//get all users
$this->data['users'] = $this->users_model->getAllUsers();
}
public function index(){
$this->load->view('register', $this->data);
}
public function register(){
$this->form_validation->set_rules('email', 'Email', 'valid_email|required');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[7]|max_length[30]');
$this->form_validation->set_rules('password_confirm', 'Confirm Password', 'required|matches[password]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('register', $this->data);
}
else{
//get user inputs
$email = $this->input->post('email');
$password = $this->input->post('password');
//generate simple random code
$set = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$code = substr(str_shuffle($set), 0, 12);
//insert user to users table and get id
$user['email'] = $email;
$user['password'] = $password;
$user['code'] = $code;
$user['active'] = false;
$id = $this->users_model->insert($user);
//set up email
$config = array(
'protocol' => 'mail',
'smtp_host' => 'smtp.gmail.com',
'smtp_port' => 587,
'smtp_user' => '********@gmail.com', // change it to yours
'smtp_pass' => '*********', // change it to yours
'mailtype' => 'html',
'wordwrap' => TRUE
);
$message = "
<html>
<head>
<title>Verification Code</title>
</head>
<body>
<h2>Thank you for Registering.</h2>
<p>Your Account:</p>
<p>Email: ".$email."</p>
<p>Password: ".$password."</p>
<p>Please click the link below to activate your account.</p>
<h4><a href='".base_url()."user/activate/".$id."/".$code."'>Activate My Account</a></h4>
</body>
</html>
";
$this->load->library('email', $config);
$this->email->set_newline("rn");
$this->email->from($config['smtp_user']);
$this->email->to($email);
$this->email->subject('Signup Verification Email');
$this->email->message($message);
//sending email
if($this->email->send()){
$this->session->set_flashdata('message','Activation code sent to email');
}
else{
$this->session->set_flashdata('message', $this->email->print_debugger());
}
redirect('register');
}
}
public function activate(){
$id = $this->uri->segment(3);
$code = $this->uri->segment(4);
//fetch user details
$user = $this->users_model->getUser($id);
//if code matches
if($user['code'] == $code){
//update user active status
$data['active'] = true;
$query = $this->users_model->activate($data, $id);
if($query){
$this->session->set_flashdata('message', 'User activated successfully');
}
else{
$this->session->set_flashdata('message', 'Something went wrong in activating account');
}
}
else{
$this->session->set_flashdata('message', 'Cannot activate account. Code didnt match');
}
redirect('register');
}
}
在您的配置更改协议中;smtp";
$config = array(
'smtp_host'=> "smtp.gmail.com",
"smtp_user"=> 'Username', // YOUR Username
"smtp_pass" => 'Password', // YOUR Password
'protocol' => 'smtp',
'smtp_crypto' => 'ssl',
"smtp_port" => 465,
'charset' => 'iso-8859-1',
'wordwrap' => TRUE,
'mailtype' => 'html',
'newline' => "rn"
);
如果您正在使用gmail帐户,确保您已打开";不太安全的应用程序"此处