DB
1 year ago
Request
1 year ago
UI
1 year ago
Auth.php
1 year ago
Check_Email_Admin_Capability_Giver.php
1 year ago
Check_Email_Export_Log.php
1 year ago
Check_Email_From_Handler.php
1 year ago
Check_Email_Log.php
1 year ago
Check_Email_Logger.php
1 year ago
Check_Email_Multisite.php
1 year ago
Check_Email_Review.php
1 year ago
Loadie.php
1 year ago
Check_Email_Logger.php
258 lines
| 1 | <?php namespace CheckEmail\Core; |
| 2 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 3 | /** |
| 4 | * Log's emails sent through `wp_mail`. |
| 5 | */ |
| 6 | use CheckEmail\Core\Auth; |
| 7 | class Check_Email_Logger implements Loadie { |
| 8 | |
| 9 | public function load() { |
| 10 | add_filter( 'wp_mail', array( $this, 'log_email' ) ); |
| 11 | add_action( 'wp_mail_failed', array( $this, 'on_email_failed' ) ); |
| 12 | |
| 13 | /** |
| 14 | * These actions are required for logging BuddyPress emails, since BuddyPress does |
| 15 | * not use wp_mail for sending emails. |
| 16 | */ |
| 17 | add_action( 'bp_send_email_success', array( $this, 'log_buddy_press_email' ), 10, 2 ); |
| 18 | add_action( 'bp_send_email_failure', array( $this, 'log_buddy_press_email' ), 10, 2 ); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Logs email to database. |
| 23 | */ |
| 24 | public function log_email( $original_mail_info ) { |
| 25 | if ('Test email to analyze check email' == $original_mail_info['subject'] && $original_mail_info['to'] == 'plugintest@check-email.tech') { |
| 26 | return; |
| 27 | } |
| 28 | $option = get_option( 'check-email-log-core' ); |
| 29 | $original_mail_info = apply_filters( 'check_email_wp_mail_log', $original_mail_info ); |
| 30 | |
| 31 | $mail_info = wp_parse_args( |
| 32 | $original_mail_info, |
| 33 | array( |
| 34 | 'to' => '', |
| 35 | 'subject' => '', |
| 36 | 'message' => '', |
| 37 | 'headers' => '', |
| 38 | 'cc' => '', |
| 39 | 'attachments' => array(), |
| 40 | ) |
| 41 | ); |
| 42 | |
| 43 | $ip = ''; |
| 44 | if ( isset( $_SERVER['REMOTE_ADDR'] ) ) { |
| 45 | $ip = sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ); |
| 46 | } |
| 47 | |
| 48 | $backtrace_segment = array(); |
| 49 | $backtrace_segment = $this->ck_mail_get_backtrace(); |
| 50 | if(!empty($backtrace_segment) && is_array($backtrace_segment)){ |
| 51 | $backtrace_segment = wp_json_encode($backtrace_segment); |
| 52 | }else{ |
| 53 | $backtrace_segment = null; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | $log = array( |
| 58 | 'to_email' => \CheckEmail\Util\wp_chill_check_email_stringify( $mail_info['to'] ), |
| 59 | 'subject' => esc_html($mail_info['subject']), |
| 60 | 'backtrace_segment'=> $backtrace_segment, |
| 61 | 'headers' => \CheckEmail\Util\wp_chill_check_email_stringify( $mail_info['headers'], "\n" ), |
| 62 | 'attachment_name' => \CheckEmail\Util\wp_chill_check_email_stringify( $mail_info['attachments'] ), |
| 63 | 'sent_date' => current_time( 'mysql' ), |
| 64 | 'ip_address' => $ip, |
| 65 | 'result' => 1, |
| 66 | ); |
| 67 | |
| 68 | if(empty($option) || !isset( $option['log_email_content']) || (isset( $option['log_email_content'])) && $option['log_email_content']){ |
| 69 | $log['message'] = wp_kses_post($mail_info['message']); |
| 70 | } |
| 71 | |
| 72 | if ( empty( $log['attachment_name'] ) ) { |
| 73 | $log['attachments'] = 'false'; |
| 74 | } else { |
| 75 | $log['attachments'] = 'true'; |
| 76 | } |
| 77 | if ( isset( $option['email_open_tracking'] ) && $option['email_open_tracking'] ) { |
| 78 | $timestamp = current_time('timestamp'); |
| 79 | $tracking_content = check_email_content_with_tracking($timestamp); |
| 80 | $original_mail_info['message'] = $original_mail_info['message'].$tracking_content; |
| 81 | $open_tracking_id = $timestamp; |
| 82 | $log['open_tracking_id'] = $open_tracking_id; |
| 83 | } |
| 84 | $smtp_options = get_option('check-email-smtp-options', true); |
| 85 | if (is_multisite()) { |
| 86 | $smtp_options = get_site_option( 'check-email-log-global-smtp'); |
| 87 | if ( isset($smtp_options['enable_global']) && ! empty($smtp_options['enable_global'] ) ) { |
| 88 | $option = $smtp_options; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | |
| 93 | $to_email = $log['to_email']; |
| 94 | $subject = $log['subject']; |
| 95 | $response = []; |
| 96 | |
| 97 | |
| 98 | if (isset($smtp_options['mailer']) && $smtp_options['mailer'] == 'outlook') { |
| 99 | $auth = new Auth('outlook'); |
| 100 | if ( $auth->is_clients_saved() && ! $auth->is_auth_required() ) { |
| 101 | $response = $auth->sendEmailByMailer(null ,$to_email, $subject, $log['message']); |
| 102 | |
| 103 | |
| 104 | if (isset($response['error']) && $response['error'] == 1) { |
| 105 | $log['result'] = 0; |
| 106 | $log['error_message'] = $response['message']; |
| 107 | |
| 108 | } |
| 109 | $mailer_options = $auth->get_mailer_option(); |
| 110 | if (isset($mailer_options['user_details']) && isset($mailer_options['user_details']['email'])) { |
| 111 | $log_header[] = 'From: '.$mailer_options['user_details']['email']; |
| 112 | $log_header[] = 'Content-Type: text/html; charset=UTF-8'; |
| 113 | $log['headers'] = \CheckEmail\Util\wp_chill_check_email_stringify( $log_header); |
| 114 | } |
| 115 | }else{ |
| 116 | $log['result'] = 0; |
| 117 | $log['error_message'] = esc_html__('Your microsoft 365 account not configured properly','check-email'); |
| 118 | } |
| 119 | |
| 120 | $log = apply_filters( 'check_email_email_log_before_insert', $log, $original_mail_info ); |
| 121 | |
| 122 | $check_email = wpchill_check_email(); |
| 123 | |
| 124 | $check_email->table_manager->insert_log( $log ); |
| 125 | do_action( 'check_email_log_inserted' ); |
| 126 | return false; |
| 127 | |
| 128 | } |
| 129 | |
| 130 | if (isset($option['forward_email']) && !empty($option['forward_email'])) { |
| 131 | $forward_email_info = $original_mail_info; |
| 132 | |
| 133 | if (isset($option['forward_to']) && !empty($option['forward_to'])) { |
| 134 | $to = \CheckEmail\Util\wp_chill_check_email_stringify( $option['forward_to'] ); |
| 135 | $forward_email_info['to'] = $to; |
| 136 | $forward_header[] = 'Content-Type: text/html; charset=UTF-8'; |
| 137 | |
| 138 | $forward_header = []; |
| 139 | if (isset($option['forward_cc']) && !empty($option['forward_cc'])) { |
| 140 | $copy_to = explode(',',$option['forward_cc']); |
| 141 | foreach($copy_to as $email){ |
| 142 | $forward_header[] = 'Cc: '.$email; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if (isset($option['forward_bcc']) && !empty($option['forward_bcc'])) { |
| 147 | $bcc_to = explode(',',$option['forward_bcc']); |
| 148 | foreach($bcc_to as $email){ |
| 149 | $forward_header[] = 'Bcc: '.$email; |
| 150 | } |
| 151 | } |
| 152 | $forward_email_info['headers'] = \CheckEmail\Util\wp_chill_check_email_stringify( $forward_header); |
| 153 | if ( function_exists('ck_mail_forward_mail') ) { |
| 154 | ck_mail_forward_mail($forward_email_info); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | $log = apply_filters( 'check_email_email_log_before_insert', $log, $original_mail_info ); |
| 160 | $check_email = wpchill_check_email(); |
| 161 | $check_email->table_manager->insert_log( $log ); |
| 162 | |
| 163 | do_action( 'check_email_log_inserted' ); |
| 164 | |
| 165 | return $original_mail_info; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Get the details of the method that originally triggered wp_mail |
| 170 | * |
| 171 | * @return array a single element of the debug_backtrace function |
| 172 | * @since 1.0.12 |
| 173 | */ |
| 174 | private function ck_mail_get_backtrace($functionName = 'wp_mail') |
| 175 | { |
| 176 | $backtraceSegment = null; |
| 177 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace |
| 178 | $backtrace = debug_backtrace(); |
| 179 | |
| 180 | foreach ($backtrace as $segment) { |
| 181 | if ($segment['function'] == $functionName) { |
| 182 | $backtraceSegment = $segment; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return $backtraceSegment; |
| 187 | } |
| 188 | |
| 189 | public function on_email_failed( $wp_error ) { |
| 190 | if ( ! is_wp_error( $wp_error ) ) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | $mail_error_data = $wp_error->get_error_data( 'wp_mail_failed' ); |
| 195 | $mail_error_message = $wp_error->get_error_message( 'wp_mail_failed' ); |
| 196 | apply_filters('wp_check_email_failed', $mail_error_data, $mail_error_message); |
| 197 | $this->mark_email_log_as_failed($mail_error_data, $mail_error_message ); |
| 198 | } |
| 199 | |
| 200 | public function log_buddy_press_email( $status, $bp_mail ) { |
| 201 | if ( ! class_exists( '\\BP_Email' ) ) { |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | if ( $bp_mail instanceof \BP_Email ) { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | $log = array( |
| 210 | 'to' => array_shift( $bp_mail->get_to() )->get_address(), |
| 211 | 'subject' => $bp_mail->get_subject( 'replace-tokens' ), |
| 212 | 'message' => $bp_mail->get_content( 'replace-tokens' ), |
| 213 | 'headers' => $bp_mail->get_headers( 'replace-tokens ' ), |
| 214 | ); |
| 215 | |
| 216 | $this->log_email( $log ); |
| 217 | |
| 218 | if ( ! $status ) { |
| 219 | $this->mark_email_log_as_failed( $log ); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | protected function mark_email_log_as_failed( $log, $error_message = '' ) { |
| 224 | if ( ! is_array( $log ) ) { |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | if ( ! isset( $log['to'], $log['subject'] ) ) { |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | $check_email = wpchill_check_email(); |
| 233 | |
| 234 | $log_item_id = $check_email->table_manager->fetch_log_id_by_data( $log ); |
| 235 | |
| 236 | |
| 237 | if ( empty( $log_item_id ) ) { |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | $check_email->table_manager->mark_log_as_failed( $log_item_id, $error_message ); |
| 242 | |
| 243 | $data = $check_email->table_manager->fetch_log_items_by_id( [$log_item_id] ); |
| 244 | $data = $data[0]; |
| 245 | $data_to_insert = array( |
| 246 | 'check_email_log_id' => $log_item_id, |
| 247 | 'content' => $data['message'], |
| 248 | 'initiator' => $data['backtrace_segment'], |
| 249 | 'created_at' => $data['sent_date'], |
| 250 | ); |
| 251 | if ( function_exists('ck_mail_insert_error_logs') ) { |
| 252 | ck_mail_insert_error_logs($data_to_insert); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | |
| 257 | } |
| 258 |