This commit is contained in:
Javier Casares 2026-02-12 13:55:56 +00:00
commit 3daee2df53
6 changed files with 116 additions and 18 deletions

View file

@ -208,9 +208,18 @@ class Plugin {
* @return WP_Error|null
*/
private function deliver_via_amazon_ses( array $mail_context, array $settings, array $parsed_headers, string $access_key, string $secret_key, string $region ): ?WP_Error {
// Prepare debug context for error reporting.
$debug_context = $this->get_ses_debug_context( $access_key, $region );
$phpmailer = $this->prepare_mailer_for_amazon_ses( $mail_context, $settings, $parsed_headers );
if ( $phpmailer instanceof WP_Error ) {
// Add SES debug context to existing error.
$error_data = $phpmailer->get_error_data();
if ( is_array( $error_data ) ) {
$error_data['ses_debug'] = $debug_context;
$phpmailer->add_data( $error_data );
}
return $phpmailer;
}
@ -231,6 +240,9 @@ class Plugin {
'headers' => $mail_context['headers'],
'attachments' => $mail_context['attachments'],
'phpmailer_exception_code' => $exception->getCode(),
'phpmailer_exception_file' => $exception->getFile(),
'phpmailer_exception_line' => $exception->getLine(),
'ses_debug' => $debug_context,
)
);
}
@ -266,12 +278,15 @@ class Plugin {
\esc_html( (string) $message )
),
array(
'to' => $mail_context['to'],
'subject' => $mail_context['subject'],
'message' => $mail_context['message'],
'headers' => $mail_context['headers'],
'attachments' => $mail_context['attachments'],
'ses_error_code' => $exception->getAwsErrorCode(),
'to' => $mail_context['to'],
'subject' => $mail_context['subject'],
'message' => $mail_context['message'],
'headers' => $mail_context['headers'],
'attachments' => $mail_context['attachments'],
'ses_error_code' => $exception->getAwsErrorCode(),
'ses_request_id' => $exception->getAwsRequestId(),
'ses_http_status_code' => $exception->getStatusCode(),
'ses_debug' => $debug_context,
)
);
} catch ( Throwable $exception ) {
@ -288,6 +303,7 @@ class Plugin {
'message' => $mail_context['message'],
'headers' => $mail_context['headers'],
'attachments' => $mail_context['attachments'],
'ses_debug' => $debug_context,
)
);
}
@ -313,6 +329,12 @@ class Plugin {
$content_type = is_string( $parsed_headers['content_type'] ?? '' ) && '' !== $parsed_headers['content_type'] ? $parsed_headers['content_type'] : ( is_string( $default_content_type ) ? $default_content_type : 'text/plain' );
$charset = is_string( $parsed_headers['charset'] ?? '' ) && '' !== $parsed_headers['charset'] ? $parsed_headers['charset'] : ( is_string( $default_charset ) ? $default_charset : 'utf-8' );
// Ensure charset is never empty, null, or the string "null".
$charset = \trim( (string) $charset );
if ( '' === $charset || 'null' === \strtolower( $charset ) ) {
$charset = 'utf-8';
}
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$phpmailer->CharSet = $charset;
$phpmailer->Encoding = '8bit';
@ -633,7 +655,11 @@ class Plugin {
list( $param, $param_value ) = explode( '=', $part, 2 );
if ( 'charset' === strtolower( trim( $param ) ) ) {
$parsed['charset'] = trim( $param_value, " \n\r\0\x0B\"'" );
$charset_value = trim( $param_value, " \n\r\0\x0B\"'" );
// Only set charset if it has a valid non-empty value and is not "null".
if ( '' !== $charset_value && 'null' !== strtolower( $charset_value ) ) {
$parsed['charset'] = $charset_value;
}
}
}
} else {
@ -757,6 +783,41 @@ class Plugin {
return $client;
}
/**
* Gathers Amazon SES debug context information for error reporting.
*
* @since 2.1.0
*
* @param string $access_key AWS access key ID.
* @param string $region AWS region identifier.
*
* @return array<string, mixed> Debug context with region, masked access key, SDK version, and endpoint.
*/
private function get_ses_debug_context( string $access_key, string $region ): array {
// Mask the access key for security (show first 4 and last 4 characters).
$masked_key = '';
$key_length = \strlen( $access_key );
if ( $key_length > 8 ) {
$masked_key = \substr( $access_key, 0, 4 ) . \str_repeat( '*', $key_length - 8 ) . \substr( $access_key, -4 );
} elseif ( $key_length > 0 ) {
$masked_key = \str_repeat( '*', $key_length );
}
// Get AWS SDK version.
$sdk_version = \defined( 'Aws\Sdk::VERSION' ) ? \Aws\Sdk::VERSION : 'unknown';
// Build SES endpoint URL.
$endpoint = \sprintf( 'https://email.%s.amazonaws.com', $region );
return array(
'region' => $region,
'access_key' => $masked_key,
'sdk_version' => $sdk_version,
'endpoint' => $endpoint,
'timestamp' => \gmdate( 'Y-m-d H:i:s' ) . ' UTC',
);
}
/**
* Marks the Amazon SES integration as active.
*