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

时间:2015-02-24 02:13:06

标签: php codeigniter email

我们有一个使用Codeigniter框架构建的网站。这个网站一直工作正常(约8个月),直到最近两周。

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

我为此目的创建了一个测试

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

我尝试print_r电子邮件配置项(smtp设置)并检索正确的数据。我该怎么办?

提前谢谢。

3 个答案:

答案 0 :(得分:2)

这个功能对我有用。

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());
    }
}

答案 1 :(得分:0)

您的电子邮件设置和端口应更改为

$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', 
);

答案 2 :(得分:0)

这有助于我找到一种更好的方法来测试codeigniter中的电子邮件发送。

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

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

查看此Mailtrap lib