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