2012-08-12 89 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 */