繁体   English   中英

您的服务器可能未配置为使用此方法发送邮件

[英]Your server might not be configured to send mail using this method

我想在用户注册时验证email。 我正在使用 codeigniter3。

当我在本地主机上运行时,我没有收到任何邮件。 我收到此消息“无法使用 PHP mail() 发送 email。您的服务器可能未配置为使用此方法发送邮件。”

这是我的代码: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("\r\n");
            $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'   => "\r\n"
);

如果您使用的是 gmail 帐户,请确保您已在此处打开“不太安全的应用程序”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM