function mail_incident($id_inc, $id_usuario, $nota, $timeused, $mode, $public = 1)
{
    global $config;
    include_once $config["homedir"] . '/include/functions_user.php';
    clean_cache_db();
    $row = get_db_row("tincidencia", "id_incidencia", $id_inc);
    $group_name = get_db_sql("SELECT nombre FROM tgrupo WHERE id_grupo = " . $row["id_grupo"]);
    $email_from = get_db_sql("SELECT email_from FROM tgrupo WHERE id_grupo = " . $row["id_grupo"]);
    $titulo = $row["titulo"];
    $description = wordwrap(ascii_output($row["descripcion"]), 70, "\n");
    $prioridad = get_priority_name($row["prioridad"]);
    $nota = wordwrap($nota, 75, "\n");
    $estado = render_status($row["estado"]);
    $resolution = render_resolution($row["resolution"]);
    $create_timestamp = $row["inicio"];
    $update_timestamp = $row["actualizacion"];
    $usuario = $row["id_usuario"];
    $creator = $row["id_creator"];
    $email_copy = $row["email_copy"];
    // Send email for owner and creator of this incident
    $email_creator = get_user_email($creator);
    $company_creator = get_user_company($creator, true);
    if (empty($company_creator)) {
        $company_creator = "";
    } else {
        $company_creator = " (" . reset($company_creator) . ")";
    }
    $email_owner = get_user_email($usuario);
    $company_owner = get_user_company($usuario, true);
    if (empty($company_owner)) {
        $company_owner = "";
    } else {
        $company_owner = " (" . reset($company_owner) . ")";
    }
    //check if user is disabled
    $owner_disabled = user_is_disabled($usuario);
    $creator_disabled = user_is_disabled($creator);
    $ticket_score = '';
    if ($row["estado"] == 7 and $row['score'] == 0) {
        $ticket_score = $config["base_url"] . "/index.php?sec=incidents&sec2=operation/incidents/incident_detail&id={$id_inc}";
        //$ticket_score =  '<a href="'.$config["base_url"].'"/index.php?sec=incidents&sec2=operation/incidents/incident_detail&id="'.$id_inc.'">'."Click hear to scoring".'</a>';
    }
    $MACROS["_sitename_"] = $config["sitename"];
    $MACROS["_fullname_"] = dame_nombre_real($usuario);
    $MACROS["_username_"] = $usuario;
    $MACROS["_incident_id_"] = $id_inc;
    $MACROS["_incident_title_"] = $titulo;
    $MACROS["_creation_timestamp_"] = $create_timestamp;
    $MACROS["_update_timestamp_"] = $update_timestamp;
    $MACROS["_group_"] = $group_name;
    $MACROS["_author_"] = dame_nombre_real($creator) . $company_creator;
    $MACROS["_owner_"] = dame_nombre_real($usuario) . $company_owner;
    $MACROS["_priority_"] = $prioridad;
    $MACROS["_status_"] = $estado;
    $MACROS["_resolution_"] = $resolution;
    $MACROS["_time_used_"] = $timeused;
    $MACROS["_incident_main_text_"] = $description;
    $access_dir = empty($config['access_public']) ? $config["base_url"] : $config['public_url'];
    $MACROS["_access_url_"] = $access_dir . "/index.php?sec=incidents&sec2=operation/incidents/incident_dashboard_detail&id={$id_inc}";
    $MACROS["_incident_epilog_"] = $row["epilog"];
    $MACROS["_incident_closed_by_"] = $row["closed_by"];
    // Resolve code for its name
    switch ($mode) {
        case 10:
            // Add Workunit
            //$subject = "[".$config["sitename"]."] Incident #$id_inc ($titulo) has a new workunit from [$id_usuario]";
            $company_wu = get_user_company($id_usuario, true);
            if (empty($company_wu)) {
                $company_wu = "";
            } else {
                $company_wu = " (" . reset($company_wu) . ")";
            }
            $MACROS["_wu_user_"] = dame_nombre_real($id_usuario) . $company_wu;
            $MACROS["_wu_text_"] = $nota;
            $text = template_process($config["homedir"] . "/include/mailtemplates/incident_update_wu.tpl", $MACROS);
            $subject = template_process($config["homedir"] . "/include/mailtemplates/incident_subject_new_wu.tpl", $MACROS);
            break;
        case 0:
            // Incident update
            $text = template_process($config["homedir"] . "/include/mailtemplates/incident_update.tpl", $MACROS);
            $subject = template_process($config["homedir"] . "/include/mailtemplates/incident_subject_update.tpl", $MACROS);
            break;
        case 1:
            // Incident creation
            $text = template_process($config["homedir"] . "/include/mailtemplates/incident_create.tpl", $MACROS);
            $subject = template_process($config["homedir"] . "/include/mailtemplates/incident_subject_create.tpl", $MACROS);
            break;
        case 2:
            // New attach
            $text = template_process($config["homedir"] . "/include/mailtemplates/incident_update.tpl", $MACROS);
            $subject = template_process($config["homedir"] . "/include/mailtemplates/incident_subject_attach.tpl", $MACROS);
            break;
        case 3:
            // Incident deleted
            $text = template_process($config["homedir"] . "/include/mailtemplates/incident_update.tpl", $MACROS);
            $subject = template_process($config["homedir"] . "/include/mailtemplates/incident_subject_delete.tpl", $MACROS);
            break;
        case 5:
            // Incident closed
            $MACROS["_ticket_score_"] = $ticket_score;
            $text = template_process($config["homedir"] . "/include/mailtemplates/incident_close.tpl", $MACROS);
            $subject = template_process($config["homedir"] . "/include/mailtemplates/incident_subject_close.tpl", $MACROS);
            break;
    }
    // Create the TicketID for have a secure reference to incident hidden
    // in the message. Will be used for POP automatic processing to add workunits
    // to the incident automatically.
    $msg_code = "TicketID#{$id_inc}";
    $msg_code .= "/" . substr(md5($id_inc . $config["smtp_pass"] . $row["id_usuario"]), 0, 5);
    $msg_code .= "/" . $row["id_usuario"];
    if ((!$config['email_ticket_on_creation_and_closing'] || $mode == 5 || $mode == 1) && !$owner_disabled) {
        integria_sendmail($email_owner, $subject, $text, false, $msg_code, $email_from, 0, "", "X-Integria: no_process");
    }
    // Send a copy to each address in "email_copy"
    //if ($email_copy != ""){
    if ($email_copy != "" && !$config['email_ticket_on_creation_and_closing']) {
        $emails = explode(",", $email_copy);
        foreach ($emails as $em) {
            integria_sendmail($em, $subject, $text, false, "", $email_from, 0, "", "X-Integria: no_process");
        }
    }
    // Incident owner
    if ($email_owner != $email_creator and !$config['email_ticket_on_creation_and_closing'] and !$creator_disabled) {
        $msg_code = "TicketID#{$id_inc}";
        $msg_code .= "/" . substr(md5($id_inc . $config["smtp_pass"] . $row["id_creator"]), 0, 5);
        $msg_code .= "/" . $row["id_creator"];
        integria_sendmail($email_creator, $subject, $text, false, $msg_code, $email_from, "", 0, "", "X-Integria: no_process");
    }
    //if ($public == 1){
    if ($public == 1 and !$config['email_ticket_on_creation_and_closing']) {
        // Send email for all users with workunits for this incident
        $sql1 = "SELECT DISTINCT(tusuario.direccion), tusuario.id_usuario FROM tusuario, tworkunit, tworkunit_incident WHERE tworkunit_incident.id_incident = {$id_inc} AND tworkunit_incident.id_workunit = tworkunit.id AND tworkunit.id_user = tusuario.id_usuario AND tusuario.disabled=0";
        if ($result = mysql_query($sql1)) {
            while ($row = mysql_fetch_array($result)) {
                if ($row[0] != $email_owner and $row[0] != $email_creator) {
                    $msg_code = "TicketID#{$id_inc}";
                    $msg_code .= "/" . substr(md5($id_inc . $config["smtp_pass"] . $row[1]), 0, 5);
                    $msg_code .= "/" . $row[1];
                    integria_sendmail($row[0], $subject, $text, false, $msg_code, $email_from, "", 0, "", "X-Integria: no_process");
                }
            }
        }
        // Send email to incident reporters associated to this incident
        if ($config['incident_reporter'] == 1) {
            $contacts = get_incident_contact_reporters($id_inc, true);
            if ($contats) {
                foreach ($contacts as $contact) {
                    $contact_email = get_db_sql("SELECT email FROM tcompany_contact WHERE fullname = '{$contact}'");
                    integria_sendmail($contact_email, $subject, $text, false, $msg_code, $email_from, "", 0, "", "X-Integria: no_process");
                }
            }
        }
    }
}
print_table ($table);

