user2960754
user2960754

Reputation: 233

Codeigniter - email doesn't read config setting

We have a website built with the Codeigniter framework. This site has been working OK (~8 months) until last 2 weeks.

So we have a form, once the user submits the form, we insert the data to our MySQL database and send an email to the user. Now, the database part works fine. But the email function stopped working 2 weeks ago. And I'm sure I didn't change anything in the code.

I created a test for this purpose

public function test_email(){ 

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

    $ddd = $this->load->config('email');  
    $data = array(
        //must have
        'recipient'         => '[email protected]',
        '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);

}

and my libraries/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;
       }

    }
}

and config/email.php

$config = Array( 
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => '[email protected]', 
'smtp_pass' => 'myPassword',
'mailtype'  => 'html', 
'charset'   => 'utf-8',
'crlf'      => "\r\n",       
'newline'   => "\r\n",       

'system_sender_mail'    => '[email protected]',
'system_sender_name'    => 'Webmaster',
'auto_bcc_to'           => '[email protected]'
);

Now, the email stopped working and we get this error

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

I tried to print_r the email config item (the smtp setting) and it retrieves the correct data. How should I proceed?

Thank you in advance.

Upvotes: 0

Views: 1999

Answers (3)

jai
jai

Reputation: 549

This is helpful I have found a better way to test email sending in codeigniter.

I am using mailtrap api to send email and have testing , its as simple as using smtp setting , but this helps keep inbox clean and on production you just have change smtp detiils .

If you are writing any test , you can use this api lib i have created.

look into this Mailtrap lib

Upvotes: 0

user2503896
user2503896

Reputation:

Thiss function is working for me.

function email_send(){
    $config = Array(
            'protocol'=>'smtp',
            'smtp_host' => 'smtp.gmail.com',
            'smtp_port' => '25',
            'SMTPAuth' => true,
            'smtp_user' => '[email protected]',
            'smtp_pass' => 'password'
        );

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

    $this->email->from('[email protected]');

    $this->email->to('[email protected]');
    $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());
    }
}

Upvotes: 2

Imran Qamer
Imran Qamer

Reputation: 2263

your email setting and port should be changed like

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

Upvotes: 0

Related Questions