Chris
Chris

Reputation: 1697

Undefined variable: config. PHP error but email is sent

Greetings!

I just downloaded version 2.0.1 of CodeIgniter framework and I am attempting to write my own email controller like so:

<?php

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

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

        $this->email->from('[email protected]', 'some.email.name');
        $this->email->to('[email protected]');        
        $this->email->subject('This is an email test');        
        $this->email->message('It is working!');

        $path = $this->config->item('server_root');        

        $file = $path . '/attachments/readme.txt';

        $this->email->attach($file);

        if($this->email->send())         {             echo 'Email sent.';         }        
        else        {            show_error($this->email->print_debugger());        }
    }
}

If I point my browser to "http://localhost/CodeIgniter/index.php/email", I get this PHP error message

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: config
Filename: controllers/email.php
Line Number: 24

The line number 24 points to this line of code in my controller $this->load->library('email', $config);

In spite of the PHP error, email is being sent fine, with a file attachment.

How can I remove this PHP error? What am I doing wrong?

Upvotes: 1

Views: 3851

Answers (3)

Sibghatullah Ansari
Sibghatullah Ansari

Reputation: 1

$config = Array(); I you initialize it like this than your array be will empty so you have no need to define array when you load library configuration properties will configured automatically. No need to pass $config variable only do this
$this->load->library('email'); it will work fine for u.

Upvotes: 0

StevenBullen
StevenBullen

Reputation: 55

No you should not fix it by defining it, that is incorrect.

You need to change

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

to

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

The array does not exist because you have set the configuration details in your config/email.php file so the $config array is not required AGAIN. :)

Upvotes: 3

code_burgar
code_burgar

Reputation: 12323

As the message states, $config is not defined. You could fix it by defining it.

$config = Array();

somewhere before line 24 should do it

Upvotes: 3

Related Questions