Search code examples
phpcodeigniteremailsmtpphpmailer

CodeIgniter, PHPMailer & SendGrid Not Working on a cPanel-Based Host


I have CodeIgniter project running on a cPanel/centOS based host. The CodeIgniter email class (running off of phpMailer v 6.1.4) is not working on this host, but is working locally and in other environments.

  • When we run the code below on our cPanel-based host, we get an uninterpretable error from the PHPMailer debugging function:

    The following SMTP error was encountered: +�W��TV���:����;�D���q�� Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

  • The weird thing is that everything was working until two days ago, and nothing was changed (to my knowledge) on the code-level.

  • When we check error logs we see the following:

Severity: Warning --> stream_socket_enable_crypto(): Peer certificate CN=*.mywebhost.com' did not match expected CN=smtp.sendgrid.net' /home/myenv/public_html/system/libraries/Email.php

The above leads me to believe that:

  • The host is utilizing some kind of configuration (such as "FKA SMTP Tweak") preventing us from connecting to SendGrid. (I toggled this on/off, but it didn't seem to to anything).
  • There's some kind of SSL issue

I've been trying to diagnose with our host, but they have been rather unhelpful until now, so any insights would be most welcome and thanks in advance!

Please let us know if we can provide any more information and thank you in advance for your cooperation.

$config                     = array();
$config['useragent']        = 'PHPMailer';
$config['protocol']         = 'smtp';
$config['mailpath']         = '/usr/sbin/sendmail';
$config['smtp_host']        = 'smtp.sendgrid.net';
$config['smtp_user']        = 'USER';
$config['smtp_pass']        = 'PASSWORD';
$config['smtp_port']        = 587;
$config['smtp_timeout']     = 5;
$config['smtp_crypto']      = 'tls';
$config['wordwrap']         = true;
$config['wrapchars']        = 76;
$config['mailtype']         = 'html';
$config['charset']          = 'iso-8859-1';
$config['validate']         = true;
$config['priority']         = 3;
$config['crlf']             = "\n";
$config['newline']          = "\n";
$config['bcc_batch_mode']   = false;
$config['bcc_batch_size']   = 200;

$this->load->library('email');
$this->email->initialize($config);
$this->email->from('[email protected]', 'Your Name');
$this->email->to('[email protected]');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
show_error($this->email->print_debugger());

Solution

  • You've got this in your config:

    $config['smtp_host']        = 'smtp.sendgrid.net';
    

    But you're seeing this in your errors:

    Peer certificate CN=*.mywebhost.com did not match expected CN=smtp.sendgrid.net
    

    This tells us that your hosting provider is redirecting SMTP traffic to their own mail server. Because this redirection is invisible to you, it's left up to TLS to spot that the expected name does not match the presented certificate, and abort the connection, because this is in effect a man-in-the-middle attack.

    You need to ask your provider to stop redirecting your SMTP traffic, or find a new hosting service.