CodeIgniter 电子邮件总是返回 false



我正在尝试发送电子邮件。我在application/config/email.php中的配置如下:

<?php
defined('BASEPATH') OR exit("No direct script access allowed");
$config = array(
    'protocol' => 'smtp',
    'smtp_host' => 'my_host',    // My host name
    'smtp_port' => 2525,
    'smtp_user' => 'username',   // My username
    'smtp_pass' => 'password',   // My password
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE,
    'smtp_timeout' => 30,
    'newline' => "rn",
    'crlf' => "rn",
    'mailtype' => "text"
);

假设我使用的是一个名为 Maintenance.php 的控制器,其中的设置如下:

<?php // Maintenance.php
defined('BASEPATH') OR exit("No direct script access allowed");
class Maintenance extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
    public function index()
    {
        date_default_timezone_set('Africa/Accra');    // This was to cater for an error given to me earlier
        $this->load->library('email');
        $this->email->from('the email I used in the email.php', "Name");
        $this->email->to('email to send to');
        $this->email->subject('Test email');
        $this->email->message("Testing the email class");
        var_dump($this->email->send());
        $this->email->print_debugger();
     }
}

var_dump()之后,我得到了bool(false).即使我的环境设置为开发,我仍然没有收到任何错误消息。$this->email->print_debugger()也没有显示任何东西。我正在使用代码点火器 3.1.3

感谢您的帮助

首先,像下面这样保存您的email's configuration application/config/email.php

defined('BASEPATH') OR exit("No direct script access allowed");
$config['mail'] = array(
    'protocol' => 'smtp',
    'smtp_host' => 'my_host',    // My host name
    'smtp_port' => 2525,
    'smtp_user' => 'username',   // My username
    'smtp_pass' => 'password',   // My password
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE,
    'smtp_timeout' => 30,
    'newline' => "rn",
    'crlf' => "rn",
    'mailtype' => "text"
);

然后在控制器load email configuration文件中,并使用如下所示$this->email->initialize($configuration)将其传递给email library

public function index()
    {
        date_default_timezone_set('Africa/Accra');    // This was to cater for an error given to me earlier
        $this->config->load('email', TRUE);//load email config file
        $confiuration = $this->config->item('mail', 'email');//email configuration
        $this->load->library('email');
        $this->email->initialize($configuration);//initializes email configuration
        $this->email->from('the email I used in the email.php', "Name");
        $this->email->to('email to send to');
        $this->email->subject('Test email');
        $this->email->message("Testing the email class");
        var_dump($this->email->send());
        $this->email->print_debugger();
     }
确保将

'smtp_timeout' =>配置设置为高于默认值的值,即 5

20 到 30 左右应该可以工作。

最新更新