0

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}

1 Answer 1

0

Have you considered using existing add-ons?

Freeform allows you to create forms on the frontend that store the submission in the database, has a Control Panel interface and allows for email sending (and customising the templates) to both the customer and admin. You can pass custom values from the form to be included in the email I believe.

Assuming you want to store the form data (reservation) in your own DB table, you can send them to another URL/template as a confirmation and trigger Email from Template, which allows you to send off an email using the content of a template (which could be populated by the form data they'd just entered).

Without actually solving your direct question as to why it works with the sleep and not without, I would express concern about the security of your code - at a basic level you're not checking for the validity of "naam", without reading up, I'm not sure if the API call makes security checks or assumes the data has already been sanitised (so possibly leaving the database open for injection) or more likely will your code allow the form to be used as a bot mailer?

1
  • Thank you, I tried to simplify my code for SE so I left out validation checks. I will look into Email from Template. Oct 27, 2014 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.