2012-08-12 91 views
2

我在這個網站和其他許多網站上看到過相同的問題,但是他們提供的解決方案對我而言並不適用。所以我再次問。無法從codeigniter框架發送電子郵件

要發送的郵件我寫了下面的代碼

<?php 

class Email extends CI_Controller{ 
function __construct() 
{ 
    parent::__construct(); 
} 

function index() 
{ 
    $config = Array(
     'protocol' => 'smtp', 
     'smtp_host' => 'ssl://smtp.googlemail.com', 
     'smtp_port' => 465, 
     'username' => '[email protected]', 
     'password' => 'password' 
    ); 

    $this->load->library('email', $config); 
    $this->email->set_newline("\r\n"); 

    $this->email->from('[email protected]', 'mymail bcc'); 
    $this->email->to('[email protected]'); 
    $this->email->subject('This is a test email'); 
    $this->email->message('Oops This is Great.'); 

    if($this->email->send()) 
    { 
     echo 'Your email was sent';  
    } 

    else 
    { 
     show_error($this->email->print_debugger()); 
    } 
     } 
     } 

我安裝OpenSSL和也從註釋掉php.ini中的延長線= php_openssl.dll。但我得到以下錯誤

An Error Was Encountered 

    220 mx.google.com ESMTP qp6sm2730344pbc.55 

    hello: 250-mx.google.com at your service, [119.30.39.78] 
    250-SIZE 35882577 
    250-8BITMIME 
    250-AUTH LOGIN PLAIN XOAUTH 
    250 ENHANCEDSTATUSCODES 

    from: 530-5.5.1 Authentication Required. Learn more at 
    530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 qp6sm2730344pbc.55 

    The following SMTP error was encountered: 530-5.5.1 Authentication Required. Learn 
     more   at   
    530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 qp6sm2730344pbc.55 

    to: 530-5.5.1 Authentication Required. Learn more at 
    530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 qp6sm2730344pbc.55 

    The following SMTP error was encountered: 530-5.5.1 Authentication Required. 
    Learn more at 
    530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 qp6sm2730344pbc.55 

    data: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 
    http://support.google.com/mail/bin/answer.py?answer=14257 qp6sm2730344pbc.55 

    The following SMTP error was encountered: 530-5.5.1 Authentication Required. Learn more at 
     530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 qp6sm2730344pbc.55 
    502 5.5.1 Unrecognized command. qp6sm2730344pbc.55 
    The following SMTP error was encountered: 502 5.5.1 Unrecognized command. qp6sm2730344pbc.55 
    Unable to send email using PHP SMTP. Your server might not be configured to send 
     mail using this method. 

當我使用父:: CI_Controller();我得到一個致命錯誤致命錯誤:調用未定義的方法CI_Controller :: CI_Controller()在C:\ wamp \ www \ test \ application \ controllers \ email.php在第6行 雖然我使用的是php 5.4.3

請讓我知道我在這裏失蹤。

+0

我沒有看到你的榜樣'父::是CI_Controller()'調用,但是這句法以來已PHP5.0棄用。這個例子的'parent :: __ construct();'是新的語法。 – complex857 2012-08-12 12:33:37

回答

1

內置的Mail類使用密鑰名稱smtp_usersmtp_pass作爲smtp證書。

改變你$config陣列usernamepassword PARAMS到smtp_usersmtp_pass

+0

現在我獲得了大量的錯誤,下面是一些錯誤時遇到 嚴重性PHP錯誤的:警告 消息:的fsockopen():無法連接到SSL://smtp.googlemail.com:465(A連接嘗試失敗,因爲連接方在一段時間後沒有正確響應,或由於連接的主機未能響應而建立連接失敗。) 文件名:libraries/Email.php 行號:1689 遇到PHP錯誤 消息:fwrite()期望參數1是資源,布爾給定 – asdfkjasdfjk 2012-08-12 17:29:17

+0

聽起來像是一個網絡問題,它是否適用於非SSL?'smtp_host'=>'smtp.gmail.com ''和''smpt_port'=> 25'?我懷疑這個ssl擴展正在起作用。 – complex857 2012-08-12 17:37:17

0

工作守則

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

/* SENDS EMAIL WITH GMAIL */ 

class Email extends CI_Controller { 


    function __construct() 
    { 
     parent::__construct(); 
     //Do your magic here 
    } 

    function index() 
    { 
     $config = array(
      'protocol' => 'smtp', 
      'smtp_host' => 'ssl://smtp.gmail.com', 
      'smtp_port' => 465, 
      'smtp_user' => '[email protected]', 
      'smtp_pass' => '*********' 
     ); 

     $this->load->library('email',$config); 
     $this->email->set_newline("\r\n"); 
     // $this->email->initialize($config); 

     $this->email->from('[email protected]', 'Chirag'); 
     $this->email->to('[email protected]'); 
     $this->email->cc('[email protected]'); 
     $this->email->subject('This is an email testing'); 
     $this->email->message('It\'s working, that\'s really awesome..!'); 

     if ($this->email->send()) 
     { 
      echo "Your Email has been sent successfully... !!"; 
     } 
     else 
     { 
      show_error($this->email->print_debugger()); 
     } 
    } 

} 

/* End of file email.php */ 
/* Location: ./application/controllers/email.php */