echo '<div id="detail_info" title="'.__("Contact details").'"></div>';

$table = new StdClass();
$table->width = "100%";
$table->data = array ();
$table->head = array ();
$table->head[0] = __('Company');
$table->head[1] = __('Contact');
$table->head[2] = __('Details');
$table->head[3] = __('Edit');

if ($config['incident_reporter'] == 1){
	$contacts = get_incident_contact_reporters ($id_incident); 

	foreach ($contacts as $contact) {
		$data = array ();
		
		$data[0] = get_db_value  ('name', 'tcompany', 'id', $contact['id_company']);
		$data[1] = $contact['fullname'];
		$details = '';
		if ($contact['phone'] != '')
			$details .= '<strong>'.__('Phone number').'</strong>: '.$contact['phone'].'<br />';
		if ($contact['mobile'] != '')
			$details .= '<strong>'.__('Mobile phone').'</strong>: '.$contact['mobile'].'<br />';
		if ($contact['position'] != '')
			$details .= '<strong>'.__('Position').'</strong>: '.$contact['position'].'<br />';
		$data[2] = print_help_tip ($details, true, 'tip_view');
		$data[3] = '<a href="index.php?sec=inventory&sec2=operation/contacts/contact_detail&id='.$contact['id'].'">'.
function mail_incident($id_inc, $id_usuario, $nota, $timeused, $mode, $public = 1)
{
    global $config;
    include_once $config["homedir"] . '/include/functions_user.php';
    include_once $config["homedir"] . '/include/functions_db.php';
    include_once $config["homedir"] . '/include/functions_db.mysql.php';
    clean_cache_db();
    $row = get_db_row("tincidencia", "id_incidencia", $id_inc);
    $group_name = get_db_sql("SELECT nombre FROM tgrupo WHERE id_grupo = " . $row["id_grupo"]);
    $email_group = get_db_sql("SELECT email_group FROM tgrupo WHERE id_grupo = " . $row["id_grupo"]);
    $forced_email = get_db_sql("SELECT forced_email FROM tgrupo WHERE id_grupo = " . $row["id_grupo"]);
    $user_defect_group = get_db_sql("SELECT id_user_default FROM tgrupo WHERE id_grupo = " . $row["id_grupo"]);
    $email_from = get_db_sql("SELECT email_from FROM tgrupo WHERE id_grupo = " . $row["id_grupo"]);
    $type_ticket = get_db_sql("SELECT name FROM tincident_type WHERE id = " . $row["id_incident_type"]);
    $titulo = $row["titulo"];
    $description = $row["descripcion"];
    $prioridad = get_priority_name($row["prioridad"]);
    $estado = render_status($row["estado"]);
    $resolution = render_resolution($row["resolution"]);
    $create_timestamp = $row["inicio"];
    $update_timestamp = $row["actualizacion"];
    $usuario = $row["id_usuario"];
    $creator = $row["id_creator"];
    $email_copy = $row["email_copy"];
    // Send email for owner and creator of this incident
    $email_creator = get_user_email($creator);
    $company_creator = get_user_company($creator, true);
    if (empty($company_creator)) {
        $company_creator = "";
    } else {
        $company_creator = " (" . reset($company_creator) . ")";
    }
    $email_owner = get_user_email($usuario);
    $company_owner = get_user_company($usuario, true);
    if (empty($company_owner)) {
        $company_owner = "";
    } else {
        $company_owner = " (" . reset($company_owner) . ")";
    }
    //check if user is disabled
    $owner_disabled = user_is_disabled($usuario);
    $creator_disabled = user_is_disabled($creator);
    $ticket_score = '';
    if ($row["estado"] == 7 and $row['score'] == 0) {
        $ticket_score = $config["base_url"] . "/index.php?sec=incidents&sec2=operation/incidents/incident_detail&id={$id_inc}";
        //$ticket_score =  '<a href="'.$config["base_url"].'"/index.php?sec=incidents&sec2=operation/incidents/incident_detail&id="'.$id_inc.'">'."Click hear to scoring".'</a>';
    }
    //name for fields
    $sql_name_custom = 'select ttf.label from tincident_field_data tfd, tincident_type_field ttf 
						where tfd.id_incident_field = ttf.id and tfd.id_incident=' . $id_inc . ';';
    $name_custom = get_db_all_rows_sql($sql_name_custom);
    if ($name_custom === false) {
        $name_custom = array();
    }
    foreach ($name_custom as $p) {
        //value according to the name of the custom fields
        $sql_value_custom = "select tfd.data from tincident_field_data tfd, tincident_type_field ttf \n\t\t\t\t\t\t\t where tfd.id_incident_field = ttf.id and tfd.id_incident=" . $id_inc . " and ttf.label='" . $p['label'] . "';";
        $value_custom = get_db_sql($sql_value_custom);
        $MACROS['_' . $p['label'] . '_'] = $value_custom;
    }
    $MACROS["_sitename_"] = $config["sitename"];
    $MACROS["_fullname_"] = dame_nombre_real($usuario);
    $MACROS["_username_"] = $usuario;
    $MACROS["_incident_id_"] = $id_inc;
    $MACROS["_incident_title_"] = $titulo;
    $MACROS["_creation_timestamp_"] = $create_timestamp;
    $MACROS["_update_timestamp_"] = $update_timestamp;
    $MACROS["_group_"] = $group_name;
    $MACROS["_author_"] = dame_nombre_real($creator) . $company_creator;
    $MACROS["_owner_"] = dame_nombre_real($usuario) . $company_owner;
    $MACROS["_priority_"] = $prioridad;
    $MACROS["_status_"] = $estado;
    $MACROS["_resolution_"] = $resolution;
    $MACROS["_time_used_"] = $timeused;
    $MACROS["_incident_main_text_"] = $description;
    $access_dir = empty($config['access_public']) ? $config["base_url"] : $config['public_url'];
    $MACROS["_access_url_"] = $access_dir . "/index.php?sec=incidents&sec2=operation/incidents/incident_dashboard_detail&id={$id_inc}";
    $MACROS["_incident_epilog_"] = $row["epilog"];
    $MACROS["_incident_closed_by_"] = $row["closed_by"];
    $MACROS["_type_tickets_"] = $type_ticket;
    // Resolve code for its name
    switch ($mode) {
        case 10:
            // Add Workunit
            //$subject = "[".$config["sitename"]."] Incident #$id_inc ($titulo) has a new workunit from [$id_usuario]";
            $company_wu = get_user_company($id_usuario, true);
            if (empty($company_wu)) {
                $company_wu = "";
            } else {
                $company_wu = " (" . reset($company_wu) . ")";
            }
            $MACROS["_wu_user_"] = dame_nombre_real($id_usuario) . $company_wu;
            $MACROS["_wu_text_"] = $nota;
            // Do not pass to safe_output. $nota is already HTML Safe in this point
            $temp_group = get_db_value('id_grupo', 'tgrupo', 'nombre', $group_name);
            $sql_body = "SELECT name FROM temail_template WHERE template_action = 7 AND id_group =" . $temp_group . ";";
            $sql_subject = "SELECT name FROM temail_template WHERE template_action = 6 AND id_group =" . $temp_group . ";";
            $templa_body = get_db_sql($sql_body);
            $templa_subj = get_db_sql($sql_subject);
            if (!$templa_body) {
                $text = template_process($config["homedir"] . "/include/mailtemplates/incident_update_wu.tpl", $MACROS);
            } else {
                $text = template_process($config["homedir"] . "/include/mailtemplates/" . $templa_body . ".tpl", $MACROS);
            }
            if (!$templa_subj) {
                $subject = template_process($config["homedir"] . "/include/mailtemplates/incident_subject_new_wu.tpl", $MACROS);
            } else {
                $subject = template_process($config["homedir"] . "/include/mailtemplates/" . $templa_subj . ".tpl", $MACROS);
            }
            break;
        case 0:
            // Incident update
            $attachments = "";
            $images = "";
            $temp_group = get_db_value('id_grupo', 'tgrupo', 'nombre', $group_name);
            $sql_body = "SELECT name FROM temail_template WHERE template_action = 9 AND id_group =" . $temp_group . ";";
            $sql_subject = "SELECT name FROM temail_template WHERE template_action = 8 AND id_group =" . $temp_group . ";";
            $templa_body = get_db_sql($sql_body);
            $templa_subj = get_db_sql($sql_subject);
            if (!$templa_body) {
                $text .= template_process($config["homedir"] . "/include/mailtemplates/incident_update.tpl", $MACROS);
            } else {
                $text .= template_process($config["homedir"] . "/include/mailtemplates/" . $templa_body . ".tpl", $MACROS);
            }
            if (!$templa_subj) {
                $subject = template_process($config["homedir"] . "/include/mailtemplates/incident_subject_update.tpl", $MACROS);
            } else {
                $subject = template_process($config["homedir"] . "/include/mailtemplates/" . $templa_subj . ".tpl", $MACROS);
            }
            $attached_files = get_db_all_rows_sql("SELECT * FROM tattachment WHERE id_incidencia=" . $id_inc);
            if ($attached_files === false) {
                $attached_files = array();
            }
            $i = 0;
            $j = 0;
            foreach ($attached_files as $file) {
                $file_name = $file['id_attachment'] . '_' . $file['filename'];
                $access_public = get_db_value("value", "tconfig", "token", "access_public");
                $access_protocol = get_db_value("value", "tconfig", "token", "access_protocol");
                if ($access_protocol) {
                    $protocol = "https://";
                } else {
                    $protocol = "http://";
                }
                $ext = strtolower(substr($file['filename'], -3, 3));
                if ($ext == "jpg" || $ext == "png" || $ext == "gif") {
                    $path_file = $protocol . $access_public . '/' . $config['baseurl'] . '/attachment/' . $file_name;
                    if ($j == 0) {
                        $images = $path_file;
                    } else {
                        $images .= ',' . $path_file;
                    }
                    $j++;
                } else {
                    $path_file = $config['homedir'] . "attachment/" . $file_name;
                    if ($i == 0) {
                        $attachments = $path_file;
                    } else {
                        $attachments .= ',' . $path_file;
                    }
                    $i++;
                }
            }
            break;
        case 1:
            // Incident creation
            $attachments = "";
            $images = "";
            $temp_group = get_db_value('id_grupo', 'tgrupo', 'nombre', $group_name);
            $sql_body = "SELECT name FROM temail_template WHERE template_action = 0 AND id_group =" . $temp_group . ";";
            $sql_subject = "SELECT name FROM temail_template WHERE template_action = 1 AND id_group =" . $temp_group . ";";
            $templa_body = get_db_sql($sql_body);
            $templa_subj = get_db_sql($sql_subject);
            if (!$templa_body) {
                $text .= template_process($config["homedir"] . "/include/mailtemplates/incident_create.tpl", $MACROS);
            } else {
                $text .= template_process($config["homedir"] . "/include/mailtemplates/" . $templa_body . ".tpl", $MACROS);
            }
            if (!$templa_subj) {
                $subject = template_process($config["homedir"] . "/include/mailtemplates/incident_subject_create.tpl", $MACROS);
            } else {
                $subject = template_process($config["homedir"] . "/include/mailtemplates/" . $templa_subj . ".tpl", $MACROS);
            }
            $attached_files = get_db_all_rows_sql("SELECT * FROM tattachment WHERE id_incidencia=" . $id_inc);
            if ($attached_files === false) {
                $attached_files = array();
            }
            $i = 0;
            $j = 0;
            foreach ($attached_files as $file) {
                $file_name = $file['id_attachment'] . '_' . $file['filename'];
                $access_public = get_db_value("value", "tconfig", "token", "access_public");
                $access_protocol = get_db_value("value", "tconfig", "token", "access_protocol");
                if ($access_protocol) {
                    $protocol = "https://";
                } else {
                    $protocol = "http://";
                }
                $ext = strtolower(substr($file['filename'], -3, 3));
                if ($ext == "jpg" || $ext == "png" || $ext == "gif") {
                    $path_file = $protocol . $access_public . '/' . $config['baseurl'] . '/attachment/' . $file_name;
                    if ($j == 0) {
                        $images = $path_file;
                    } else {
                        $images .= ',' . $path_file;
                    }
                    $j++;
                } else {
                    $path_file = $config['homedir'] . "attachment/" . $file_name;
                    if ($i == 0) {
                        $attachments = $path_file;
                    } else {
                        $attachments .= ',' . $path_file;
                    }
                    $i++;
                }
            }
            break;
        case 2:
            // New attach
            $attachments = "";
            $images = "";
            $temp_group = get_db_value('id_grupo', 'tgrupo', 'nombre', $group_name);
            $sql_body = "SELECT name FROM temail_template WHERE template_action = 9 AND id_group =" . $temp_group . ";";
            $sql_subject = "SELECT name FROM temail_template WHERE template_action = 4 AND id_group =" . $temp_group . ";";
            $templa_body = get_db_sql($sql_body);
            $templa_subj = get_db_sql($sql_subject);
            if (!$templa_body) {
                $text = template_process($config["homedir"] . "/include/mailtemplates/incident_update.tpl", $MACROS);
            } else {
                $text = template_process($config["homedir"] . "/include/mailtemplates/" . $templa_body . ".tpl", $MACROS);
            }
            if (!$templa_subj) {
                $subject = template_process($config["homedir"] . "/include/mailtemplates/incident_subject_attach.tpl", $MACROS);
            } else {
                $subject = template_process($config["homedir"] . "/include/mailtemplates/" . $templa_subj . ".tpl", $MACROS);
            }
            $attached_files = get_db_all_rows_sql("SELECT * FROM tattachment WHERE id_incidencia=" . $id_inc);
            if ($attached_files === false) {
                $attached_files = array();
            }
            $i = 0;
            $j = 0;
            foreach ($attached_files as $file) {
                $file_name = $file['id_attachment'] . '_' . $file['filename'];
                $access_public = get_db_value("value", "tconfig", "token", "access_public");
                $access_protocol = get_db_value("value", "tconfig", "token", "access_protocol");
                if ($access_protocol) {
                    $protocol = "https://";
                } else {
                    $protocol = "http://";
                }
                $ext = strtolower(substr($file['filename'], -3, 3));
                if ($ext == "jpg" || $ext == "png" || $ext == "gif") {
                    $path_file = $protocol . $access_public . '/' . $config['baseurl'] . '/attachment/' . $file_name;
                    if ($j == 0) {
                        $images = $path_file;
                    } else {
                        $images .= ',' . $path_file;
                    }
                    $j++;
                } else {
                    $path_file = $config['homedir'] . "attachment/" . $file_name;
                    if ($i == 0) {
                        $attachments = $path_file;
                    } else {
                        $attachments .= ',' . $path_file;
                    }
                    $i++;
                }
            }
            break;
        case 3:
            // Incident deleted
            $temp_group = get_db_value('id_grupo', 'tgrupo', 'nombre', $group_name);
            $sql_body = "SELECT name FROM temail_template WHERE template_action = 9 AND id_group =" . $temp_group . ";";
            $sql_subject = "SELECT name FROM temail_template WHERE template_action = 5 AND id_group =" . $temp_group . ";";
            $templa_body = get_db_sql($sql_body);
            $templa_subj = get_db_sql($sql_subject);
            if (!$templa_body) {
                $text = template_process($config["homedir"] . "/include/mailtemplates/incident_update.tpl", $MACROS);
            } else {
                $text = template_process($config["homedir"] . "/include/mailtemplates/" . $templa_body . ".tpl", $MACROS);
            }
            if (!$templa_subj) {
                $subject = template_process($config["homedir"] . "/include/mailtemplates/incident_subject_delete.tpl", $MACROS);
            } else {
                $subject = template_process($config["homedir"] . "/include/mailtemplates/" . $templa_subj . ".tpl", $MACROS);
            }
            break;
        case 5:
            // Incident closed
            $MACROS["_ticket_score_"] = $ticket_score;
            $temp_group = get_db_value('id_grupo', 'tgrupo', 'nombre', $group_name);
            $sql_body = "SELECT name FROM temail_template WHERE template_action = 2 AND id_group =" . $temp_group . ";";
            $sql_subject = "SELECT name FROM temail_template WHERE template_action = 3 AND id_group =" . $temp_group . ";";
            $templa_body = get_db_sql($sql_body);
            $templa_subj = get_db_sql($sql_subject);
            if (!$templa_body) {
                $text = template_process($config["homedir"] . "/include/mailtemplates/incident_close.tpl", $MACROS);
            } else {
                $text = template_process($config["homedir"] . "/include/mailtemplates/" . $templa_body . ".tpl", $MACROS);
            }
            if (!$templa_subj) {
                $subject = template_process($config["homedir"] . "/include/mailtemplates/incident_subject_close.tpl", $MACROS);
            } else {
                $subject = template_process($config["homedir"] . "/include/mailtemplates/" . $templa_subj . ".tpl", $MACROS);
            }
            break;
    }
    // Create the TicketID for have a secure reference to incident hidden
    // in the message. Will be used for POP automatic processing to add workunits
    // to the incident automatically.
    if ($public != 7) {
        //owner
        $msg_code = "TicketID#{$id_inc}";
        $msg_code .= "/" . substr(md5($id_inc . $config["smtp_pass"] . $row["id_usuario"]), 0, 5);
        $msg_code .= "/" . $row["id_usuario"];
        integria_sendmail($email_owner, $subject, $text, $attachments, $msg_code, $email_from, 0, "", "X-Integria: no_process", $images);
        //creator
        if ($email_owner != $email_creator) {
            $msg_code = "TicketID#{$id_inc}";
            $msg_code .= "/" . substr(md5($id_inc . $config["smtp_pass"] . $row["id_creator"]), 0, 5);
            $msg_code .= "/" . $row["id_creator"];
            integria_sendmail($email_creator, $subject, $text, $attachments, $msg_code, $email_from, 0, "", "X-Integria: no_process", $images);
        }
        // Send emails to the people in the group added
        if ($forced_email != 0) {
            $email_default = get_user_email($user_defect_group);
            integria_sendmail($email_default, $subject, $text, $attachments, $msg_code, $email_from, 0, "", "X-Integria: no_process", $images);
            if ($email_group) {
                $email_g = explode(',', $email_group);
                foreach ($email_g as $k) {
                    integria_sendmail($k, $subject, $text, $attachments, $msg_code, $email_from, 0, "", "X-Integria: no_process", $images);
                }
            }
        }
    }
    if ($public == 7) {
        // Send a copy to each address in "email_copy"
        if ($email_copy != "") {
            $emails = explode(",", $email_copy);
            foreach ($emails as $em) {
                integria_sendmail($em, $subject, $text, $attachments, "", $email_from, 0, "", "X-Integria: no_process", $images);
            }
        }
    }
    if ($public == 1) {
        // Send email for all users with workunits for this incident
        $sql1 = "SELECT DISTINCT(tusuario.direccion), tusuario.id_usuario FROM tusuario, tworkunit, tworkunit_incident WHERE tworkunit_incident.id_incident = {$id_inc} AND tworkunit_incident.id_workunit = tworkunit.id AND tworkunit.id_user = tusuario.id_usuario AND tusuario.disabled=0";
        if ($result = mysql_query($sql1)) {
            while ($row = mysql_fetch_array($result)) {
                if ($row[0] != $email_owner and $row[0] != $email_creator) {
                    $msg_code = "TicketID#{$id_inc}";
                    $msg_code .= "/" . substr(md5($id_inc . $config["smtp_pass"] . $row[1]), 0, 5);
                    $msg_code .= "/" . $row[1];
                    integria_sendmail($row[0], $subject, $text, false, $msg_code, $email_from, 0, "", "X-Integria: no_process", $images);
                }
            }
        }
        // Send email to incident reporters associated to this incident
        if ($config['incident_reporter'] == 1) {
            $contacts = get_incident_contact_reporters($id_inc, true);
            if ($contats) {
                foreach ($contacts as $contact) {
                    $contact_email = get_db_sql("SELECT email FROM tcompany_contact WHERE fullname = '{$contact}'");
                    integria_sendmail($contact_email, $subject, $text, false, $msg_code, $email_from, 0, "", "X-Integria: no_process", $images);
                }
            }
        }
    }
}