簡體   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