PluginProbe ʕ •ᴥ•ʔ
Check & Log Email – Easy Email Testing & Mail logging / 2.0.3
Check & Log Email – Easy Email Testing & Mail logging v2.0.3
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 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
254 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 $option = get_option( 'check-email-log-core' );
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 = wp_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 'backtrace_segment'=> $backtrace_segment,
58 'headers' => \CheckEmail\Util\wp_chill_check_email_stringify( $mail_info['headers'], "\n" ),
59 'attachment_name' => \CheckEmail\Util\wp_chill_check_email_stringify( $mail_info['attachments'] ),
60 'sent_date' => current_time( 'mysql' ),
61 'ip_address' => $ip,
62 'result' => 1,
63 );
64
65 if(empty($option) || !isset( $option['log_email_content']) || (isset( $option['log_email_content'])) && $option['log_email_content']){
66 $log['message'] = wp_kses_post($mail_info['message']);
67 }
68
69 if ( empty( $log['attachment_name'] ) ) {
70 $log['attachments'] = 'false';
71 } else {
72 $log['attachments'] = 'true';
73 }
74 if ( isset( $option['email_open_tracking'] ) && $option['email_open_tracking'] ) {
75 $timestamp = current_time('timestamp');
76 $tracking_content = check_email_content_with_tracking($timestamp);
77 $original_mail_info['message'] = $original_mail_info['message'].$tracking_content;
78 $open_tracking_id = $timestamp;
79 $log['open_tracking_id'] = $open_tracking_id;
80 }
81 $smtp_options = get_option('check-email-smtp-options', true);
82 if (is_multisite()) {
83 $smtp_options = get_site_option( 'check-email-log-global-smtp');
84 if ( isset($smtp_options['enable_global']) && ! empty($smtp_options['enable_global'] ) ) {
85 $option = $smtp_options;
86 }
87 }
88
89
90 $to_email = $log['to_email'];
91 $subject = $log['subject'];
92 $response = [];
93
94
95 if (isset($smtp_options['mailer']) && $smtp_options['mailer'] == 'outlook') {
96 $auth = new Auth('outlook');
97 if ( $auth->is_clients_saved() && ! $auth->is_auth_required() ) {
98 $response = $auth->sendEmailByMailer(null ,$to_email, $subject, $log['message']);
99
100
101 if (isset($response['error']) && $response['error'] == 1) {
102 $log['result'] = 0;
103 $log['error_message'] = $response['message'];
104
105 }
106 $mailer_options = $auth->get_mailer_option();
107 if (isset($mailer_options['user_details']) && isset($mailer_options['user_details']['email'])) {
108 $log_header[] = 'From: '.$mailer_options['user_details']['email'];
109 $log_header[] = 'Content-Type: text/html; charset=UTF-8';
110 $log['headers'] = \CheckEmail\Util\wp_chill_check_email_stringify( $log_header);
111 }
112 }else{
113 $log['result'] = 0;
114 $log['error_message'] = esc_html__('Your microsoft 365 account not configured properly','check-email');
115 }
116
117 $log = apply_filters( 'check_email_email_log_before_insert', $log, $original_mail_info );
118
119 $check_email = wpchill_check_email();
120
121 $check_email->table_manager->insert_log( $log );
122 do_action( 'check_email_log_inserted' );
123 return false;
124
125 }
126
127 if (isset($option['forward_email']) && !empty($option['forward_email'])) {
128 $forward_email_info = $original_mail_info;
129
130 if (isset($option['forward_to']) && !empty($option['forward_to'])) {
131 $to = \CheckEmail\Util\wp_chill_check_email_stringify( $option['forward_to'] );
132 $forward_email_info['to'] = $to;
133 $forward_header[] = 'Content-Type: text/html; charset=UTF-8';
134
135 $forward_header = [];
136 if (isset($option['forward_cc']) && !empty($option['forward_cc'])) {
137 $copy_to = explode(',',$option['forward_cc']);
138 foreach($copy_to as $email){
139 $forward_header[] = 'Cc: '.$email;
140 }
141 }
142
143 if (isset($option['forward_bcc']) && !empty($option['forward_bcc'])) {
144 $bcc_to = explode(',',$option['forward_bcc']);
145 foreach($bcc_to as $email){
146 $forward_header[] = 'Bcc: '.$email;
147 }
148 }
149 $forward_email_info['headers'] = \CheckEmail\Util\wp_chill_check_email_stringify( $forward_header);
150 if ( function_exists('ck_mail_forward_mail') ) {
151 ck_mail_forward_mail($forward_email_info);
152 }
153 }
154 }
155
156 $log = apply_filters( 'check_email_email_log_before_insert', $log, $original_mail_info );
157 $check_email = wpchill_check_email();
158 $check_email->table_manager->insert_log( $log );
159
160 do_action( 'check_email_log_inserted' );
161
162 return $original_mail_info;
163 }
164
165 /**
166 * Get the details of the method that originally triggered wp_mail
167 *
168 * @return array a single element of the debug_backtrace function
169 * @since 1.0.12
170 */
171 private function ck_mail_get_backtrace($functionName = 'wp_mail')
172 {
173 $backtraceSegment = null;
174 $backtrace = debug_backtrace();
175
176 foreach ($backtrace as $segment) {
177 if ($segment['function'] == $functionName) {
178 $backtraceSegment = $segment;
179 }
180 }
181
182 return $backtraceSegment;
183 }
184
185 public function on_email_failed( $wp_error ) {
186 if ( ! is_wp_error( $wp_error ) ) {
187 return;
188 }
189
190 $mail_error_data = $wp_error->get_error_data( 'wp_mail_failed' );
191 $mail_error_message = $wp_error->get_error_message( 'wp_mail_failed' );
192
193 $this->mark_email_log_as_failed(apply_filters('wp_check_email_failed', $mail_error_data, $mail_error_message) );
194 }
195
196 public function log_buddy_press_email( $status, $bp_mail ) {
197 if ( ! class_exists( '\\BP_Email' ) ) {
198 return;
199 }
200
201 if ( $bp_mail instanceof \BP_Email ) {
202 return;
203 }
204
205 $log = array(
206 'to' => array_shift( $bp_mail->get_to() )->get_address(),
207 'subject' => $bp_mail->get_subject( 'replace-tokens' ),
208 'message' => $bp_mail->get_content( 'replace-tokens' ),
209 'headers' => $bp_mail->get_headers( 'replace-tokens ' ),
210 );
211
212 $this->log_email( $log );
213
214 if ( ! $status ) {
215 $this->mark_email_log_as_failed( $log );
216 }
217 }
218
219 protected function mark_email_log_as_failed( $log, $error_message = '' ) {
220 if ( ! is_array( $log ) ) {
221 return;
222 }
223
224 if ( ! isset( $log['to'], $log['subject'] ) ) {
225 return;
226 }
227
228 $check_email = wpchill_check_email();
229
230 $log_item_id = $check_email->table_manager->fetch_log_id_by_data( $log );
231
232
233 if ( empty( $log_item_id ) ) {
234 return;
235 }
236
237 $check_email->table_manager->mark_log_as_failed( $log_item_id, $error_message );
238
239 $data = $check_email->table_manager->fetch_log_items_by_id( [$log_item_id] );
240 $data = $data[0];
241 $data_to_insert = array(
242 'check_email_log_id' => $log_item_id,
243 'content' => $data['message'],
244 'initiator' => $data['backtrace_segment'],
245 'created_at' => $data['sent_date'],
246 );
247 if ( function_exists('ck_mail_insert_error_logs') ) {
248 ck_mail_insert_error_logs($data_to_insert);
249 }
250 }
251
252
253 }
254