2015-10-15 99 views
1

我使用codeigniter版本2.2.1发送电子邮件。但它给了我错误。请告诉我为什么 ?这是我的PHP代码。为什么发送电子邮件在codeigniter中不起作用?

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

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

     $this->email->set_newline("\r\n"); 
     $this->email->from('[email protected]', "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" <[email protected]> 
Return-Path: <[email protected]> 
To: [email protected] 
Subject: =?utf-8?Q?subject?= 
Reply-To: "[email protected]" <[email protected]> 
X-Sender: [email protected] 
X-Mailer: CodeIgniter 
X-Priority: 3 (Normal) 
Message-ID: <[email protected]> 
Mime-Version: 1.0 


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

Every find bro ? I am here for u . 

我使用本地主机的XAMPP。那么我需要在我的本地主机上配置 吗?

+0

其验证失败,意味着用户名或密码不正确 –

+0

对不起。发布错误的错误消息。现在我编辑它。那么请问是什么问题? –

+0

您的startTLS命令未执行,这意味着TLS端口被禁用,或者它们未被添加到防火墙中 –

回答

0

谢谢大家的帮助。最后我得到了答案。问题来自谷歌。它阻止了不太安全的应用程序。因此,我登录google后导航到此网址https://www.google.com/settings/security/lesssecureapps,并打开访问安全性较低的应用。你可以在那里找到更多的细节。

0

试试这个

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/

+0

$ email_setting的其余部分与我的$ config相同吗? –

+0

是的,你可以使用任何一个 – Ricky

+0

尝试先用我的代码运行,并检查它是否工作,你可以稍后修改你的代码 – Ricky

0

这是一个完全有效的电子邮件功能,我目前正在使用。

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 = "[email protected]"; 
$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 
} 
+0

这不起作用 –

+0

什么是错误? – hzq

+0

如果这是同一个错误,那么我建议你按照他对你的问题的评论@ noobie-php建议的链接。来自函数的 – hzq

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(); 

} 
0

做检查你下面演示代码

$config = array( 
    'protocol' => 'smtp', 
    'smtp_host' => 'mail.google.com', 
    //'smtp_port' => '465', 
    'smtp_timeout' => '7', 
    'smtp_user' => '[email protected]', 
    '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('[email protected],[email protected],[email protected]'); 
$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` 

请与检查你的代码上面的片段。使用// echo $this->email->print_debugger();调试您的邮件代码。如果仍然存在问题,请在此发布。

相关问题