【问题标题】:Why sending email is not working in CodeIgniter?为什么在 CodeIgniter 中无法发送电子邮件?
【发布时间】:2019-05-29 01:20:58
【问题描述】:

我正在使用 CodeIgniter 版本 2.2.1 发送电子邮件。但这给了我错误。这是我的 PHP 代码:

       $config = array(
            'protocol'=>'smtp',
            'smtp_host'=>'ssl://smtp.googlemail.com',
            'smtp_port'=>465,
            'smtp_user'=>'xxx@gmail.com',
            'smtp_pass'=>'xxx'
        );

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

        $this->email->set_newline("\r\n");
        $this->email->from('xxx@gmail.com', "My Name");
        $this->email->to($to);
        $this->email->subject($subject);
        $this->email->message($message);
        $this->email->send();
        return $this->email->print_debugger();

但是当我发送时,它给了我这个错误。我尝试了很多方法。为什么会这样?

   220 smtp.gmail.com ESMTP ci2sm13227458pbc.66 - gsmtp

hello: 250-smtp.gmail.com at your service, [61.4.76.240]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8

Failed to send AUTH LOGIN command. Error: 530 5.7.0 Must issue a STARTTLS command first. ci2sm13227458pbc.66 - gsmtp

from: 530 5.7.0 Must issue a STARTTLS command first. ci2sm13227458pbc.66 - gsmtp

The following SMTP error was encountered: 530 5.7.0 Must issue a STARTTLS command first. ci2sm13227458pbc.66 - gsmtp

to: 530 5.7.0 Must issue a STARTTLS command first. ci2sm13227458pbc.66 - gsmtp

The following SMTP error was encountered: 530 5.7.0 Must issue a STARTTLS command first. ci2sm13227458pbc.66 - gsmtp

data: 530 5.7.0 Must issue a STARTTLS command first. ci2sm13227458pbc.66 - gsmtp

The following SMTP error was encountered: 530 5.7.0 Must issue a STARTTLS command first. ci2sm13227458pbc.66 - gsmtp
502 5.5.1 Unrecognized command. ci2sm13227458pbc.66 - gsmtp
The following SMTP error was encountered: 502 5.5.1 Unrecognized command. ci2sm13227458pbc.66 - gsmtp
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

User-Agent: CodeIgniter
Date: Thu, 15 Oct 2015 08:45:28 +0200
From: "War" <iljimae.ic@gmail.com>
Return-Path: <iljimae.ic@gmail.com>
To: iljimae.ic@gmail.com
Subject: =?utf-8?Q?subject?=
Reply-To: "iljimae.ic@gmail.com" <iljimae.ic@gmail.com>
X-Sender: iljimae.ic@gmail.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <561f4b884c5d7@gmail.com>
Mime-Version: 1.0


Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

Every find bro ? I am here for u .

我正在使用 xampp 的 localhost。我需要在我的 localhost 中配置一些东西吗?

【问题讨论】:

  • 认证失败,表示用户名或密码错误
  • 对不起。发布了错误的错误信息。现在我编辑了它。那么请问是什么问题?
  • 你的 startTLS 命令没有执行,这意味着 TLS 端口要么被禁用,要么没有添加到防火墙中
  • 请问如何启用 TLS 端口?
  • 你的操作系统是什么?

标签: php codeigniter email authentication


【解决方案1】:

试试这个:

public function some_name() {

    $email = $this->input->post('email');

    $config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => '*****@gmail.com',
        'smtp_pass' => '*****',
        'mailtype'  => 'html', 
        'charset' => 'utf-8',
        'wordwrap' => TRUE
    );            

    /* 
    *
    * Send email with #temp_pass as a link
    *
    */

    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $this->email->from('****@gmail.com', "****");
    $this->email->to($email);
    $this->email->subject("***********");

    $message = "<p>Your Account ************</p>";
    $this->email->message($message);
    $result = $this->email->send();

}

