Codeigniter Email PHP SMPT 错误,无法通过 Gmail 发送邮件

Codeigniter Email PHP SMPT Error, cannot send mail through Gmail

我目前正在尝试在完成注册后发送电子邮件或验证电子邮件。问题是由于这个错误,我无法通过 gmail 发送验证:

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

我已经尝试在 Whosebug 中搜索解决方案,但我还没有找到任何适合我的解决方案 problem.i 也完成了将 smtp_port 更改为 465但是,它仍然没有解决我的问题。

我正在使用 Localhost,我在 xampp 文件夹上配置了 php.inisendmail.ini

php.ini

[mail function]
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = qwerty@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

发送邮件

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=qwerty@gmail.com
auth_password=qwertyqwerty
force_sender=qwerty@gmail.com

我的代码

//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' => 'smtp.gmail.com',
    'smtp_port' => 587,
    'smtp_user' => 'qwerty@gmail.com', // change it to yours
    'smtp_pass' => 'qwertyqwerty', // change it to yours
    'smtp_timeout' => "",
    'mailtype' => 'html',
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE,
    'validate' => FALSE
);

$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());

}

您无需更改 php.inisendmail.ini 即可发送 SMTP。请设为默认值。

在我查看了您的 SMTP 配置后,您遗漏了一件小事但似乎很重要:加密协议。

您可以在 $config['smtp_host'] 中设置加密协议,如下所示:

$config['smtp_host'] = 'ssl://smtp.googlemail.com'

或者你也可以试试这个配置:

$config['protocol'] = "smtp";
$config['smtp_host'] = "smtp.googlemail.com";
$config['smtp_port'] = "465";   // if you decided to use 465, then set smtp crypto to ssl, else for example 587, you need to set smtp crypto to tls.
$config['smtp_crypto'] = 'ssl';  // this is based on your smtp port -> ssl/tls
$config['smtp_timeout'] = "400";
$config['smtp_user'] = "youruser@gmail.com";
$config['smtp_pass'] = "yourpassword%&^$&$";
$config['validate'] = true;
$config['charset'] = 'utf-8';
$config['mailtype'] = "html";
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";

然后将 $config 分配给电子邮件库:$this->load->library('email', $config);

请检查较低的安全性(在 gmail 设置中)是否已启用。

否则,您可以在 application/config/email.php

中创建 email.php 作为电子邮件配置库

把它放在那里:

<?php
defined('BASEPATH') or exit('No direct script access allowed');

$config['protocol'] = "smtp";
$config['smtp_host'] = "smtp.googlemail.com";
$config['smtp_port'] = "465";   // if you decided to use 465, then set smtp crypto to ssl, else for example 587, you need to set smtp crypto to tls.
$config['smtp_crypto'] = 'ssl';  // this is based on your smtp port -> ssl/tls
$config['smtp_timeout'] = "400";
$config['smtp_user'] = "youruser@gmail.com";
$config['smtp_pass'] = "yourpassword%&^$&$";
$config['validate'] = true;
$config['charset'] = 'utf-8';
$config['mailtype'] = "html";
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";

?>

然后在你的控制器中,你不需要再设置你的 $config :D,但你所要做的只是在控制器中调用 $this->load->library('email');