繁体   English   中英

SMTP 邮件未发送 - Codeigniter 电子邮件库

[英]SMTP Mail not sending - Codeigniter Email Library

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

欢迎.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. 试过了,谷歌 SMTP,还是不发送。 (使用 PHPMailer 库来测试凭据。它正在处理它们。)

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

$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;
}

解决方案:问题已解决 罪魁祸首是Valid Hostname & Reverse DNS

详细信息:由于配置错误,SMTP 响应时间约为7 - 10 secs 根据文档,如果我们不指定smtp_timeout ,它将采用默认值5 sec 所以我将smtp_timeout从默认的5 sec更改为10 sec它可以正常工作

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

由于现在的安全检查,通过 SMTP 发送电子邮件可能非常棘手。

验证是否:

  1. 您的服务器上有 rDNS ,它与您的服务器 IP 匹配
  2. 在您用来发送电子邮件的电子邮件上禁用两步验证
  3. 在您用来发送电子邮件的电子邮件上启用 IMAP 和 POP

按如下方式更改 CI 设置:

    //Settings for CI
    $config['protocol'] = 'smtp';  
    $config['smtp_host'] = 'ssl://smtp.zoho.com'; 
    $config['smtp_user'] = 'anyemail@server.com'; 
    $config['smtp_pass'] = '***********';
    $config['smtp_port'] = '465';
    $config['smtp_crypto'] = 'ssl';

    //Google Settings for CI
    $config['protocol'] = 'ssmtp';  
    $config['smtp_host'] = 'ssl://ssmtp.gmail.com'; 
    $config['smtp_user'] = 'anyemail@server.com'; 
    $config['smtp_pass'] = '***********';
    $config['smtp_port'] = '465';
    $config['smtp_crypto'] = 'ssl';

    //general settings
    $config['_smtp_auth'] = TRUE; //important
    $config['smtp_timeout'] = 30;
    $config['charset'] = 'utf-8';
    $config['mailtype'] = 'html';
    //optional
    $config['wrapchars'] = 76;
    $config['wordwrap'] = TRUE;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM