powpow Posted October 18, 2012 Share Posted October 18, 2012 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' => '[email protected]', '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(); } } Quote Link to comment https://forums.phpfreaks.com/topic/269607-ci-email-undefined-index-subject/ Share on other sites More sharing options...
DavidAM Posted October 18, 2012 Share Posted October 18, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/269607-ci-email-undefined-index-subject/#findComment-1386116 Share on other sites More sharing options...
powpow Posted October 19, 2012 Author Share Posted October 19, 2012 well it is so clear now... also I was wondering with your comment on not using extract, it seems like it is such a convenient function to use is it depreciated or prone to error? Quote Link to comment https://forums.phpfreaks.com/topic/269607-ci-email-undefined-index-subject/#findComment-1386428 Share on other sites More sharing options...
DavidAM Posted October 19, 2012 Share Posted October 19, 2012 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"; Quote Link to comment https://forums.phpfreaks.com/topic/269607-ci-email-undefined-index-subject/#findComment-1386474 Share on other sites More sharing options...
powpow Posted October 23, 2012 Author Share Posted October 23, 2012 Thank you for taking the time and explaining it, its one thing to know and another to share it. If I use extract I will be sure to use the 3rd parameter as you have shown. Quote Link to comment https://forums.phpfreaks.com/topic/269607-ci-email-undefined-index-subject/#findComment-1387152 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.