【问题标题】:SMTP Mail not sending - Codeigniter Email LibrarySMTP 邮件未发送 - Codeigniter 电子邮件库
【发布时间】:2018-12-08 16:01:03
【问题描述】:

我在通过 SMTP 协议发送邮件时遇到问题。

Welcome.php

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

$config = array();
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.zoho.com';
$config['smtp_user'] = 'support@domain.com';
$config['smtp_pass'] = '**************';
$config['smtp_port'] = 465;
$config["smtp_crypto"] = "ssl";

$this->email->initialize($config);

$this->email->set_newline("\r\n");
$this->email->from('support@domain.com', 'Support name'); // change it to yours
$this->email->to($to);// change it to yours
$this->email->subject($subject);
$this->email->message($message);

    if($this->email->send())
    {
      echo "Success! - An email has been sent to ".$to;
    }
    else
    { 
      show_error($this->email->print_debugger());
      return false;
    }
}

这是输出错误:

An Error Was Encountered
220 mx.zohomail.com SMTP Server ready June 29, 2018 5:16:40 AM PDT 

hello: 

The following SMTP error was encountered: 
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
Date: Fri, 29 Jun 2018 12:16:40 +0000
From: "Support Name" <support@domain.com>
Return-Path: <support@domain.com>
To: recipent@gmail.com
Subject: =?ISO-8859-1?Q?=43=6F=70=79=20=61=6C=6C=20=74=68=65?=
Reply-To: <support@domain.com>
User-Agent: CodeIgniter
X-Sender: support@domain.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <5b36232840595@domain.com>
Mime-Version: 1.0


Content-Type: multipart/alternative; boundary="B_ALT_5b3623284061c"

This is a multi-part message in MIME format.
Your email application may not support this format.

--B_ALT_5b3623284061c
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

Some


--B_ALT_5b3623284061c
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Some Email Description=0A=0A Some Email Description

--B_ALT_5b3623284061c--

注意:该脚本在 localhost 以及多个 其他主机,但不适用于 VPS 主机。

这些是要记住的事情:

  1. 代码没问题(在不同的主机上试过,效果很好)
  2. $config['protocol'] = "smtp"; 更改为$config['protocol'] = "sendmail"; 即可。但我只想通过 SMTP 协议发送邮件。
  3. 使用 Zoho Mail SMTP 协议 (smtp.zoho.com)
  4. 已尝试,Google SMTP,仍然无法发送。 (使用 PHPMailer 库来测试凭据。它正在处理它们。)

【问题讨论】:

  • 但是,使用相同的凭据,在相同的 VPS 中尝试使用 PHPMailer。它在那里工作。邮件按预期发送。问题仅在 CodeIgniter 上。太奇怪了。

标签: php codeigniter email codeigniter-3


【解决方案1】:

我希望这段代码可以正常工作

$Config = [
  'protocol'  => 'smtp', 
  'smtp_host' => 'smtp.zoho.com', 
  'smtp_port' =>  465, 
  'smtp_user' => 'support@domain.com', 
  'smtp_pass' => '**************', 
  'mailtype'  => 'html', 
  'charset'   => 'utf-8'
];
$this->load->library('email',$config);
$this->email->set_newline("\r\n");
$this->email->from('support@domain.com', 'Support name'); // change it to yours
$this->email->to($to);// change it to yours
$this->email->subject($subject);
$this->email->message($message);
if($this->email->send()){
   echo "Success! - An email has been sent to ".$to;
}
else{ 
   show_error($this->email->print_debugger());
   return false;
}

【讨论】:

  • 您的配置是否正确?也许您的 SMTP 服务器不正确
  • 尝试使用 PHPMailer。有效。该脚本也在其他主机上运行...我 100% 确定凭据是正确的。
  • 是的.. 甚至代码也在某些主机上工作。但在 VPS 上,它不起作用.. 是否需要在 codeigniter 中发送邮件?应该看看那些。
【解决方案2】:

解决方案: 问题已修复!罪魁祸首是有效主机名反向DNS

详细信息: 由于配置错误,SMTP 在7 - 10 secs 附近响应。根据docs,如果我们不指定smtp_timeout,则默认为5 sec。 所以我将smtp_timeout 从默认的5 sec 更改为10 sec 并且它可以工作

找出问题所在后,发现 SMTP 响应缓慢。没有添加有效的主机名,反向 DNS。所以补充说。现在它按预期工作。现在我删除了smtp_timeout 字段。它现在正在工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-26
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    • 1970-01-01
    相关资源
    最近更新 更多