PluginProbe ʕ •ᴥ•ʔ
Check & Log Email – Easy Email Testing & Mail logging / 1.0.13
Check & Log Email – Easy Email Testing & Mail logging v1.0.13
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
186 lines
1 <?php namespace CheckEmail\Core;
2
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 // if ( is_array( $option ) && array_key_exists( 'enable_logs', $option ) && 'true' === strtolower( $option['enable_logs'] ) ) {
26 $original_mail_info = apply_filters( 'check_email_wp_mail_log', $original_mail_info );
27
28 $mail_info = wp_parse_args(
29 $original_mail_info,
30 array(
31 'to' => '',
32 'subject' => '',
33 'message' => '',
34 'headers' => '',
35 'cc' => '',
36 'attachments' => array(),
37 )
38 );
39
40 $ip = '';
41 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
42 $ip = sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) );
43 }
44
45 $backtrace_segment = array();
46 $backtrace_segment = $this->ck_mail_get_backtrace();
47 if(!empty($backtrace_segment) && is_array($backtrace_segment)){
48 $backtrace_segment = json_encode($backtrace_segment);
49 }else{
50 $backtrace_segment = null;
51 }
52
53
54 $log = array(
55 'to_email' => \CheckEmail\Util\wp_chill_check_email_stringify( $mail_info['to'] ),
56 'subject' => esc_html($mail_info['subject']),
57 'message' => wp_kses_post($mail_info['message']),
58 'backtrace_segment'=> $backtrace_segment,
59 'headers' => \CheckEmail\Util\wp_chill_check_email_stringify( $mail_info['headers'], "\n" ),
60 'attachment_name' => \CheckEmail\Util\wp_chill_check_email_stringify( $mail_info['attachments'] ),
61 'sent_date' => current_time( 'mysql' ),
62 'ip_address' => $ip,
63 'result' => 1,
64 );
65
66 if ( empty( $log['attachment_name'] ) ) {
67 $log['attachments'] = 'false';
68 } else {
69 $log['attachments'] = 'true';
70 }
71
72 if (isset($option['forward_email']) && !empty($option['forward_email'])) {
73 $forward_email_info = $original_mail_info;
74
75 if (isset($option['forward_to']) && !empty($option['forward_to'])) {
76 $to = \CheckEmail\Util\wp_chill_check_email_stringify( $option['forward_to'] );
77 $forward_email_info['to'] = $to;
78 $forward_header[] = 'Content-Type: text/html; charset=UTF-8';
79
80 $forward_header = [];
81 if (isset($option['forward_cc']) && !empty($option['forward_cc'])) {
82 $copy_to = explode(',',$option['forward_cc']);
83 foreach($copy_to as $email){
84 $forward_header[] = 'Cc: '.$email;
85 }
86 }
87
88 if (isset($option['forward_bcc']) && !empty($option['forward_bcc'])) {
89 $bcc_to = explode(',',$option['forward_bcc']);
90 foreach($bcc_to as $email){
91 $forward_header[] = 'Bcc: '.$email;
92 }
93 }
94 $forward_email_info['headers'] = \CheckEmail\Util\wp_chill_check_email_stringify( $forward_header);
95 check_mail_forward_mail($forward_email_info);
96 }
97 }
98 $log = apply_filters( 'check_email_email_log_before_insert', $log, $original_mail_info );
99 $check_email = wpchill_check_email();
100 $check_email->table_manager->insert_log( $log );
101
102
103
104 do_action( 'check_email_log_inserted' );
105 // }
106
107 return $original_mail_info;
108 }
109
110 /**
111 * Get the details of the method that originally triggered wp_mail
112 *
113 * @return array a single element of the debug_backtrace function
114 * @since 1.0.12
115 */
116 private function ck_mail_get_backtrace($functionName = 'wp_mail')
117 {
118 $backtraceSegment = null;
119 $backtrace = debug_backtrace();
120
121 foreach ($backtrace as $segment) {
122 if ($segment['function'] == $functionName) {
123 $backtraceSegment = $segment;
124 }
125 }
126
127 return $backtraceSegment;
128 }
129
130 public function on_email_failed( $wp_error ) {
131 if ( ! is_wp_error( $wp_error ) ) {
132 return;
133 }
134
135 $mail_error_data = $wp_error->get_error_data( 'wp_mail_failed' );
136 $mail_error_message = $wp_error->get_error_message( 'wp_mail_failed' );
137
138 $this->mark_email_log_as_failed( $mail_error_data, $mail_error_message );
139 }
140
141 public function log_buddy_press_email( $status, $bp_mail ) {
142 if ( ! class_exists( '\\BP_Email' ) ) {
143 return;
144 }
145
146 if ( $bp_mail instanceof \BP_Email ) {
147 return;
148 }
149
150 $log = array(
151 'to' => array_shift( $bp_mail->get_to() )->get_address(),
152 'subject' => $bp_mail->get_subject( 'replace-tokens' ),
153 'message' => $bp_mail->get_content( 'replace-tokens' ),
154 'headers' => $bp_mail->get_headers( 'replace-tokens ' ),
155 );
156
157 $this->log_email( $log );
158
159 if ( ! $status ) {
160 $this->mark_email_log_as_failed( $log );
161 }
162 }
163
164 protected function mark_email_log_as_failed( $log, $error_message = '' ) {
165 if ( ! is_array( $log ) ) {
166 return;
167 }
168
169 if ( ! isset( $log['to'], $log['subject'] ) ) {
170 return;
171 }
172
173 $check_email = wpchill_check_email();
174
175 $log_item_id = $check_email->table_manager->fetch_log_id_by_data( $log );
176
177 if ( empty( $log_item_id ) ) {
178 return;
179 }
180
181 $check_email->table_manager->mark_log_as_failed( $log_item_id, $error_message );
182 }
183
184
185 }
186