CodeIgniter电子邮件始终返回false

时间:2017-03-25 10:06:02

标签: php codeigniter codeigniter-3

我试图发送电子邮件。我在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' => "\r\n",
    'crlf' => "\r\n",
    '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()也没有显示任何内容。我正在使用 CodeIgniter 3.1.3

感谢您的帮助

2 个答案:

答案 0 :(得分:3)

首先在[{1}}

中保存您的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' => "\r\n", 'crlf' => "\r\n", 'mailtype' => "text" ); 文件中,使用load email configuration将其传递给email library,如下所示。

$this->email->initialize($configuration)

答案 1 :(得分:0)

确保您的'smtp_timeout' =>配置设置为高于默认值5的值。

20到30左右应该有效。