⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

callback after insert



Reuben Baltazar
  • profile picture
  • Member

Posted 22 August 2013 - 07:38 AM

i want to send a copy of password to a student using their email..

i used callback_after_insert to send the email but it didnt work what is the problem?

 


briggers
  • profile picture
  • Member

Posted 22 August 2013 - 08:02 AM

Please show us your controller and we might be able to tell you


Reuben Baltazar
  • profile picture
  • Member

Posted 22 August 2013 - 08:29 AM

function student($operation = null)
{
$this->unset_data();
 
$this->grocery_crud->set_table('tblstudent');
//$this->grocery_crud->set_theme('datatables');
$this->grocery_crud->set_subject('Student');
$this->grocery_crud->unset_read();
$this->grocery_crud->unset_export();
$this->grocery_crud->unset_columns('image');
$this->grocery_crud->set_relation('CurriculumID','tblcurriculum','CurriculumCode');
$this->grocery_crud->display_as('CurriculumID','Course');
$this->grocery_crud->fields('StudentNumber','CurriculumID','FirstName','LastName','MiddleName','Birthday','Address','ContactNumber','EmailAddress','Status','Year','Active','semester','pass','image');
$this->grocery_crud->required_fields('FirstName','LastName','MiddleName','Birthday','Address');
$this->grocery_crud->set_field_upload('image','assets/uploads/files/Student');
$data = $this->selectCourseDesc();
//$this->grocery_crud->field_type('Active','dropdown',array('1'=>'true','2'=>'false'));
$this->grocery_crud->field_type('pass','hidden',$this->generateRandomString());
$this->grocery_crud->field_type('Birthday','date');
$this->grocery_crud->field_type('CurriculumID','enum',$data);
$this->grocery_crud->callback_edit_field('Birthday',array($this,'returnbirthdate'));
//$this->grocery_crud->callback_after_insert(array($this,'hidden'));
 
if($operation =='insert_validation' || $operation == 'insert')
{
$this->grocery_crud->set_rules('StudentNumber','Student Number','numeric|xss_clean|trim|required|callback_studentNumber_check|is_unique[tblstudent.StudentNumber]');
$this->grocery_crud->set_rules('ContactNumber','Contact Number','numeric|xss_clean|trim|required');
$this->grocery_crud->set_rules('EmailAddress','Email Address','xss_clean|callback_validEmailAdd');
$this->grocery_crud->callback_insert(array($this,'findcourseID'));
}
else
{
$this->grocery_crud->set_rules('StudentNumber','StudentNumber','numeric|xss_clean|trim|required|callback_Student_number_checking');
$this->grocery_crud->set_rules('ContactNumber','Contact Number','numeric|xss_clean|trim|required');
$this->grocery_crud->set_rules('EmailAddress','Email Address','xss_clean|callback_validEmailAdd|callback_Student_email_checking');
$this->grocery_crud->callback_update(array($this,'updatefindcourseID'));
}
$output = $this->grocery_crud->render();
 
$this->_home_output($output);     
}

Amit Shah
  • profile picture
  • Member

Posted 22 August 2013 - 08:31 AM

sir if you refer to your code ...

//$this->grocery_crud->callback_after_insert(array($this,'hidden'));

how can you expect php to execute this piece of code?


Reuben Baltazar
  • profile picture
  • Member

Posted 22 August 2013 - 08:32 AM

this is my calback after insert 

 

function sendEmail($post_array,$primary_key)

{

$this->load->library('email');
$this->email->set_newline("\r\n");
 
$this->email->from('revshare18@gmail.com','reu');
$this->email->to('reubenbaltazar@yahoo.com');
$this->email->subject('sample');
$this->email->message('testing');
 
if($this->email->send())
{
echo 'your email sent';
}
else
{
show_error($this->email->print_debugger());
}

 

}


briggers
  • profile picture
  • Member

Posted 22 August 2013 - 10:33 AM

  1. It really helps us understand what you are doing if you include any code you paste in "code" tags - that's the [<>] button in the editor
  2. Check the documentation at: http://www.grocerycrud.com/documentation/options_functions/callback_after_insert
  3. Note that when you call the function you use
    $crud->callback_after_insert(array($this, 'name_of_callback_function'));
    

    then

    function name_of_callback_function($post_array,$primary_key)
    {
        //your code for what you want to do
     
        return true;
    }
    

    Don't comment out the line.


Reuben Baltazar
  • profile picture
  • Member

Posted 22 August 2013 - 12:00 PM

i include this and it's still didnt work

 

$this->grocery_crud->callback_after_insert(array($this,'sendEmail'));


Reuben Baltazar
  • profile picture
  • Member

Posted 22 August 2013 - 12:07 PM

function sendEmail($post_array,$primary_key)
{
$this->load->library('email');
$this->email->set_newline("\r\n");
 
$this->email->from('revshare18@gmail.com','reu');
$this->email->to('reubenbaltazar@yahoo.com');
$this->email->subject('sample');
$this->email->message('testing');
 
$this->email->send();
return true;
}
 
then this is my callback_after_insert  function  still does not working

Amit Shah
  • profile picture
  • Member

