# supportcandy/2.3.1/includes/actions/en_submit_note.php

SupportCandy – AI Customer Support Ticket System &amp; Live Chatbot Agent, version 2.3.1. 172 lines.

- Page: https://pluginprobe.com/plugins/supportcandy/2.3.1/code/includes/actions/en_submit_note.php
- Raw: https://pluginprobe.com/plugins/supportcandy/2.3.1/raw/includes/actions/en_submit_note.php
- Modified: 2022-07-21T08:20:56+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/supportcandy/2.3.1/code/includes/actions/en_submit_note.php#L10-L20`.

```php
<?php
if ( ! defined( 'ABSPATH' ) ) {
  exit; // Exit if accessed directly
}

global $wpscfunction, $current_user,$wpdb;

$from_name     = get_option('wpsc_en_from_name','');
$from_email    = get_option('wpsc_en_from_email','');
$reply_to      = get_option('wpsc_en_reply_to','');
$ignore_emails = get_option('wpsc_en_ignore_emails','');

$wpsc_email_sending_method = get_option('wpsc_email_sending_method') ; 

if ( !$from_name || !$from_email ) {
  return;
}

$email_templates = get_terms([
  'taxonomy'   => 'wpsc_en',
  'hide_empty' => false,
  'orderby'    => 'ID',
  'order'      => 'ASC',
  'meta_query' => array(
    'relation' => 'AND',
    array(
      'key'     => 'type',
      'value'   => 'private_note',
      'compare' => '='
    ),
  ),
]);


$email_subject = get_option('wpsc_email_notification_subject');
$email_body    = get_option('wpsc_email_notification_body');

foreach ($email_templates as $email) :

  $conditions = get_term_meta($email->term_id,'conditions',true);
  if( $wpscfunction->check_ticket_conditions($conditions,$ticket_id) ) :

    $subject          = $wpscfunction->replace_macro($email_subject['email_subject_' . $email->term_id], $ticket_id);
    $subject          = '['.get_option('wpsc_ticket_alice','').$ticket_id.'] '.stripslashes($subject);
    $body             = $wpscfunction->replace_macro(stripslashes($email_body['email_body_' . $email->term_id]), $ticket_id);
    $recipients       = get_term_meta($email->term_id,'recipients',true);
    $extra_recipients = get_term_meta($email->term_id,'extra_recipients',true);

    $email_addresses = array();
    foreach ($recipients as $recipient) {
      if(is_numeric($recipient)){
        $agents = get_terms([
          'taxonomy'   => 'wpsc_agents',
          'hide_empty' => false,
          'meta_query' => array(
            'relation' => 'AND',
            array(
              'key'     => 'role',
              'value'   => $recipient,
              'compare' => '='
            ),
          ),
        ]);
        foreach ($agents as $agent) {
          $user_id = get_term_meta($agent->term_id,'user_id',true);
          if($user_id){
            $user = get_user_by('id',$user_id);
            $email_addresses[] = $user->user_email;
          }
        }
      } else {
        switch ($recipient) {
          case 'customer':
            $customer_email    = $wpscfunction->get_ticket_fields($ticket_id,'customer_email');
            $email_addresses[] = $customer_email;
            break;
          case 'assigned_agent':
            $email_addresses = array_merge($email_addresses,$wpscfunction->get_assigned_agent_emails($ticket_id));
            break;
          case 'previously_assigned_agent':
            $email_addresses = array_merge($email_addresses,$wpscfunction->get_previously_assigned_agents($ticket_id));
            break;
          case 'extra_ticket_users' : 
            $extra_users     = $wpscfunction->get_ticket_meta($ticket_id,'extra_ticket_users');
            $email_addresses = array_merge($email_addresses, $extra_users);
            break;
        }
      }
    }
    if(isset($extra_recipients[0]) && $extra_recipients[0] ) {
      $email_addresses = array_merge($email_addresses,$extra_recipients);
    }

    $bcc = isset($_POST['reply_bcc']) ? trim($_POST['reply_bcc']) : '';
    if ($bcc) {
        $bcc = array_filter(array_map('sanitize_email', explode(',', $bcc)));
        $email_addresses = array_merge($email_addresses, $bcc);
    }

    $last_note = $wpscfunction->get_last_note($ticket_id);
    if(in_array('current_user', $recipients)){
      $email_addresses[] = $last_note['user_email'];
    }else{
      $email_addresses = array_diff($email_addresses,array($last_note['user_email']));
    }
    $email_addresses = apply_filters('wpsc_en_submit_note_email_addresses',$email_addresses,$email,$thread_id,$ticket_id);
    $email_addresses = array_values($email_addresses);

    if ($ignore_emails) {
      $ignore_emails = array_map('trim', $ignore_emails);
      $email_addresses = array_map('trim', $email_addresses);
      foreach ($ignore_emails as $ignore_email) {
        foreach($email_addresses as $index => $email){
          if(fnmatch($ignore_email, $email)){
            unset($email_addresses[$index]);
          }
        }
      }
    }
    $email_addresses = array_unique($email_addresses);
    $email_addresses = array_values($email_addresses);

    $to =  isset($email_addresses[0])? $email_addresses[0] : '';
    if($to){
      unset($email_addresses[0]);
    } else {
      continue; // no email address found to send. So go to next foreach iteration.
    }

    $from_email = apply_filters('wpsc_add_note_from_email_headers',$from_email,$ticket_id);
    $reply_to   = apply_filters('wpsc_add_note_ticket_replyto_headers',$reply_to,$ticket_id);

    $bcc = implode(',',$email_addresses);

    $to = apply_filters('wpsc_submit_note_to_email',$to,$email_addresses,$ticket_id);
    $bcc = apply_filters('wpsc_submit_note_bcc_emails',$bcc,$email_addresses,$ticket_id);

    $args = array(
      'ticket_id'     => $ticket_id,
      'from_email'    => $from_email,
      'reply_to'      => $reply_to,
      'email_subject' => $subject,
      'email_body'    => $body,
      'to_email'      => $to,
      'bcc_email'     => $bcc,
      'date_created'  => date("Y-m-d H:i:s"),
      'mail_status'   => 0,
      'email_type'    => 'submit_note',

    );
    if($wpsc_email_sending_method){
      $wpdb->insert( $wpdb->prefix . 'wpsc_email_notification',$args);
      
    }else{
      $headers  = "From: {$from_name} <{$from_email}>\r\n";
      $headers .= "Reply-To: {$reply_to}\r\n";
      $email_addresses = explode(',',$bcc);
      foreach ($email_addresses as $email_address) {
        if(strlen($email_address)) $headers .= "BCC: {$email_address}\r\n";
      }
  
      $headers .= "Content-Type: text/html; charset=utf-8\r\n";
  
      wp_mail($to, $subject, $body, $headers);
    }

    do_action('wpsc_after_submit_note_mail',$ticket_id,$args);
    
  endif;

endforeach;

```
