【问题标题】:Codeigniter Email PHP SMPT Error, cannot send mail through GmailCodeigniter 电子邮件 PHP SMTP 错误,无法通过 Gmail 发送邮件
【发布时间】:2020-08-30 14:47:50
【问题描述】:

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

遇到以下 SMTP 错误:无法使用以下方式发送电子邮件 PHP SMTP。您的服务器可能未配置为使用此功能发送邮件 方法。

我已经尝试在 stackoverflow 中搜索解决方案,但我没有找到任何适合我的问题的解决方案。我还完成了将 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 codeigniter email


    【解决方案1】:

    您无需更改 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-&gt;load-&gt;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-&gt;load-&gt;library('email');

    【讨论】:

    • 如果我要部署或在线实现它,我还能使用这些代码吗?会有变化吗?谢谢你,好先生。
    • 这取决于您的托管服务提供商。有时,共享主机(服务器)无法通过 gmail 应用 SMTP。您必须仔细检查您的托管服务提供商是否提供。不,您仍将使用我的朋友的代码,无需任何更改。你知道 SMTP 中继吗?我通常使用 MAILGUN 作为我的中继(虽然这不是一项免费服务。因为他们有日志、监控器以及针对您的消息的反垃圾邮件)。
    • 今天我会注意的,谢谢你好先生。我昨天搜索了几个小时的解决方案,今天终于解决了。谢谢!
    • 随时,我的朋友。如果它仍然不起作用,请告诉我。
    • 您好,先生,我想知道为什么电子邮件会进入“SPAM”文件夹。有什么解决办法吗?谢谢
    猜你喜欢
    • 1970-01-01
    • 2020-02-21
    • 2022-01-21
    • 2011-08-11
    • 2014-01-04
    • 2020-05-30
    • 2012-05-29
    • 2017-05-12
    • 1970-01-01
    相关资源
    最近更新 更多