PluginProbe ʕ •ᴥ•ʔ
Check & Log Email – Easy Email Testing & Mail logging / 2.0.1
Check & Log Email – Easy Email Testing & Mail logging v2.0.1
2.0.16 2.0.15 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 2.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.13.1 2.0.13.2 2.0.14 2.0.2 2.0.3 2.0.4 2.0.5 2.0.5.1 2.0.6 2.0.7 2.0.8 2.0.9 trunk 0.5.7 0.6.0 0.6.1 0.6.2 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.12.1 1.0.13 1.0.13.1 1.0.2 1.0.3
check-email / include / Core / Check_Email_Logger.php
check-email / include / Core Last commit date
DB 2 years ago Request 2 years ago UI 2 years ago Check_Email_Admin_Capability_Giver.php 2 years ago Check_Email_Export_Log.php 2 years ago Check_Email_From_Handler.php 2 years ago Check_Email_Log.php 2 years ago Check_Email_Logger.php 2 years ago Check_Email_Review.php 2 years ago Loadie.php 2 years ago
Check_Email_Logger.php
203 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 class Check_Email_Logger implements Loadie {
7
8 public function load() {
9 add_filter( 'wp_mail', array( $this, 'log_email' ) );
10 add_action( 'wp_mail_failed', array( $this, 'on_email_failed' ) );
11
12 /**
13 * These actions are required for logging BuddyPress emails, since BuddyPress does
14 * not use wp_mail for sending emails.
15 */
16 add_action( 'bp_send_email_success', array( $this, 'log_buddy_press_email' ), 10, 2 );
17 add_action( 'bp_send_email_failure', array( $this, 'log_buddy_press_email' ), 10, 2 );
18 }
19
20 /**
21 * Logs email to database.
22 */
23 public function log_email( $original_mail_info ) {
24 $option = get_option( 'check-email-log-core' );
25 $original_mail_info = apply_filters( 'check_email_wp_mail_log', $original_mail_info );
26
27 $mail_info = wp_parse_args(
28 $original_mail_info,
29 array(
30 'to' => '',
31 'subject' => '',
32 'message' => '',
33 'headers' => '',
34 'cc' => '',
35 'attachments' => array(),
36 )
37 );
38
39 $ip = '';
40 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
41 $ip = sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) );
42 }
43
44 $backtrace_segment = array();
45 $backtrace_segment = $this->ck_mail_get_backtrace();
46 if(!empty($backtrace_segment) && is_array($backtrace_segment)){
47 $backtrace_segment = wp_json_encode($backtrace_segment);
48 }else{
49 $backtrace_segment = null;
50 }
51
52
53 $log = array(
54 'to_email' => \CheckEmail\Util\wp_chill_check_email_stringify( $mail_info['to'] ),
55 'subject' => esc_html($mail_info['subject']),
56 'backtrace_segment'=> $backtrace_segment,
57 'headers' => \CheckEmail\Util\wp_chill_check_email_stringify( $mail_info['headers'], "\n" ),
58 'attachment_name' => \CheckEmail\Util\wp_chill_check_email_stringify( $mail_info['attachments'] ),
59 'sent_date' => current_time( 'mysql' ),
60 'ip_address' => $ip,
61 'result' => 1,
62 );
63
64 if(empty($option) || !isset( $option['log_email_content']) || (isset( $option['log_email_content'])) && $option['log_email_content']){
65 $log['message'] = wp_kses_post($mail_info['message']);
66 }
67
68 if ( empty( $log['attachment_name'] ) ) {
69 $log['attachments'] = 'false';
70 } else {
71 $log['attachments'] = 'true';
72 }
73
74 if (isset($option['forward_email']) && !empty($option['forward_email'])) {
75 $forward_email_info = $original_mail_info;
76
77 if (isset($option['forward_to']) && !empty($option['forward_to'])) {
78 $to = \CheckEmail\Util\wp_chill_check_email_stringify( $option['forward_to'] );
79 $forward_email_info['to'] = $to;
80 $forward_header[] = 'Content-Type: text/html; charset=UTF-8';
81
82 $forward_header = [];
83 if (isset($option['forward_cc']) && !empty($option['forward_cc'])) {
84 $copy_to = explode(',',$option['forward_cc']);
85 foreach($copy_to as $email){
86 $forward_header[] = 'Cc: '.$email;
87 }
88 }
89
90 if (isset($option['forward_bcc']) && !empty($option['forward_bcc'])) {
91 $bcc_to = explode(',',$option['forward_bcc']);
92 foreach($bcc_to as $email){
93 $forward_header[] = 'Bcc: '.$email;
94 }
95 }
96 $forward_email_info['headers'] = \CheckEmail\Util\wp_chill_check_email_stringify( $forward_header);
97 if ( function_exists('ck_mail_forward_mail') ) {
98 ck_mail_forward_mail($forward_email_info);
99 }
100 }
101 }
102 $log = apply_filters( 'check_email_email_log_before_insert', $log, $original_mail_info );
103 $check_email = wpchill_check_email();
104 $check_email->table_manager->insert_log( $log );
105
106
107
108 do_action( 'check_email_log_inserted' );
109
110 return $original_mail_info;
111 }
112
113 /**
114 * Get the details of the method that originally triggered wp_mail
115 *
116 * @return array a single element of the debug_backtrace function
117 * @since 1.0.12
118 */
119 private function ck_mail_get_backtrace($functionName = 'wp_mail')
120 {
121 $backtraceSegment = null;
122 $backtrace = debug_backtrace();
123
124 foreach ($backtrace as $segment) {
125 if ($segment['function'] == $functionName) {
126 $backtraceSegment = $segment;
127 }
128 }
129
130 return $backtraceSegment;
131 }
132
133 public function on_email_failed( $wp_error ) {
134 if ( ! is_wp_error( $wp_error ) ) {
135 return;
136 }
137
138 $mail_error_data = $wp_error->get_error_data( 'wp_mail_failed' );
139 $mail_error_message = $wp_error->get_error_message( 'wp_mail_failed' );
140
141 $this->mark_email_log_as_failed(apply_filters('wp_check_email_failed', $mail_error_data, $mail_error_message) );
142 }
143
144 public function log_buddy_press_email( $status, $bp_mail ) {
145 if ( ! class_exists( '\\BP_Email' ) ) {
146 return;
147 }
148
149 if ( $bp_mail instanceof \BP_Email ) {
150 return;
151 }
152
153 $log = array(
154 'to' => array_shift( $bp_mail->get_to() )->get_address(),
155 'subject' => $bp_mail->get_subject( 'replace-tokens' ),
156 'message' => $bp_mail->get_content( 'replace-tokens' ),
157 'headers' => $bp_mail->get_headers( 'replace-tokens ' ),
158 );
159
160 $this->log_email( $log );
161
162 if ( ! $status ) {
163 $this->mark_email_log_as_failed( $log );
164 }
165 }
166
167 protected function mark_email_log_as_failed( $log, $error_message = '' ) {
168 if ( ! is_array( $log ) ) {
169 return;
170 }
171
172 if ( ! isset( $log['to'], $log['subject'] ) ) {
173 return;
174 }
175
176 $check_email = wpchill_check_email();
177
178 $log_item_id = $check_email->table_manager->fetch_log_id_by_data( $log );
179
180
181 if ( empty( $log_item_id ) ) {
182 return;
183 }
184
185 $check_email->table_manager->mark_log_as_failed( $log_item_id, $error_message );
186
187 $data = $check_email->table_manager->fetch_log_items_by_id( [$log_item_id] );
188 $data = $data[0];
189 $data_to_insert = array(
190 'check_email_log_id' => $log_item_id,
191 'content' => $data['message'],
192 'initiator' => $data['backtrace_segment'],
193 'created_at' => $data['sent_date'],
194 );
195
196 if ( function_exists('ck_mail_insert_error_logs') ) {
197 ck_mail_insert_error_logs($data_to_insert);
198 }
199 }
200
201
202 }
203