prefix . 'aio_email_logs';
$to = isset( $mail_data['to'] ) ? $mail_data['to'] : '';
if ( is_array( $to ) ) {
$to = implode( ', ', $to );
}
$subject = isset( $mail_data['subject'] ) ? (string) $mail_data['subject'] : '';
$message = isset( $mail_data['message'] ) ? aio_smtp_mask_sensitive_body( (string) $mail_data['message'] ) : '';
$headers = isset( $mail_data['headers'] ) ? $mail_data['headers'] : '';
if ( is_array( $headers ) ) {
$headers = implode( "\n", $headers );
}
$mailer = (string) aio_get_option( 'opt-mailer', 'smtp' );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
$wpdb->insert(
$table,
array(
'sent_at' => current_time( 'mysql' ),
'recipient' => substr( sanitize_text_field( (string) $to ), 0, 255 ),
'subject' => substr( sanitize_text_field( $subject ), 0, 255 ),
'message' => $message,
'headers' => (string) $headers,
'status' => 'sent' === $status ? 'sent' : 'failed',
'error_msg' => sanitize_text_field( (string) $error_msg ),
'mailer' => substr( sanitize_key( $mailer ), 0, 40 ),
),
array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )
);
$retention = max( 20, min( 2000, (int) aio_get_option( 'numSmtpLogRetention', 200 ) ) );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$cutoff_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$table} ORDER BY id DESC LIMIT 1 OFFSET %d", $retention ) );
if ( $cutoff_id ) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE id <= %d", (int) $cutoff_id ) );
}
}
}
if ( ! has_filter( 'wp_mail', 'aio_smtp_capture_last_mail_args' ) ) {
add_filter( 'wp_mail', 'aio_smtp_capture_last_mail_args', 999 );
}
if ( ! function_exists( 'aio_smtp_capture_last_mail_args' ) ) {
function aio_smtp_capture_last_mail_args( $args ) {
$GLOBALS['aio_last_wp_mail_args'] = $args;
return $args;
}
}
if ( ! has_action( 'wp_mail_succeeded', 'aio_smtp_on_mail_succeeded' ) ) {
add_action( 'wp_mail_succeeded', 'aio_smtp_on_mail_succeeded', 10, 1 );
}
if ( ! function_exists( 'aio_smtp_on_mail_succeeded' ) ) {
function aio_smtp_on_mail_succeeded( $mail_data ) {
aio_smtp_record_email_log( $mail_data, 'sent', '' );
}
}
if ( ! has_action( 'wp_mail_failed', 'aio_smtp_on_mail_failed' ) ) {
add_action( 'wp_mail_failed', 'aio_smtp_on_mail_failed', 10, 1 );
}
if ( ! function_exists( 'aio_smtp_on_mail_failed' ) ) {
function aio_smtp_on_mail_failed( $wp_error ) {
if ( ! is_wp_error( $wp_error ) ) {
return;
}
$error_msg = $wp_error->get_error_message();
$mail_data = $wp_error->get_error_data();
if ( ! is_array( $mail_data ) || empty( $mail_data['to'] ) ) {
$mail_data = isset( $GLOBALS['aio_last_wp_mail_args'] ) && is_array( $GLOBALS['aio_last_wp_mail_args'] ) ? $GLOBALS['aio_last_wp_mail_args'] : array();
}
aio_smtp_record_email_log( $mail_data, 'failed', $error_msg );
// Optional instant Telegram/Larksuite alert (rate-limited to 1 per 5 minutes)
if ( ! empty( aio_get_option( 'optNotifyEmailFailure' ) ) && ! get_transient( 'aio_smtp_fail_alert_lock' ) ) {
set_transient( 'aio_smtp_fail_alert_lock', 1, 5 * MINUTE_IN_SECONDS );
if ( ! class_exists( 'AIO_Messenger' ) && file_exists( AIO_MODULES_PATH . 'messenger/public-messenger.php' ) ) {
require_once AIO_MODULES_PATH . 'messenger/public-messenger.php';
}
if ( class_exists( 'AIO_Messenger' ) && method_exists( 'AIO_Messenger', 'send_instant_alert' ) ) {
$to_str = isset( $mail_data['to'] ) ? ( is_array( $mail_data['to'] ) ? implode( ', ', $mail_data['to'] ) : (string) $mail_data['to'] ) : 'Unknown';
$sub_str = isset( $mail_data['subject'] ) ? (string) $mail_data['subject'] : '';
$alert = sprintf(
/* translators: 1: recipient email, 2: email subject, 3: SMTP error message */
__( "Recipient: %1\$s\nSubject: %2\$s\nError: %3\$s", 'aio-tools' ),
esc_html( $to_str ),
esc_html( $sub_str ),
esc_html( $error_msg )
);
AIO_Messenger::send_instant_alert( __( '⚠️ SMTP Email Delivery Failed', 'aio-tools' ), $alert, false );
}
}
}
}