I’m creating a custom ext. that handles form submission + email sending. When a form or reservation has been made the data is put in EE and 2 emails are being send. One email to the client and one to the admin. Sometimes my sendMail function doesn’t send the body of the first email and I’m getting an empty email. The body of the email is an EE template loaded with file_get_contents. When I add sleep(2) (delay execution) before the emails are send it works fine. I have no idea what the problem is.
Maybe I should do this in another way? Hope you guys can help me out.
public function template_fetch_template($row)
{
// -------------------------------------------
// Get the latest version of $row
// -------------------------------------------
if (ee()->extensions->last_call !== FALSE)
{
$row = ee()->extensions->last_call;
}
if (ee()->uri->segment(1) == "reservation")
{
$this->reservation();
}
return $row;
}
private function reservation()
{
static $run;
if ($run) return;
if (ee()->uri->segment(1) == "reservation" && ee()->uri->segment(2) != "" && ee()->uri->segment(3) == "")
{
if (count($_POST))
{
$naam = $_POST["naam"];
ee()->session->create_new_session(2);
ee()->session->fetch_session_data();
ee()->session->fetch_member_data();
$channel_id = 21;
ee()->load->library('api');
ee()->api->instantiate('channel_entries');
ee()->api->instantiate('channel_fields');
$data = array(
'entry_id'=> 0,
'title'=> $naam,
'entry_date' => time(),
'edit_date' => time(),
'member_id' => 2
);
ee()->api_channel_fields->setup_entry_settings($channel_id, $data);
if (ee()->api_channel_entries->save_entry($data, $channel_id))
{
$entry_id = ee()->api_channel_entries->entry_id;
sleep(2);
// CLIENT EMAIL
$this->sendMail("e-mail","client_".$this->res_email_template, $entry_id, “[email protected]”, "Client Reservatie ". ee()->config->_global_vars['site'] . " " . $entry_id);
// ADMIN EMAIL
$this->sendMail("e-mail","admin_".$this->res_email_template, $entry_id, “[email protected], "Admin Reservatie ". ee()->config->_global_vars['site']. " " . $entry_id);
ee()->functions->redirect("/".ee()->uri->segment(1)."/".ee()->uri->segment(2)."/".$entry_id);
}
}
}
$run = true;
}
private function sendMail($template_group, $template, $entry_id, $to_email, $subject)
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.priorweb.be',
'smtp_timeout'=>30,
'smtp_port' => 587,
'mailtype' => 'html',
'charset' => 'utf-8',
'wordwrap' => FALSE
);
ee()->email->clear();
ee()->load->library('email', $config);
ee()->load->helper('text');
ee()->email->mailtype = 'html';
ee()->email->from(‘[email protected]');
ee()->email->to($to_email);
ee()->email->subject($subject);
ee()->email->message(entities_to_ascii(file_get_contents(ee()->config->site_url().ee()->publisher_lib->lang_code."/".$template_group."/".$template."/".$entry_id, FILE_USE_INCLUDE_PATH)));
ee()->email->send();
if ( ! ee()->email->send())
{
// Send failed, data was not cleared
ee()->email->clear();
}
}
/*****/ EMAIL TEMPLATE SOME HTML WITH THIS TAG
{exp:channel:entries channel="reserveer_berichten" limit="1" dynamic="off" entry_id="{segment_3}"}
{title}
{/exp:channel:entries}