CodeIgniter、PHPMailer 和 SendGrid 无法在基于 cPanel 的主机上运行

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

我在基于 cPanel/centOS 的主机上有 CodeIgniter 项目 运行ning。 CodeIgniter email class (running off of phpMailer v 6.1.4) 不在此主机上运行,​​但在本地和其他环境中运行。

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

以上让我相信:

我一直在尝试通过我们的房东进行诊断,但直到现在他们都没有帮助,所以我们非常欢迎任何见解并提前致谢!

如果我们可以提供更多信息,请告知我们,在此先感谢您的合作。

$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('my@email.com', 'Your Name');
$this->email->to('my@email.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
show_error($this->email->print_debugger());

你的配置中有这个:

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

但是您在错误中看到了这一点:

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

这告诉我们您的托管服务提供商正在将 SMTP 流量重定向到他们自己的邮件服务器。因为这种重定向对您来说是不可见的,所以由 TLS 来发现预期的名称与提供的证书不匹配,并中止连接,因为这实际上是中间人攻击。

您需要请求您的提供商停止重定向您的 SMTP 流量,或者寻找新的托管服务。

注意:此解决方案可能有所不同,但这就是解决我的问题的方法。 (请参阅@Syncro 的评论,更准确...下面的 'solution' 是我在特定情况下与托管服务提供商解决问题的方式)。

我的托管服务提供商必须:

  • 确保服务器可以接受传出连接
  • 重启防火墙
  • 确保已禁用 FKA SMTP 调整。

就是说,我已经测试了 SendGrid 的 PHP API Library 并且可能会使用它。我认为 PHPMailer 是一个很好的解决方案,但在某些情况下(例如我的情况),某些托管服务提供商可能会干扰它。