/** * This handy function figures out the various components of an email and returns them in a hash: * from, reply_to, to, cc, bcc, subject, html_content and text_content * * This is used both when sending the emails but also for testing. This should be the only place that * email content is actually constructed. All other email functions should be using it, regardless of * what mechanism actually sends the email. * * @param integer $form_id * @param mixed $submission_id for non-test emails, this is included. For testing, it may be blank. * @param integer $email_id * @return array */ function ft_get_email_components($form_id, $submission_id = "", $email_id, $is_test = false, $test_settings = array()) { global $g_table_prefix, $g_root_dir, $LANG, $g_default_theme; $email_template = ft_get_email_template($email_id); // if the administrator limited the email content to fields in a particular View, pass those fields to the // template - NOT all of the form fields (which is the default) $fields_for_email_template = array(); if (!empty($email_template["limit_email_content_to_fields_in_view"])) { $view_fields = ft_get_view_fields($email_template["limit_email_content_to_fields_in_view"]); // here, $view_fields just contains the info from the view_fields table. We need the info from the form_fields // table instead - since it contains presentation information likely to be needed in the email templates $fields_for_email_template = array(); foreach ($view_fields as $view_field_info) { $fields_for_email_template[] = ft_get_form_field($view_field_info["field_id"], array("include_field_type_info" => true)); } } else { $fields_for_email_template = ft_get_form_fields($form_id, array("include_field_type_info" => true)); } // this returns a hash with three keys: html_content, text_content and submission_id $templates = _ft_get_email_template_content($form_id, $submission_id, $email_template, $is_test, $test_settings); $submission_id = $templates["submission_id"]; // unfortunately we need this, even though it was just called in _ft_get_email_template_content() $submission_info = ft_get_submission($form_id, $submission_id); // retrieve the placeholders and their substitutes $submission_placeholders = ft_get_submission_placeholders($form_id, $submission_id); $admin_info = ft_get_admin_info(); $file_info = array(); $updated_fields_for_email_template = array(); foreach ($fields_for_email_template as $field_info) { if ($field_info["is_file_field"] == "yes") { $field_id = $field_info["field_id"]; $field_settings = ft_get_field_settings($field_id); $field_info["folder_url"] = $field_settings["folder_url"]; $field_info["folder_path"] = $field_settings["folder_path"]; $filename = $field_info["field_name"]; $field_info["answer"] = $submission_placeholders["FILENAME_{$filename}"]; } $updated_fields_for_email_template[] = $field_info; } $fields_for_email_template = $updated_fields_for_email_template; $updated_fields_for_email_template = array(); foreach ($fields_for_email_template as $field_info) { while (list($placeholder, $value) = each($submission_placeholders)) { if ($placeholder != "ANSWER_{$field_info["field_name"]}") { continue; } $field_info["answer"] = $value; break; } reset($submission_placeholders); $updated_fields_for_email_template[] = $field_info; } $fields_for_email_template = $updated_fields_for_email_template; $return_info = array(); $return_info["email_id"] = $email_id; $return_info["attachments"] = array(); $smarty = new Smarty(); $smarty->template_dir = "{$g_root_dir}/global/smarty/"; $smarty->compile_dir = "{$g_root_dir}/themes/{$g_default_theme}/cache/"; $smarty->assign("LANG", $LANG); $smarty->assign("fields", $fields_for_email_template); if (!empty($templates["text"])) { list($templates["text"], $attachments) = _ft_extract_email_attachment_info($templates["text"], $form_id, $submission_placeholders); foreach ($attachments as $attachment_info) { if (!in_array($attachment_info, $return_info["attachments"])) { $return_info["attachments"][] = $attachment_info; } } $smarty->assign("eval_str", $templates["text"]); while (list($key, $value) = each($submission_placeholders)) { $smarty->assign($key, $value); } reset($submission_placeholders); $return_info["text_content"] = $smarty->fetch("eval.tpl"); } if (!empty($templates["html"])) { list($templates["html"], $attachments) = _ft_extract_email_attachment_info($templates["html"], $form_id, $submission_placeholders); foreach ($attachments as $attachment_info) { if (!in_array($attachment_info, $return_info["attachments"])) { $return_info["attachments"][] = $attachment_info; } } $smarty->assign("eval_str", $templates["html"]); while (list($key, $value) = each($submission_placeholders)) { // convert any newlines chars to page breaks for any answer fields. Hmm... if (strpos($key, "ANSWER_") === 0) { $value = nl2br($value); } $smarty->assign($key, $value); } $return_info["html_content"] = $smarty->fetch("eval.tpl"); } // compile the "to" / "from" / "reply-to" recipient list, based on this form submission. Virtually // everything is already stored in $email_template["recipients"], but needs to be extracted. // The notable exception is the FORM EMAIL FIELD information: that has to be constructed separately $return_info["to"] = array(); $return_info["cc"] = array(); $return_info["bcc"] = array(); foreach ($email_template["recipients"] as $recipient_info) { $recipient_type_key = $recipient_info["recipient_type"]; if ($recipient_info["recipient_type"] == "" || $recipient_info["recipient_type"] == "main") { $recipient_type_key = "to"; } if ($recipient_info["recipient_user_type"] == "form_email_field") { $header_info = _ft_get_form_email_field_headers($recipient_info["form_email_id"], $submission_info); $user_recipient = $header_info["recipient_line"]; $user_first_name = $header_info["first_name"]; $user_last_name = $header_info["last_name"]; $user_email = $header_info["email"]; $curr_recipient_info = array("recipient_line" => $user_recipient, "name" => "{$user_first_name} {$user_last_name}", "email" => $user_email); } else { $curr_recipient_info = array("recipient_line" => $recipient_info["final_recipient"], "name" => $recipient_info["final_name"], "email" => $recipient_info["final_email"]); } if (!empty($curr_recipient_info["email"])) { $return_info[$recipient_type_key][] = $curr_recipient_info; } } $return_info["from"] = array(); switch ($email_template["email_from"]) { case "admin": $return_info["from"] = array("recipient_line" => "{$admin_info["first_name"]} {$admin_info["last_name"]} <{$admin_info["email"]}>", "name" => "{$admin_info["first_name"]} {$admin_info["last_name"]}", "email" => $admin_info["email"]); break; case "client": $client_info = ft_get_account_info($email_template["email_from_account_id"]); $return_info["from"] = array("recipient_line" => "{$client_info["first_name"]} {$client_info["last_name"]} <{$client_info["email"]}>", "name" => "{$client_info["first_name"]} {$client_info["last_name"]}", "email" => $client_info["email"]); break; case "form_email_field": $header_info = _ft_get_form_email_field_headers($email_template["email_from_form_email_id"], $submission_info); $user_recipient = $header_info["recipient_line"]; $user_first_name = $header_info["first_name"]; $user_last_name = $header_info["last_name"]; $user_email = $header_info["email"]; $return_info["from"] = array("recipient_line" => $user_recipient, "name" => "{$user_first_name} {$user_last_name}", "email" => $user_email); break; case "custom": $return_info["from"] = array("recipient_line" => "{$email_template["custom_from_name"]} <{$email_template["custom_from_email"]}>", "name" => $email_template["custom_from_name"], "email" => $email_template["custom_from_email"]); break; } $return_info["reply_to"] = array(); switch ($email_template["email_reply_to"]) { case "admin": $return_info["reply_to"] = array("recipient_line" => "{$admin_info["first_name"]} {$admin_info["last_name"]} <{$admin_info["email"]}>", "name" => "{$admin_info["first_name"]} {$admin_info["last_name"]}", "email" => $admin_info["email"]); break; case "client": $client_info = ft_get_account_info($email_template["email_reply_to_account_id"]); $return_info["reply_to"] = array("recipient_line" => "{$client_info["first_name"]} {$client_info["last_name"]} <{$client_info["email"]}>", "name" => "{$client_info["first_name"]} {$client_info["last_name"]}", "email" => $client_info["email"]); break; case "form_email_field": $form_email_id = $email_template["email_reply_to_form_email_id"]; $header_info = _ft_get_form_email_field_headers($form_email_id, $submission_info); $user_recipient = $header_info["recipient_line"]; $user_first_name = $header_info["first_name"]; $user_last_name = $header_info["last_name"]; $user_email = $header_info["email"]; $return_info["reply_to"] = array("recipient_line" => $user_recipient, "name" => "{$user_first_name} {$user_last_name}", "email" => $user_email); break; case "custom": $return_info["reply_to"] = array("recipient_line" => "{$email_template["custom_reply_to_name"]} <{$email_template["custom_reply_to_email"]}>", "name" => $email_template["custom_reply_to_name"], "email" => $email_template["custom_reply_to_email"]); break; } $return_info["subject"] = ft_eval_smarty_string($email_template["subject"], $submission_placeholders); return array(true, $return_info); }
/** * Generates the placeholders for a particular form submission. This is used in the email templates, and here and there * for providing placeholder functionality to fields (like the "Edit Submission Label" textfield for a form, where they can * enter placeholders populated here). * * This returns ALL available placeholders for a form, regardless of View. * * @param integer $form_id * @param integer $submission_id * @param array $client_info a hash of information about the appropriate user (optional) * @return array a hash of placeholders and their replacement values (e.g. $arr["FORMURL"] => 17) */ function ft_get_submission_placeholders($form_id, $submission_id, $client_info = "") { global $g_root_url; $placeholders = array(); $settings = ft_get_settings(); $form_info = ft_get_form($form_id); $submission_info = ft_get_submission($form_id, $submission_id); $admin_info = ft_get_admin_info(); $file_field_type_ids = ft_get_file_field_type_ids(); $field_types = ft_get_field_types(true); // now loop through the info stored for this particular submission and for this particular field, // add the custom submission responses to the placeholder hash $form_field_params = array("include_field_type_info" => true, "include_field_settings" => true, "evaluate_dynamic_settings" => true); $form_fields = ft_get_form_fields($form_id, $form_field_params); foreach ($submission_info as $field_info) { $field_id = $field_info["field_id"]; $field_name = $field_info["field_name"]; $field_type_id = $field_info["field_type_id"]; if ($field_info["is_system_field"] == "no") { $placeholders["QUESTION_{$field_name}"] = $field_info["field_title"]; } if (in_array($field_type_id, $file_field_type_ids)) { $field_settings = ft_get_field_settings($field_id); $placeholders["FILENAME_{$field_name}"] = $field_info["content"]; $placeholders["FILEURL_{$field_name}"] = "{$field_settings["folder_url"]}/{$field_info["content"]}"; } else { $detailed_field_info = array(); foreach ($form_fields as $curr_field_info) { if ($curr_field_info["field_id"] != $field_id) { continue; } $detailed_field_info = $curr_field_info; break; } $params = array("form_id" => $form_id, "submission_id" => $submission_id, "value" => $field_info["content"], "field_info" => $detailed_field_info, "field_types" => $field_types, "settings" => $settings, "context" => "email_template"); $value = ft_generate_viewable_field($params); $placeholders["ANSWER_{$field_name}"] = $value; // for backward compatibility if ($field_name == "core__submission_date") { $placeholders["SUBMISSIONDATE"] = $value; } else { if ($field_name == "core__last_modified") { $placeholders["LASTMODIFIEDDATE"] = $value; } else { if ($field_name == "core__ip_address") { $placeholders["IPADDRESS"] = $value; } } } } } // other misc placeholders $placeholders["ADMINEMAIL"] = $admin_info["email"]; $placeholders["FORMNAME"] = $form_info["form_name"]; $placeholders["FORMURL"] = $form_info["form_url"]; $placeholders["SUBMISSIONID"] = $submission_id; $placeholders["LOGINURL"] = $g_root_url . "/index.php"; if (!empty($client_info)) { $placeholders["EMAIL"] = $client_info["email"]; $placeholders["FIRSTNAME"] = $client_info["first_name"]; $placeholders["LASTNAME"] = $client_info["last_name"]; $placeholders["COMPANYNAME"] = $client_info["company_name"]; } extract(ft_process_hook_calls("end", compact("placeholders"), array("placeholders")), EXTR_OVERWRITE); return $placeholders; }