【问题标题】:CodeIgniter throws "Message: Call to a member function initialize() on null"CodeIgniter 抛出“消息:在 null 上调用成员函数 initialize()”
【发布时间】:2018-06-25 00:00:55
【问题描述】:

welcome.php 文件

  function formData(){
        if (empty($this->input->post('checkprocess'))) {
                        $email_config = Array(
                            'mailtype' => 'html',
                            'charset' => 'iso-8859-1'
                        );

                        $admin_email ='rockpokhrel@gmail.com';
                        $name = $this->input->post('full_name');
                        $email = $this->input->post('email');
                        $message = "Dear Admin, <br/>" .
                        "Contact Information of Users:" .'<br/>'.
                        "<b>Name</b> :- " . $this->input->post('full_name') .'<br/>'.
                        "<b>Email:-</b> " . $this->input->post('email') .'<br/>'.
                        "<b>Contact Number:-</b> " . $this->input->post('mob_num') .'<br/>'.'<br/>'.
                        "<b>Address:-</b> " . $this->input->post('address') .'<br/>'.'<br/>'.
                        "<b>Message:-</b> " . $this->input->post('message');
                        $this->load->library('email', $email_config);
                        $this->email->set_newline("\r\n");
                        $this->email->from($this->input->post('email'), $this->input->post('name'));
                        $this->email->to($admin_email);
                        $this->email->subject("Public Contact/Feedback");
                        $this->email->message($message);

                        if ($this->email->send()) {
                        echo "<meta http-equiv=REFRESH CONTENT=0;url='".base_url()."'>";exit();

                        $this->db->insert('tbl_contactusform',$email_config);
                                    redirect(base_url());
                            }
    }
}

库/email.php 文件

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

$config = array(
    'protocol'  => 'smtp',
    'smtp_host' => 'ssl://smtp.example.com',
    'smtp_port' => 465,
    'smtp_user' => '**********@gmail.com',
    'smtp_pass' => '************',
    'mailtype'  => 'html',
    'charset'   => 'utf-8'
);
$this->email->initialize($config);
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");

$htmlContent = '<h1>Sending email via SMTP server</h1>';
$htmlContent .= '<p>This email has sent via SMTP server from CodeIgniter application.</p>';

$this->email->to('**********@gmail.com');
$this->email->from('**********@gmail.com','MyWebsite');
$this->email->subject('How to send email via SMTP server in CodeIgniter');
$this->email->message($htmlContent);


$this->email->send();

我想在网站中添加电子邮件功能,但我得到了这个堆栈跟踪:

遇到 PHP 错误 严重性:通知

消息:未定义属性:Welcome::$email

文件名:MX/Loader.php

行号:304

回溯:

错误和

遇到未捕获的异常类型:错误

消息:在 null 上调用成员函数 initialize()

文件名:E:\xampp\htdocs\website\application\libraries\email.php

行号:17

我是 CodeIgniter 的初学者。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    为了使用电子邮件类,您必须使用加载库

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

    application/config/autoload.php

    $autoload['libraries'] = array('email');
    

    【讨论】:

    • 我试过这种方式,在我的 welcome.php 中,函数 __construct() { parent::__construct(); $this->load->model('Mhome'); $this->load->library('email'); }
    • 刚看到你为什么加library/email.php file。您可以在 config.php 中添加这些配置并在代码中调用它们
    • 没错。可以做。但我更喜欢在 config.php 中进行。简单安全
    • 首先删除这些并将其添加到您的控制器中并检查
    • 哦!谢谢 。它在本地服务器上工作,但在远程服务器上很好:)
    猜你喜欢
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    • 2017-06-18
    • 2019-12-04
    • 2017-08-09
    • 1970-01-01
    • 2020-09-02
    相关资源
    最近更新 更多