Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add internationalization (i18n) support for PHPMailer error messages #7937

Open
wants to merge 17 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/wp-includes/class-wp-phpmailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* PHPMailer extensions for WordPress
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
*
* @package WordPress
* @since 6.7.1
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
*/

/**
* PHPMailer child class
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
*
* Overrides the internationalization method in order to use WordPress' instead.
*
* @since 6.7.1
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
*/
class WP_PHPMailer extends PHPMailer\PHPMailer\PHPMailer {
/**
* Public constructor
*
* @param bool $exceptions Whether to throw exceptions for errors.
*/
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
public function __construct( $exceptions = false ) {
parent::__construct( $exceptions );
$this->SetLanguage();
}

/**
* Defines the error messages using WordPress' internationalization method.
*
* @return bool Always returns true in order to mimic PHPMailer's setLanguage() behavior
*/
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
public function SetLanguage( $langcode = 'en', $lang_path = '' ) {
$error_strings = array(
'authenticate' => __( 'SMTP Error: Could not authenticate.' ),
'buggy_php' => __(
'Your version of PHP is affected by a bug that may result in corrupted messages. To fix it, switch to sending using SMTP, disable the mail.add_x_header option in your php.ini, switch to MacOS or Linux, or upgrade your PHP to version 7.0.17+ or 7.1.3+.'
),
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
'connect_host' => __( 'SMTP Error: Could not connect to SMTP host.' ),
'data_not_accepted' => __( 'SMTP Error: data not accepted.' ),
'empty_message' => __( 'Message body empty' ),
'encoding' => __( 'Unknown encoding: ' ),
'execute' => __( 'Could not execute: ' ),
'extension_missing' => __( 'Extension missing: ' ),
'file_access' => __( 'Could not access file: ' ),
'file_open' => __( 'File Error: Could not open file: ' ),
'from_failed' => __( 'The following From address failed: ' ),
'instantiate' => __( 'Could not instantiate mail function.' ),
'invalid_address' => __( 'Invalid address: ' ),
'invalid_header' => __( 'Invalid header name or value' ),
'invalid_hostentry' => __( 'Invalid hostentry: ' ),
'invalid_host' => __( 'Invalid host: ' ),
'mailer_not_supported' => __( ' mailer is not supported.' ),
'provide_address' => __( 'You must provide at least one recipient email address.' ),
'recipients_failed' => __( 'SMTP Error: The following recipients failed: ' ),
'signing' => __( 'Signing Error: ' ),
'smtp_code' => __( 'SMTP code: ' ),
'smtp_code_ex' => __( 'Additional SMTP info: ' ),
'smtp_connect_failed' => __( 'SMTP connect() failed.' ),
'smtp_detail' => __( 'Detail: ' ),
'smtp_error' => __( 'SMTP server error: ' ),
'variable_set' => __( 'Cannot set or reset variable: ' ),
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
);
$this->language = $error_strings;
return true;
}
}
3 changes: 2 additions & 1 deletion src/wp-includes/pluggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';
require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php';
require_once ABSPATH . WPINC . '/PHPMailer/Exception.php';
$phpmailer = new PHPMailer\PHPMailer\PHPMailer( true );
require_once ABSPATH . WPINC . '/class-wp-phpmailer.php';
$phpmailer = new WP_PHPMailer( true );

$phpmailer::$validator = static function ( $email ) {
return (bool) is_email( $email );
Expand Down
18 changes: 18 additions & 0 deletions tests/phpunit/tests/mail/test-phpmailer-translations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

class Test_PHPMailer_Translations extends WP_UnitTestCase {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a test that actually loads a translation file for those new strings and then triggers a PHPMailer error, so that we can verify the error messages are actually translated?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if you'd need help with those btw

/**
* @ticket 23311
*/
public function test_wp_phpmailer_error_message_keys_match() {
require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';
require_once ABSPATH . WPINC . '/class-wp-phpmailer.php';
swissspidy marked this conversation as resolved.
Show resolved Hide resolved

$phpmailer = new PHPMailer\PHPMailer\PHPMailer();
$phpmailer->SetLanguage();

$wp_phpmailer = new WP_PHPMailer();

$this->assertTrue( array_keys( $phpmailer->GetTranslations() ) === array_keys( $wp_phpmailer->GetTranslations() ) );
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading