Jump to content

Ci Email Undefined Index: Subject


powpow

Recommended Posts

Hello Freaks,

 

I am currently running into an issue with using CI email. It seems that when using my own created model that calls the email library I some how receive two emails. IMO the second email triggers the error because it does not have a subject(Note: I also receive the second email with no subject):

 

A PHP Error was encountered

Severity: Notice

Message: Undefined index: Subject

Filename: libraries/Email.php

Line Number: 982

 

Here is my controller:

 

 

$this->Usr_model->reg_confirm($regArray);

$msg = array (
'to' => $this->input->post('reg_email'),
'from' => 'XXXXXXXl@yahoo.com',
'sub' => 'Your energy service request.',
'msg' => "Thank you, you have successfully submitted your information. A representative will contact you within 3 business days.");
$this->Usr_model->email($msg);
$this->load->view('view_success',$msg);

 

and here is the module:

 

 

function email($msg)
{

extract($msg);

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

$this->email->from($to);
$this->email->to($from);
$this->email->subject($sub);
$this->email->message($msg);

$this->email->send();

if ( ! $this->email->send())
{
echo $this->email->print_debugger();
}
} 

Link to comment
Share on other sites

I would avoid the use of extract(), it makes your code much more difficult to understand.

 

## Your code, with MY comments and SOME changes
function email($msg)
{

## extract($msg); ## Avoid the use of extract()

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

/* ## change the following
 $this->email->from($to);
 $this->email->to($from);
 $this->email->subject($sub);
 $this->email->message($msg); ## the extract overwrote your original $msg, I think?
*/ ## to the following
 $this->email->from($msg['to']);
 $this->email->to($msg['from']);
 $this->email->subject($msg['sub']);
 $this->email->message($msg['msg']);

 $this->email->send();  ## This line is sending the email (probably)

 if ( ! $this->email->send())  ## This line is sending the email (probably) AGAIN!!
 {
   echo $this->email->print_debugger();
 }
}

 

You are sending the email twice. I would remove the first call, since there is no error checking on it.

Link to comment
Share on other sites

Look at your original code:

 

function email($msg)
{

 extract($msg);

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

 $this->email->from($to);
 $this->email->to($from);
 $this->email->subject($sub);
 $this->email->message($msg);

 $this->email->send();

 if ( ! $this->email->send())
 {
   echo $this->email->print_debugger();
 }
} 

 

Six months or a year from now, when you come back to make some change to this function, it will not be immediately clear where $to, $from, and $sub come from. And, in fact, $msg is not what it looks like at first. Coming in to the function $msg is an array, but it gets overwritten by the "'msg'" element of that array when you extract() it.

 

My first response to your original question was that $sub was not defined and did not have a value. This was because I did not see the extract() and did not see where the variables were coming from.

 

Also, consider what happens if you decide to use this function in another area of your application. But in that area, you have loaded the $msg array with a whole lot more data, maybe even including an element called "'this'". What happens when you extract that? I really don't know if it will damage the object's reference to itself, or if PHP protects against it. But it would be a difficult bug to track down if it did.

 

I agree that the extract() function seems to be a handy function, and I used to use it when I first started writing PHP. But, as I said, coming back to the code later left me confused. If you are going to use extract, have a look at the third parameter, and make it a point to use it. This puts a prefix on the variable names so you 1) don't overwrite existing variables, and 2) have an indicator of the source of the variable's definition:

 

extract($msg, EXTR_PREFIX_ALL, 'EX_');

echo "To: $EX_to";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.