Posted 22 August 2013 - 12:11 PM

how you saying it is not working as? you not receiving mail or is it some other way you commencing on it. Not receiving mail may be reason of configuration of server.

 

what i will suggest you do is.. try making some log entry in file / database to check if it is happening or not. If it does happen.. it means mail functionality has issue.. and not code


Reuben Baltazar
  • profile picture
  • Member

Posted 22 August 2013 - 12:16 PM

the email code is working i already try it


Reuben Baltazar
  • profile picture
  • Member

Posted 22 August 2013 - 12:18 PM

but when i used it as my callback_after_insert it doesnt work  

 

really need some help..


briggers
  • profile picture
  • Member

Posted 22 August 2013 - 14:00 PM

Do you have an email config file stored in application/config/email.php?

 

if you don't then you need something like this:

$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;

$this->email->initialize($config);

after

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

and before you issue the send command

 

See: http://ellislab.com/codeigniter/user-guide/libraries/email.html - Setting Email Preferences

 

so you function might look like this

function sendEmail($post_array,$primary_key)
{
$this->load->library('email');

// email config start ---------------
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;

$this->email->initialize($config);
// email config end --------------------

$this->email->set_newline("\r\n");
 
$this->email->from('revshare18@gmail.com','reu');
$this->email->to('reubenbaltazar@yahoo.com');
$this->email->subject('sample');
$this->email->message('testing');
 
$this->email->send();
return true;
}

Reuben Baltazar
  • profile picture
  • Member

Posted 22 August 2013 - 14:16 PM

yes a already tested the mail function and its fine... 

 

how to check if the callback_after_insert is working? 

 

i think the callback function isn't working


briggers
  • profile picture
  • Member

Posted 22 August 2013 - 14:54 PM

what is in your file: application/config/email.php ?


Reuben Baltazar
  • profile picture
  • Member

Posted 22 August 2013 - 15:57 PM

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

 
$config['protocol'] ='smtp';
$config['smtp_host']='ssl://smtp.googlemail.com';
$config['smtp_port']=465;
$config['smtp_user']='revshare18@gmail.com';
$config['smtp_pass']='secret';
$config['wordwrap'] = TRUE;
 
 
?>

briggers
  • profile picture
  • Member

Posted 22 August 2013 - 17:48 PM

 

$config['smtp_host']='ssl://smtp.googlemail.com';

 

I don't think you can use that as a host name it should be just smtp.googlemail.com


Reuben Baltazar
  • profile picture
  • Member

Posted 23 August 2013 - 03:11 AM

i already check the email config and it works fine...

i have no problems in my config email..

 

 

my problem is the callback_after_insert   its not working.  

how to check if this function is executing?


Amit Shah
  • profile picture
  • Member

Posted 23 August 2013 - 03:32 AM

can you share your environment with me.. like php version / codeigniter version and grocerycrud version.

 

Also .. what i will suggest you to do is.. do a print / echo of some statement in the function and make it die. ...

then look @the firebug to see the response from insert.. if it gets stuck up with the output ..meaning the function is getting called in but if it dose not .. then it might be a situation to look into.

Make sure you write die() post the print statement.. and try and keep it ahead of all the code in the function so we are sure it is not dying cuz of some other issue.


davidoster
  • profile picture
  • Member

Posted 23 August 2013 - 09:11 AM

 

function sendEmail($post_array,$primary_key)
{
$this->load->library('email');
$this->email->set_newline("\r\n");
 
$this->email->from('revshare18@gmail.com','reu');
$this->email->to('reubenbaltazar@yahoo.com');
$this->email->subject('sample');
$this->email->message('testing');
 
$this->email->send();
return true;
}
 
then this is my callback_after_insert  function  still does not working

 

Hello and welcome to the forums [member=reuben baltazar].

Please remove this line: $this->email->set_newline("\r\n");

Check here why.


briggers
  • profile picture
  • Member

Posted 23 August 2013 - 10:25 AM

i already check the email config and it works fine...

i have no problems in my config email..

 

 

my problem is the callback_after_insert   its not working.  

how to check if this function is executing?

One problem you have is that your callback function calls the mail send library which tries to use the SMTP host:  ssl://smtp.googlemail.com

That host does not exist - try pinging it and you will see

ping ssl://smtp.googlemail.com
ping: unknown host ssl://smtp.googlemail.com

So the mail function returns an error to your callback. Hostnames can only contain letters, numbers and hyphens (-) but not / or : and are always 

something.something1.something2

 

Try pinging smtp.googlemail.com and you will get something like

ping smtp.googlemail.com
PING googlemail-smtp.l.google.com (74.125.136.16) 56(84) bytes of data.
64 bytes from ea-in-f16.1e100.net (74.125.136.16): icmp_req=1 ttl=45 time=34.1 ms
64 bytes from ea-in-f16.1e100.net (74.125.136.16): icmp_req=2 ttl=45 time=32.7 ms
64 bytes from ea-in-f16.1e100.net (74.125.136.16): icmp_req=3 ttl=45 time=31.4 ms

which shows that exists.

 

Nothing can send mail to a non-existent host.

 
There may be other errors, but this is a show-stopper