【讨论】:

    【解决方案2】:

    这是我目前正在使用的完全可用的电子邮件功能。

    public function send_email($to, $subject, $message)
    {
        // Initilize email configuration
        $config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'intsmtp.abc.com.sg',
        'mailtype' => 'html',
        'charset' => 'iso-8859-1',
        'wordwrap' => TRUE
        );
    
        $this->load->library('email', $config);
        $this->email->set_newline("\r\n");
        $this->email->from('ABCMaster');
        $this->email->to($to);
        $this->email->subject($subject);
        $this->email->message($message);
    
        return ($this->email->send()); // returns TRUE or FALSE
    }
    

    我是这样使用它的:

    $to = "abc@gmail.com";
    $subject = "Learning ABCs";
    $message = "ABCs are very simple. I am using them now.<br>";
    $message .= "If you would like to learn more, please contact me.";
    
    // Send email
    if ( $this->send_email( $to, $subject, $message ) )
    {
        // Sending email success
    }
    else
    {
        // Sending Email has failed
    }
    

    【讨论】:

    • 如果是同样的错误,那么我建议您从他对您的问题的评论中关注@noobie-php 建议的链接。
    • 函数取两个参数。对吧?
    【解决方案3】:

    试试这个

     public function sendEmail(){
        //now copy and paste the below code
        $this->load->library('email');  
        $html = "Test email";
         $email_setting = array('mailtype'=>'html');
         $this->email->initialize($email_setting);
         $this->email->from('Test');
         $this->email->to('the email address');
         $this->email->subject('Title': 'New email');
         $this->email->message($html);
         $this->email->send();
    }
    

    让我知道这是否有效。 您可以稍后修改您的代码。 有关如何在 codeigniter 中发送电子邮件的更多信息,请尝试此链接 http://w3code.in/2015/09/how-to-send-email-using-codeigniter/

    【讨论】:

    • $email_setting 的其余部分将与我的 $config 相同?
    • 是的,您可以使用两者中的任何一种
    • 先尝试运行我的代码并检查它是否有效,您可以稍后修改您的代码
    • 好的,我已经更新了我的答案。将代码复制并粘贴到您的控制器中,并将您的电子邮件地址更新为 $this->email->to('the email address');并运行该功能。此代码正在我的系统上运行
    • 更多我给你的链接。
    【解决方案4】:

    请使用以下 sn-ps 检查您的代码

    $config = array( 
        'protocol' => 'smtp', 
        'smtp_host' => 'mail.google.com', 
        //'smtp_port' => '465', 
        'smtp_timeout' => '7', 
        'smtp_user' => 'info@gmail.com', 
        'smtp_pass' => 'password', 
        'charset' => 'utf-8', 
        'newline' => "rn", 
        'mailtype' => 'html', // or html 
        'wordwrap' => FALSE, 
        'validation' => TRUE // bool whether to validate email or not 
    ); 
    
    $this->load->library('email'); 
    $this->email->initialize($config); 
    $this->email->set_newline("rn"); 
    $this->email->from($email, 'Subject'); 
    $this->email->to('nam@gmail.com,nam@gmail.com,nam@gmail.com'); 
    $this->email->subject('Contact us form by: ' . $name); 
    $this->email->message($message . '<br>website:' . $website); 
    $this->email->send(); 
    
    //echo $this->email->print_debugger(); `enter code here`
    

    请使用上面的 sn-p 检查您的代码。使用 //echo $this-&gt;email-&gt;print_debugger(); 调试您的邮件代码。如果问题仍然存在,请在此处发布。

    【讨论】:

      【解决方案5】:

      谢谢大家帮助我。终于我得到了答案。问题来自谷歌。它正在阻止不太安全的应用程序。所以我只是在登录谷歌后导航到这个 url https://www.google.com/settings/security/lesssecureapps 并打开不太安全的应用程序的访问权限。您可以在那里找到更多详细信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-12-05
        • 1970-01-01
        • 1970-01-01
        • 2013-08-13
        • 1970-01-01
        • 2014-02-20
        • 2013-06-15
        相关资源
        最近更新 更多