无法从codeigniter框架发送电子邮件

时间:2012-08-12 06:12:14

标签: codeigniter email

我在这个和许多其他网站上看到了同样的问题,但他们给出的解决方案对我不起作用。所以我再问一次。

要发送邮件,我写了以下代码

<?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' => 'mymail@gmail.com',
        'password' => 'password' 
    );

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

    $this->email->from('mymail@gmail.com', 'mymail bcc');
    $this->email->to('mymail@gmail.com');
    $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取消注释了line extension = 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.

当我使用parent :: CI_Controller()时;我得到一个致命错误致命错误:在第6行的C:\ wamp \ www \ test \ application \ controllers \ email.php中调用未定义的方法CI_Controller :: CI_Controller() 虽然我使用的是PHP 5.4.3

请告诉我这里缺少的东西。

2 个答案:

答案 0 :(得分:1)

内置的Mail类使用键名smtp_usersmtp_pass来获取smtp凭据。

username数组中的password$config参数更改为smtp_usersmtp_pass

答案 1 :(得分: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' => 'mygmail@gmail.com',
            'smtp_pass' => '*********'
        );

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

        $this->email->from('mygmail@gmail.com', 'Chirag');
        $this->email->to('mygmail@gmail.com');
        $this->email->cc('myanothergmail@gmail.com');   
        $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 */