【问题标题】:CodeIgniter email throws errorCodeIgniter 电子邮件引发错误
【发布时间】:2018-05-24 16:24:45
【问题描述】:

我无法使用 CodeIgniter 发送邮件。它显示以下错误。

fwrite(): send of 28 bytes failed with errno=10054 An existing connection was forcibly closed by the remote host.

这是我在 email.php 库中的设置

public $useragent   = 'CodeIgniter';
public $mailpath    = '/usr/sbin/sendmail'; // Sendmail path
public $protocol    = 'smtp';       // mail/sendmail/smtp
public $smtp_host   = 'smtp.mailhostbox.com';
public $smtp_user   = 'xxx@xx.in';
public $smtp_pass   = 'xxxxxx';
public $smtp_port   = 25;
public $smtp_timeout    = 5;
public $smtp_keepalive  = FALSE;
public $smtp_crypto = '';
public $newline     = "\r\n";

我可以发送电子邮件到昨天。但是从今天早上开始,它会显示此错误。
更新: 我已将主机更改为 gmail ,但仍然无法正常工作。以下是我所做的更改

public $useragent   = 'CodeIgniter';
public $mailpath    = '/usr/sbin/sendmail'; // Sendmail path
public $protocol    = 'smtp';       // mail/sendmail/smtp
public $smtp_host   = 'smtp.gmail.com';
public $smtp_user   = 'noreply.xxx@gmail.com';
public $smtp_pass   = 'xxxxxx';
public $smtp_port   = 465;
public $smtp_timeout    = 5;
public $smtp_keepalive  = FALSE;
public $smtp_crypto = 'ssl';
public $newline     = "\r\n";

但它显示错误为
Message: fsockopen(): unable to connect to ssl://smtp.gmail.com:465:465 (Failed to parse address "smtp.gmail.com:465:465")

我将端口从465改为587如下

public $smtp_port   = 587;
public $smtp_crypto = 'tls';

但我得到了这个错误

Message: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed

我不明白真正的问题是什么,因为它在所有可能的情况下都会引发错误

【问题讨论】:

  • 将$smtp_port 值更改为465
  • 我将端口更改为465,但我得到了一些其他错误fsockopen(): unable to connect to smtp.mailhostbox.com:465 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. )
  • 我已经检查过,那里列出的建议对我不起作用
  • 你初始化配置了吗?

标签: php codeigniter email


【解决方案1】:

你需要初始化配置。see here

举例

function send_email($attributes) {

        $this->load->library('email');

        $this->email->set_newline("\r\n");

        $config['protocol'] = 'smtp';
        $config['smtp_host'] = 'host';
        $config['smtp_port'] = '465';
        $config['smtp_user'] = 'user@smtp.com';
        $config['smtp_from_name'] = 'FROM NAME';
        $config['smtp_pass'] = 'XXX';
        $config['wordwrap'] = TRUE;
        $config['newline'] = "\r\n";
        $config['mailtype'] = 'html';                       

        $this->email->initialize($config);

        $this->email->from($config['smtp_user'], $config['smtp_from_name']);
        $this->email->to($attributes['to']);
        $this->email->cc($attributes['cc']);
        $this->email->bcc($attributes['cc']);
        $this->email->subject($attributes['subject']);

        $this->email->message($attributes['message']);

        if($this->email->send()) {
            return true;        
        } else {
            return false;
        }       

}

【讨论】:

  • $this->email->set_newline("\r\n"); 对这一行是多余的:$config['newline'] = "\r\n"; 我更喜欢第二行,因为您以这种方式设置所有其他参数。
  • 我已经在email library中设置了所有这些参数。并使用auotoload.php 加载此文件
猜你喜欢
  • 2015-03-17
  • 2016-05-06
  • 2016-09-02
  • 2011-06-12
  • 2014-06-07
  • 2011-09-18
  • 1970-01-01
  • 2013-01-07
  • 2019-08-07
相关资源
最近更新 更多