简体   繁体   中英

The following SMTP error was encountered: 0 Unable to send email using PHP SMTP

    this is controller code
    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' => 'smtp',
                    'smtp_host' => 'ssl://smtp.googlemail.com',
                    'smtp_port' => 465,
                    'smtp_user' => 'testsourcecodester@gmail.com', // change it to yours
                    'smtp_pass' => 'mysourcepass', // change it to yours
                    'mailtype' => 'html',
                    'charset' => 'iso-8859-1',
                    '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');
            }

        }
This is view 
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>CodeIgniter Signup with Email Verification</title>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <h1 class="page-header text-center">CodeIgniter Signup with Email Verification</h1>
    <div class="row">
        <div class="col-sm-4">
            <?php
                if(validation_errors()){
                    ?>
                    <div class="alert alert-info text-center">
                        <?php echo validation_errors(); ?>
                    </div>
                    <?php
                }

                if($this->session->flashdata('message')){
                    ?>
                    <div class="alert alert-info text-center">
                        <?php echo $this->session->flashdata('message'); ?>
                    </div>
                    <?php
                }   
            ?>
            <h3 class="text-center">Signup Form</h3>
            <form method="POST" action="<?php echo base_url().'user/register'; ?>">
                <div class="form-group">
                    <label for="email">Email:</label>
                    <input type="text" class="form-control" id="email" name="email" value="<?php echo set_value('email'); ?>">
                </div>
                <div class="form-group">
                    <label for="password">Password:</label>
                    <input type="password" class="form-control" id="password" name="password" value="<?php echo set_value('password'); ?>">
                </div>
                <div class="form-group">
                    <label for="password_confirm">Password:</label>
                    <input type="password" class="form-control" id="password_confirm" name="password_confirm" value="<?php echo set_value('password_confirm'); ?>">
                </div>
                <button type="submit" class="btn btn-primary">Register</button>
            </form>
        </div>
        <div class="col-sm-8">
            <h3 class="text-center">Users Table</h3>
            <table class="table table-bordered table-striped">
                <thead>
                    <tr>
                        <th>UserID</th>
                        <th>Email</th>
                        <th>Password</th>
                        <th>Code</th>
                        <th>Active</th>
                    </tr>
                </thead>
                <tbody>
                <?php
                    foreach($users as $row){
                        ?>
                        <tr>
                            <td><?php echo $row->id; ?></td>
                            <td><?php echo $row->email; ?></td>
                            <td><?php echo $row->password; ?></td>
                            <td><?php echo $row->code; ?></td>
                            <td><?php echo $row->active ? 'True' : 'False'; ?></td>
                        </tr>
                        <?php
                    }
                ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>

I'm getting error when the email is sent in CodeIgniter.

The following SMTP error was encountered: 0 Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

I'm doing it in localhost. what is the issue in the code whether the email can be sent in CodeIgniter using localhost how to solve the issue in the code? if need more will help you. What type of setting I have to do so an email can be sent?

This is a working code to send mails from localhost

function sendMail()
    {
        $config = Array(
      'protocol' => 'smtp',
      'smtp_host' => 'ssl://smtp.googlemail.com',
      'smtp_port' => 465,
      'smtp_user' => 'xxxx@gmail.com', // change it to yours
      'smtp_pass' => 'xxx', // change it to yours
      'mailtype' => 'html',
      'charset' => 'iso-8859-1',
      'wordwrap' => TRUE
    );

            $message = 'hi test Mailer';
            $this->load->library('email', $config);
          $this->email->set_newline("\r\n");
          $this->email->from('xxx@gmail.com'); // change it to yours
          $this->email->to('xxx@gmail.com');// change it to yours
          $this->email->subject('Test mail from localhost');
          $this->email->message($message);
          if($this->email->send())
         {
          echo 'Email sent.';
         }
         else
        {
         show_error($this->email->print_debugger());
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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