Welcome Guest, Not a member yet? Register   Sign In
[SOLVED ]Undefined variable: config -- PHP error but email is sent.
#1

[eluser]kristo5747[/eluser]
Greetings!

I am a *newbie* and just downloaded CI version 2.0.1. Please bear with me with this long email.

I am attempting to write my own email controller like so:

Code:
<?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

Quote: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
Quote:$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?
#2

[eluser]kristo5747[/eluser]
Duh!!!

I added $config = Array(); in my index() function. The error went away.

Stupid, stupid, stupid me.
#3

[eluser]Eric Barnes[/eluser]
That is because you are not setting $config. So change your line to: $this->load->library('email');
#4

[eluser]louisl[/eluser]
Change $this->load->library('email', $config); to $this->load->library('email'); unless you actually need to change the default config in which case you'd do something like this:-

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

$config['newline'] = "\r\n";
$config['crlf'] = "\r\n";
//etc...

$this->email->initialize($config);
    
$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!');
//etc...

$this->email->send(); 
#5

[eluser]kristo5747[/eluser]
[quote author="Eric Barnes" date="1302052414"]That is because you are not setting $config. So change your line to: $this->load->library('email');[/quote]

I did what you suggested. Problem went way (makes sense).

Thanks for the reply.




Theme © iAndrew 2016 - Forum software by © MyBB