class-activation.php
2 years ago
class-admin-columns-manager.php
2 years ago
class-admin-menu-organizer.php
2 years ago
class-auto-publish-posts-with-missed-schedule.php
2 years ago
class-avif-upload.php
2 years ago
class-change-login-url.php
2 years ago
class-cleanup-admin-bar.php
2 years ago
class-code-snippets-manager.php
2 years ago
class-common-methods.php
2 years ago
class-content-duplication.php
2 years ago
class-content-order.php
2 years ago
class-custom-admin-footer-text.php
2 years ago
class-custom-body-class.php
2 years ago
class-custom-content-types.php
2 years ago
class-custom-css.php
2 years ago
class-custom-nav-menu-items-in-new-tab.php
2 years ago
class-deactivation.php
2 years ago
class-disable-comments.php
2 years ago
class-disable-dashboard-widgets.php
2 years ago
class-disable-feeds.php
2 years ago
class-disable-gutenberg.php
2 years ago
class-disable-rest-api.php
2 years ago
class-disable-smaller-components.php
2 years ago
class-disable-updates.php
2 years ago
class-disable-xml-rpc.php
2 years ago
class-display-system-summary.php
2 years ago
class-email-address-obfuscator.php
2 years ago
class-email-delivery.php
2 years ago
class-enhance-list-tables.php
2 years ago
class-external-permalinks.php
2 years ago
class-heartbeat-control.php
2 years ago
class-hide-admin-bar.php
2 years ago
class-hide-admin-notices.php
2 years ago
class-image-sizes-panel.php
2 years ago
class-image-upload-control.php
2 years ago
class-insert-head-body-footer-code.php
2 years ago
class-last-login-column.php
2 years ago
class-limit-login-attempts.php
2 years ago
class-login-id-type.php
2 years ago
class-login-logout-menu.php
2 years ago
class-maintenance-mode.php
2 years ago
class-manage-ads-appads-txt.php
2 years ago
class-manage-robots-txt.php
2 years ago
class-media-replacement.php
2 years ago
class-multiple-user-roles.php
2 years ago
class-obfuscate-author-slugs.php
2 years ago
class-open-external-links-in-new-tab.php
2 years ago
class-password-protection.php
2 years ago
class-redirect-after-login.php
2 years ago
class-redirect-after-logout.php
2 years ago
class-redirect-fourofour.php
2 years ago
class-revisions-control.php
2 years ago
class-search-engines-visibility.php
2 years ago
class-settings-fields-render.php
2 years ago
class-settings-sanitization.php
2 years ago
class-settings-sections-fields.php
2 years ago
class-show-custom-taxonomy-filters.php
2 years ago
class-site-identity-on-login-page.php
2 years ago
class-svg-upload.php
2 years ago
class-various-admin-ui-enhancements.php
2 years ago
class-view-admin-as-role.php
2 years ago
class-wider-admin-menu.php
2 years ago
class-email-delivery.php
350 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | |
| 5 | use WP_Error; |
| 6 | use ASENHA\EmailDelivery\Email_Log_Table; |
| 7 | /** |
| 8 | * Class for Email Delivery module |
| 9 | * |
| 10 | * @since 6.9.5 |
| 11 | */ |
| 12 | class Email_Delivery { |
| 13 | private $log_entry_id; |
| 14 | |
| 15 | /** |
| 16 | * Send emails using external SMTP service |
| 17 | * |
| 18 | * @since 4.6.0 |
| 19 | */ |
| 20 | public function deliver_email_via_smtp( $phpmailer ) { |
| 21 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 22 | $smtp_host = $options['smtp_host']; |
| 23 | $smtp_port = $options['smtp_port']; |
| 24 | $smtp_security = $options['smtp_security']; |
| 25 | $smtp_username = $options['smtp_username']; |
| 26 | $smtp_password = $options['smtp_password']; |
| 27 | $smtp_default_from_name = $options['smtp_default_from_name']; |
| 28 | $smtp_default_from_email = $options['smtp_default_from_email']; |
| 29 | $smtp_force_from = $options['smtp_force_from']; |
| 30 | $smtp_bypass_ssl_verification = $options['smtp_bypass_ssl_verification']; |
| 31 | $smtp_debug = $options['smtp_debug']; |
| 32 | // Do nothing if host or password is empty |
| 33 | // if ( empty( $smtp_host ) || empty( $smtp_password ) ) { |
| 34 | // return; |
| 35 | // } |
| 36 | // Maybe override FROM email and/or name if the sender is "WordPress <wordpress@sitedomain.com>", the default from WordPress core and not yet overridden by another plugin. |
| 37 | $from_name = $phpmailer->FromName; |
| 38 | $from_email_beginning = substr( $phpmailer->From, 0, 9 ); |
| 39 | // Get the first 9 characters of the current FROM email |
| 40 | if ( $smtp_force_from ) { |
| 41 | $phpmailer->FromName = $smtp_default_from_name; |
| 42 | $phpmailer->From = $smtp_default_from_email; |
| 43 | } else { |
| 44 | if ( 'WordPress' === $from_name && !empty( $smtp_default_from_name ) ) { |
| 45 | $phpmailer->FromName = $smtp_default_from_name; |
| 46 | } |
| 47 | if ( 'wordpress' === $from_email_beginning && !empty( $smtp_default_from_email ) ) { |
| 48 | $phpmailer->From = $smtp_default_from_email; |
| 49 | } |
| 50 | } |
| 51 | // Only attempt to send via SMTP if all the required info is present. Otherwise, use default PHP Mailer settings as set by wp_mail() |
| 52 | if ( !empty( $smtp_host ) && !empty( $smtp_port ) && !empty( $smtp_security ) && !empty( $smtp_username ) && !empty( $smtp_password ) ) { |
| 53 | // Send using SMTP |
| 54 | $phpmailer->isSMTP(); |
| 55 | // phpcs:ignore |
| 56 | // Enanble SMTP authentication |
| 57 | $phpmailer->SMTPAuth = true; |
| 58 | // phpcs:ignore |
| 59 | // Set some other defaults |
| 60 | // $phpmailer->CharSet = 'utf-8'; // phpcs:ignore |
| 61 | $phpmailer->XMailer = 'Admin and Site Enhancements v' . ASENHA_VERSION . ' - a WordPress plugin'; |
| 62 | // phpcs:ignore |
| 63 | $phpmailer->Host = $smtp_host; |
| 64 | // phpcs:ignore |
| 65 | $phpmailer->Port = $smtp_port; |
| 66 | // phpcs:ignore |
| 67 | $phpmailer->SMTPSecure = $smtp_security; |
| 68 | // phpcs:ignore |
| 69 | $phpmailer->Username = trim( $smtp_username ); |
| 70 | // phpcs:ignore |
| 71 | $phpmailer->Password = trim( $smtp_password ); |
| 72 | // phpcs:ignore |
| 73 | } |
| 74 | // If verification of SSL certificate is bypassed |
| 75 | // Reference: https://www.php.net/manual/en/context.ssl.php & https://stackoverflow.com/a/30803024 |
| 76 | if ( $smtp_bypass_ssl_verification ) { |
| 77 | $phpmailer->SMTPOptions = [ |
| 78 | 'ssl' => [ |
| 79 | 'verify_peer' => false, |
| 80 | 'verify_peer_name' => false, |
| 81 | 'allow_self_signed' => true, |
| 82 | ], |
| 83 | ]; |
| 84 | } |
| 85 | // If debug mode is enabled, send debug info (SMTP::DEBUG_CONNECTION) to WordPress debug.log file set in wp-config.php |
| 86 | // Reference: https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging |
| 87 | if ( $smtp_debug ) { |
| 88 | $phpmailer->SMTPDebug = 4; |
| 89 | //phpcs:ignore |
| 90 | $phpmailer->Debugoutput = 'error_log'; |
| 91 | //phpcs:ignore |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Send a test email and use SMTP host if defined in settings |
| 97 | * |
| 98 | * @since 5.3.0 |
| 99 | */ |
| 100 | public function send_test_email() { |
| 101 | if ( isset( $_REQUEST ) ) { |
| 102 | $content = array( |
| 103 | array( |
| 104 | 'title' => 'Hey... are you getting this?', |
| 105 | 'body' => '<p><strong>Looks like you did!</strong></p>', |
| 106 | ), |
| 107 | array( |
| 108 | 'title' => 'There\'s a message for you...', |
| 109 | 'body' => '<p><strong>Here it is:</strong></p>', |
| 110 | ), |
| 111 | array( |
| 112 | 'title' => 'Is it working?', |
| 113 | 'body' => '<p><strong>Yes, it\'s working!</strong></p>', |
| 114 | ), |
| 115 | array( |
| 116 | 'title' => 'Hope you\'re getting this...', |
| 117 | 'body' => '<p><strong>Looks like this was sent out just fine and you got it.</strong></p>', |
| 118 | ), |
| 119 | array( |
| 120 | 'title' => 'Testing delivery configuration...', |
| 121 | 'body' => '<p><strong>Everything looks good!</strong></p>', |
| 122 | ), |
| 123 | array( |
| 124 | 'title' => 'Testing email delivery', |
| 125 | 'body' => '<p><strong>Looks good!</strong></p>', |
| 126 | ), |
| 127 | array( |
| 128 | 'title' => 'Config is looking good', |
| 129 | 'body' => '<p><strong>Seems like everything has been set up properly!</strong></p>', |
| 130 | ), |
| 131 | array( |
| 132 | 'title' => 'All set up', |
| 133 | 'body' => '<p><strong>Your configuration is working properly.</strong></p>', |
| 134 | ), |
| 135 | array( |
| 136 | 'title' => 'Good to go', |
| 137 | 'body' => '<p><strong>Config is working great.</strong></p>', |
| 138 | ), |
| 139 | array( |
| 140 | 'title' => 'Good job', |
| 141 | 'body' => '<p><strong>Everything is set.</strong></p>', |
| 142 | ) |
| 143 | ); |
| 144 | $random_number = rand( 0, count( $content ) - 1 ); |
| 145 | $to = $_REQUEST['email_to']; |
| 146 | $title = $content[$random_number]['title']; |
| 147 | $body = $content[$random_number]['body'] . '<p>This message was sent from <a href="' . get_bloginfo( 'url' ) . '">' . get_bloginfo( 'url' ) . '</a> on ' . wp_date( 'F j, Y' ) . ' at ' . wp_date( 'H:i:s' ) . ' via ASE.</p>'; |
| 148 | $headers = array('Content-Type: text/html; charset=UTF-8'); |
| 149 | $success = wp_mail( |
| 150 | $to, |
| 151 | $title, |
| 152 | $body, |
| 153 | $headers |
| 154 | ); |
| 155 | if ( $success ) { |
| 156 | $response = array( |
| 157 | 'status' => 'success', |
| 158 | ); |
| 159 | } else { |
| 160 | $response = array( |
| 161 | 'status' => 'failed', |
| 162 | ); |
| 163 | } |
| 164 | echo json_encode( $response ); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Perform additional logging for data that are not properly logged via wp_mail hook |
| 170 | * |
| 171 | * @since 7.1.0 |
| 172 | */ |
| 173 | public function additional_logging__premium_onlly( $phpmailer ) { |
| 174 | // Set custom Message-ID header, e.g. // e.g. <message-3@subdomain.domain.com>> |
| 175 | // The number here is the ID of the message in the email delivery log table |
| 176 | $message_id = '<message-' . $this->log_entry_id . '@' . parse_url( get_site_url(), PHP_URL_HOST ) . '>'; |
| 177 | $phpmailer->MessageID = $message_id; |
| 178 | // Get headers |
| 179 | // Reference: https://plugins.trac.wordpress.org/browser/mailarchiver/tags/4.0.0/includes/listeners/class-corelistener.php#L216 |
| 180 | if ( method_exists( $phpmailer, 'createHeader' ) ) { |
| 181 | $headers = $phpmailer->createHeader(); |
| 182 | if ( !is_array( $headers ) ) { |
| 183 | $headers = explode( "\n", str_replace( "\r\n", "\n", $headers ) ); |
| 184 | } |
| 185 | // Remove empty elements and get sender info (name and email) |
| 186 | $sender = ''; |
| 187 | $headers_array = array(); |
| 188 | $sender = ''; |
| 189 | $reply_to = ''; |
| 190 | if ( !empty( $headers ) ) { |
| 191 | foreach ( $headers as $header ) { |
| 192 | if ( '' !== $header ) { |
| 193 | $headers_array[] = $header; |
| 194 | } |
| 195 | if ( false !== strpos( $header, 'From:' ) ) { |
| 196 | $sender_array = explode( ': ', $header ); |
| 197 | $sender = ( isset( $sender_array[1] ) ? $sender_array[1] : '' ); |
| 198 | } |
| 199 | if ( false !== strpos( $header, 'Reply-To:' ) ) { |
| 200 | $reply_to_array = explode( ': ', $header ); |
| 201 | $reply_to = ( isset( $reply_to_array[1] ) ? $reply_to_array[1] : '' ); |
| 202 | } |
| 203 | if ( false !== strpos( $header, 'Content-Type:' ) ) { |
| 204 | $content_type_array = explode( ': ', $header ); |
| 205 | $content_type_maybe_with_charset = ( isset( $content_type_array[1] ) ? $content_type_array[1] : '' ); |
| 206 | // e.g. text/html; charset=UTF-8 |
| 207 | if ( false !== strpos( $content_type_maybe_with_charset, 'charset' ) ) { |
| 208 | $content_type_array = explode( '; ', $content_type_maybe_with_charset ); |
| 209 | $content_type = ( isset( $content_type_array[0] ) ? $content_type_array[0] : '' ); |
| 210 | } else { |
| 211 | $content_type = trim( $content_type_maybe_with_charset ); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | // Add sender and headers info to the existing log entry for the mail |
| 217 | // https://developer.wordpress.org/reference/classes/wpdb/update/ |
| 218 | global $wpdb; |
| 219 | $result = $wpdb->update( |
| 220 | $wpdb->prefix . 'asenha_email_delivery', |
| 221 | // Log table name |
| 222 | array( |
| 223 | 'sender' => sanitize_text_field( str_replace( array('<', '>'), array('(', ')'), $sender ) ), |
| 224 | 'reply_to' => sanitize_text_field( str_replace( array('<', '>'), array('(', ')'), $reply_to ) ), |
| 225 | 'content_type' => sanitize_text_field( $content_type ), |
| 226 | 'headers' => serialize( $headers_array ), |
| 227 | ), |
| 228 | array( |
| 229 | 'id' => $this->log_entry_id, |
| 230 | ), |
| 231 | array( |
| 232 | '%s', |
| 233 | // string - sender |
| 234 | '%s', |
| 235 | ), |
| 236 | array('%d') |
| 237 | ); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Trigger scheduling of email delivery log clean up event |
| 243 | * |
| 244 | * @since 7.1.1 |
| 245 | */ |
| 246 | public function trigger_clear_or_schedule_log_clean_up_by_amount( $option_name ) { |
| 247 | if ( 'smtp_email_log_schedule_cleanup_by_amount' == $option_name ) { |
| 248 | $this->clear_or_schedule_log_clean_up_by_amount(); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Schedule email delivery log clean up event |
| 254 | * |
| 255 | * @link https://plugins.trac.wordpress.org/browser/lana-email-logger/tags/1.1.0/lana-email-logger.php#L750 |
| 256 | * @since 7.1.1 |
| 257 | */ |
| 258 | public function clear_or_schedule_log_clean_up_by_amount() { |
| 259 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 260 | $smtp_email_log_schedule_cleanup_by_amount = ( isset( $options['smtp_email_log_schedule_cleanup_by_amount'] ) ? $options['smtp_email_log_schedule_cleanup_by_amount'] : false ); |
| 261 | // If scheduled clean up is not enabled, let's clear the schedule |
| 262 | if ( !$smtp_email_log_schedule_cleanup_by_amount ) { |
| 263 | wp_clear_scheduled_hook( 'asenha_email_log_cleanup_by_amount' ); |
| 264 | return; |
| 265 | } |
| 266 | // If there's no next scheduled clean up event, let's schedule one |
| 267 | if ( !wp_next_scheduled( 'asenha_email_log_cleanup_by_amount' ) ) { |
| 268 | wp_schedule_event( time(), 'hourly', 'asenha_email_log_cleanup_by_amount' ); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Perform clean up of email delivery log by the amount of entries to keep |
| 274 | * |
| 275 | * @link https://plugins.trac.wordpress.org/browser/lana-email-logger/tags/1.1.0/lana-email-logger.php#L768 |
| 276 | * @since 7.1.1 |
| 277 | */ |
| 278 | public function perform_email_log_clean_up_by_amount() { |
| 279 | global $wpdb; |
| 280 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 281 | $smtp_email_log_schedule_cleanup_by_amount = ( isset( $options['smtp_email_log_schedule_cleanup_by_amount'] ) ? $options['smtp_email_log_schedule_cleanup_by_amount'] : false ); |
| 282 | $smtp_email_log_entries_amount_to_keep = ( isset( $options['smtp_email_log_entries_amount_to_keep'] ) ? $options['smtp_email_log_entries_amount_to_keep'] : 1000 ); |
| 283 | // Bail if scheduled clean up by amount is not enabled |
| 284 | if ( !$smtp_email_log_schedule_cleanup_by_amount ) { |
| 285 | return; |
| 286 | } |
| 287 | $table_name = $wpdb->prefix . 'asenha_email_delivery'; |
| 288 | $wpdb->query( "DELETE email_log_entries FROM " . $table_name . " \n AS email_log_entries JOIN ( SELECT id FROM " . $table_name . " ORDER BY id DESC LIMIT 1 OFFSET " . $smtp_email_log_entries_amount_to_keep . " ) \n AS email_log_entries_limit ON email_log_entries.id <= email_log_entries_limit.id;" ); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Resend email that failed on the first attempt and marked as such |
| 293 | * |
| 294 | * @since 7.1.3 |
| 295 | */ |
| 296 | public function resend_email() { |
| 297 | if ( isset( $_REQUEST['action'] ) && 'resend_email' == $_REQUEST['action'] && isset( $_REQUEST['message_id'] ) && !empty( $_REQUEST['message_id'] ) && is_numeric( $_REQUEST['message_id'] ) && isset( $_REQUEST['resend_to'] ) && !empty( $_REQUEST['resend_to'] ) && isset( $_REQUEST['nonce'] ) && !empty( $_REQUEST['nonce'] ) ) { |
| 298 | $db_row_id = intval( sanitize_text_field( $_REQUEST['message_id'] ) ); |
| 299 | $send_to = sanitize_text_field( $_REQUEST['resend_to'] ); |
| 300 | $nonce = sanitize_text_field( $_REQUEST['nonce'] ); |
| 301 | // $nonce_check = wp_verify_nonce( $nonce, 'email-delivery-log' . get_current_user_id() ); |
| 302 | if ( wp_verify_nonce( $nonce, 'email-delivery-log' . get_current_user_id() ) ) { |
| 303 | global $wpdb; |
| 304 | $table_name = $wpdb->prefix . 'asenha_email_delivery'; |
| 305 | $email = $wpdb->get_row( 'SELECT * FROM ' . $table_name . ' |
| 306 | WHERE id = "' . $db_row_id . '"', ARRAY_A ); |
| 307 | // Set conte type to text/html. Default is text/plain. |
| 308 | add_filter( 'wp_mail_content_type', array($this, 'return_html_email_type') ); |
| 309 | // $email_sent = true; // For testing |
| 310 | $email_sent = wp_mail( |
| 311 | $send_to, |
| 312 | $email['subject'], |
| 313 | $email['message'], |
| 314 | '', |
| 315 | $email['attachments'] |
| 316 | ); |
| 317 | // Reset content-type to avoid conflicts -- https://core.trac.wordpress.org/ticket/23578 |
| 318 | remove_filter( 'wp_mail_content_type', array($this, 'return_html_email_type') ); |
| 319 | // When invoking email resend via a GET request, we need to perform redirection |
| 320 | // wp_redirect( admin_url( 'tools.php?page=email-delivery-log' ) ); |
| 321 | if ( $email_sent ) { |
| 322 | $response = array( |
| 323 | 'resend_status' => 'successful', |
| 324 | 'send_to' => $send_to, |
| 325 | 'notice_message' => '<span class="dashicons dashicons-yes-alt"></span>' . __( 'Email was sent. This page will reload now...', 'admin-site-enhancements' ), |
| 326 | ); |
| 327 | echo json_encode( $response ); |
| 328 | } else { |
| 329 | $response = array( |
| 330 | 'resend_status' => 'failed', |
| 331 | 'send_to' => $send_to, |
| 332 | 'notice_message' => '<span class="dashicons dashicons-warning"></span>' . __( 'Something went wrong. Please check the latest log entry for details. This page will reload now...', 'admin-site-enhancements' ), |
| 333 | ); |
| 334 | echo json_encode( $response ); |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Return 'text/html' email content type for wp_mail_content_type filter hook |
| 342 | * |
| 343 | * @since 7.1.3 |
| 344 | */ |
| 345 | public function return_html_email_type() { |
| 346 | return 'text/html'; |
| 347 | } |
| 348 | |
| 349 | } |
| 350 |