Search code examples
codeigniteremail

Issues with email deliverability with CodeIgniter. my code returns email sent, but doesn't get delivered to the recipient on a vps server


Here's my code. I have tried using 'mail' as protocol, yet still doesn't deliver. But it does get delivered on localhost perfectly and on other servers. But on this particular vps server, it doesn't. Thanks in advance.

$config = array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => '[email protected]',
        'smtp_pass' => '***********',
        'smtp_timeout' => 10,
        'mailtype' => 'html',
        'starttls'  => true,
        'newline'   => "\r\n", 
    );
    $this->load->library('email');
    $this->email->initialize($config);
    $this->email->from($sender);
    $this->email->to($email);
    $this->email->subject($subject);
    $this->email->message($mailToSend);
    $flag = $this->email->send();
    if ($flag) {
        return $flag;
    } else {
        return false;
    }

Solution

  • [SOLVED] The Issue was from the mail headers. I didn't set the headers. Here's a working example

    $config = array(
            'protocol' => 'sendmail',
            'smtp_host' => 'ssl://smtp.googlemail.com',
            'smtp_port' => '465',
            'smtp_user' => '[email protected]',
            'smtp_pass' => '****', 
            'smtp_timeout' => 50, 
            'mailtype' => 'html',
            'starttls'  => true,  
            'charset' => 'utf-8',
            'protocol' => 'sendmail',
            'mailpath' => '/usr/sbin/sendmail',
            'charset' => 'iso-8859-1',
            'wordwrap' => TRUE,
        );  
        $this->load->library('email');
        $this->email->initialize($config);  
        
        $this->email->set_header('X-Mailer', 'CodeIgniter');
        $this->email->set_header('X-Priority', '1');
        $this->email->set_header('Subject', $subject);
        $this->email->set_header('Mime-Version', '1.0');
        $this->email->set_header('Importance', 'High');
        $this->email->set_header('X-MSMail-Priority', 'High'); 
    
        $this->email->set_newline("\r\n"); 
        $this->email->from('[email protected]', $sender);
        $this->email->to($email);
        $this->email->cc('[email protected]');  
        $this->email->subject($subject);
        $this->email->message($mailToSend);
        $flag = $this->email->send();
        if ($flag) {
            return $flag;
        } else {
            return false;
        }