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