Codeigniter-电子邮件不;t读取配置设置


Codeigniter - email doesn't read config setting

我们有一个使用Codeigniter框架构建的网站。该网站在过去两周之前一直运行良好(约8个月)。

因此,我们有一个表单,一旦用户提交表单,我们就会将数据插入MySQL数据库,并向用户发送电子邮件。现在,数据库部分工作正常。但电子邮件功能在两周前停止工作。我确信我没有更改代码中的任何内容。

我为此创建了一个测试

public function test_email(){ 
    $this->load->config('email');
    $this->load->library('custom_email');
    $ddd = $this->load->config('email');  
    $data = array(
        //must have
        'recipient'         => 'test@test.com',
        'recipient_name'    => 'Test',
        'title'             => 'Ms',
        'subject'           => 'Email Test',    
        'admin_email'       => $this->config->item('auto_bcc_to'),
        "smtp" =>$this->config->item('smtp_host')
    );
    ch_printr($data);
    $this->custom_email->send('test_email',$data,TRUE);
}

和我的库/custom_email.php

class custom_email {
    function __construct() {
        $this->ci = & get_instance();
        $this->ci->load->helper('html');
        $this->ci->load->library('email');
    }
    function send($type,$data,$debug_mode = FALSE){
        $data['site_name']  = $this->ci->config->item('site_name');
        $data['message']    = $this->ci->parser->parse('email/email--'.$type,$data,TRUE); 
       return $this->_send($data,$debug_mode);
    }
    private function _send($data,$debug_mode = FALSE){
        $this->ci->config->load("email");
        $email = $data['recipient'];
        $email_message = $data['message'];
        $subject = $data['subject'];
       $headers  = 'MIME-Version: 1.0' . "'r'n";
       $headers .= 'Content-type: text/html; charset=utf-8' . "'r'n";
    // Additional headers
       $headers .= 'From: '.$this->ci->config->item('system_sender_name').'<'.$this->ci->config->item('system_sender_mail').'>' . "'r'n";
        $headers .= 'To: '.$data['recipient_name'].'<'.$data['recipient'].'>' . "'r'n"; 
        $headers .= 'X-Mailer: PHP/' . phpversion();
       if(mail($email, '=?UTF-8?B?'.base64_encode($subject).'?=', $email_message, $headers)){    //error is pointed here
           return true; 
       }else{
          return false;
       }
    }
}

和config/email.php

$config = Array( 
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => 'webmaster@test.com', 
'smtp_pass' => 'myPassword',
'mailtype'  => 'html', 
'charset'   => 'utf-8',
'crlf'      => "'r'n",       
'newline'   => "'r'n",       
'system_sender_mail'    => 'webmaster@test.com',
'system_sender_name'    => 'Webmaster',
'auto_bcc_to'           => 'admin@test.com'
);

现在,电子邮件停止工作,我们得到这个错误

Message: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()
Filename: libraries/custom_email.php
Line Number: 110

我尝试打印电子邮件配置项(smtp设置),它检索到了正确的数据。我应该如何继续?

提前谢谢。

这个函数对我有效。

function email_send(){
    $config = Array(
            'protocol'=>'smtp',
            'smtp_host' => 'smtp.gmail.com',
            'smtp_port' => '25',
            'SMTPAuth' => true,
            'smtp_user' => 'email@gmail.com',
            'smtp_pass' => 'password'
        );
    $this->load->library('email', $config);
    $this->email->from('test@gmail.com');
    $this->email->to('test1@gmail.com');
    $this->email->subject('Test mail');
    $this->email->message('This is a test mail from codeigniter');
   if( ! $this->email->send()){
        echo "Your mail was sent successfully!!!";
    }
    else{
        show_error($this->email->print_debugger());
    }
}

您的电子邮件设置和端口应该像一样更改

$config = Array( 
'protocol' => 'smtp',
'smtp_host' => 'smtp.gmail.com',
'smtp_port' => 25,
'SMTPAuth' => true,
'smtp_user' => 'webmaster@test.com', 
'smtp_pass' => 'myPassword',
'mailtype'  => 'html', 
'charset'   => 'utf-8', 
);

这很有帮助,我找到了一种更好的方法来测试代码点火器中的电子邮件发送。

我正在使用mailtrap-api发送电子邮件并进行测试,它就像使用smtp设置一样简单,但这有助于保持收件箱的清洁,在生产中,你只需要更改smtp detiils。

如果你正在编写任何测试,你可以使用我创建的这个api库。

查看此Mailtrap库