codeigniter 遇到以下SMTP错误:0无法使用PHP SMTP发送电子邮件

zpjtge22  于 2023-11-14  发布在  PHP
关注(0)|答案(2)|浏览(149)
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' => '[email protected]', // 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>

字符串
我收到错误时,电子邮件是在CodeIgniter发送。
遇到以下SMTP错误:0无法使用PHP SMTP发送电子邮件。您的服务器可能未配置为使用此方法发送邮件。
我在localhost中这样做。代码中的问题是什么电子邮件是否可以在CodeIgniter中使用localhost发送如何解决代码中的问题?如果需要更多将帮助你。我必须做什么类型的设置才能发送电子邮件?

yhuiod9q

yhuiod9q1#

这是一个从本地主机发送邮件的工作代码

function sendMail()
    {
        $config = Array(
      'protocol' => 'smtp',
      'smtp_host' => 'ssl://smtp.googlemail.com',
      'smtp_port' => 465,
      'smtp_user' => '[email protected]', // 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('[email protected]'); // change it to yours
          $this->email->to('[email protected]');// 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());
        }

    }

字符串

vfwfrxfs

vfwfrxfs2#

如果你使用的是goDaddy主机,那么google SMTP将无法工作。goDaddy阻止了所有其他SMTP来推广自己的SMTP。

相关问题