WP_PHPMailer

WordPress extension of PHPMailer providing internationalized error messages.

Since: 6.8.0
Source: wp-includes/class-wp-phpmailer.php
Extends: PHPMailerPHPMailerPHPMailer

Purpose

WP_PHPMailer overrides PHPMailer’s language handling to use WordPress’s internationalization system (__()). This ensures error messages are translated according to the site’s locale rather than PHPMailer’s built-in language files.

Constructor

php
public function __construct( $exceptions = false )
ParameterTypeDefaultDescription
$exceptionsboolfalseWhether to throw exceptions for errors

Calls parent constructor and immediately sets language strings.

Methods

setLanguage()

php
public static function setLanguage( $langcode = 'en', $lang_path = '' )

Overrides PHPMailer’s setLanguage() to populate static::$language with WordPress-translated strings.

ParameterTypeDefaultDescription
$langcodestring'en'Unused. Ignored by WordPress implementation
$lang_pathstring''Unused. Ignored by WordPress implementation

Returns: true (always)

Translated Error Messages

KeyEnglish Message
authenticateSMTP Error: Could not authenticate.
buggy_phpYour version of PHP is affected by a bug…
connect_hostSMTP Error: Could not connect to SMTP host.
data_not_acceptedSMTP Error: Data not accepted.
empty_messageMessage body empty
encodingUnknown encoding:
executeCould not execute:
extension_missingExtension missing:
file_accessCould not access file:
file_openFile Error: Could not open file:
from_failedThe following From address failed:
instantiateCould not instantiate mail function.
invalid_addressInvalid address:
invalid_headerInvalid header name or value
invalid_hostentryInvalid host entry:
invalid_hostInvalid host:
mailer_not_supportedmailer is not supported.
provide_addressYou must provide at least one recipient email address.
recipients_failedSMTP Error: The following recipients failed:
signingSigning Error:
smtp_codeSMTP code:
smtp_code_exAdditional SMTP info:
smtp_connect_failedSMTP connect() failed.
smtp_detailDetail:
smtp_errorSMTP server error:
variable_setCannot set or reset variable:
no_smtputf8Server does not support SMTPUTF8 needed to send to Unicode addresses
imap_recommendedUsing simplified address parser is not recommended…
deprecated_argumentArgument $useimap is deprecated

Usage

WordPress automatically uses WP_PHPMailer when wp_mail() is called:

php
// In wp_mail() - wp-includes/pluggable.php
if ( ! ( $phpmailer instanceof PHPMailerPHPMailerPHPMailer ) ) {
    require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';
    require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php';
    require_once ABSPATH . WPINC . '/PHPMailer/Exception.php';
    require_once ABSPATH . WPINC . '/class-wp-phpmailer.php';
    $phpmailer = new WP_PHPMailer( true );

    $phpmailer::$validator = static function ( $email ) {
        return (bool) is_email( $email );
    };
}

Note: WordPress sets $exceptions = true and replaces PHPMailer’s email validator with WordPress’s is_email() function.

Inherited Properties

Key properties from PHPMailerPHPMailerPHPMailer:

PropertyTypeDefaultDescription
$Mailerstring'mail'Send method: mail, sendmail, smtp
$Hoststring'localhost'SMTP host(s)
$Portint25SMTP port
$SMTPAuthboolfalseEnable SMTP authentication
$Usernamestring''SMTP username
$Passwordstring''SMTP password
$SMTPSecurestring''Encryption: ”, ‘tls’, ‘ssl’
$SMTPAutoTLSbooltrueAuto-enable TLS if supported
$Fromstring''From email address
$FromNamestring''From name
$Subjectstring''Email subject
$Bodystring''Email body (HTML or plain)
$AltBodystring''Plain-text alternative body
$CharSetstring'iso-8859-1'Character set
$ContentTypestring'text/plain'MIME content type
$Encodingstring'8bit'Message encoding
$Timeoutint300SMTP timeout in seconds
$SMTPDebugint0Debug output level (0-4)

Inherited Methods

MethodDescription
send()Send the email
isMail()Set mailer to use PHP mail()
isSMTP()Set mailer to use SMTP
isSendmail()Set mailer to use sendmail
setFrom($address, $name)Set From address
addAddress($address, $name)Add To recipient
addCC($address, $name)Add CC recipient
addBCC($address, $name)Add BCC recipient
addReplyTo($address, $name)Add Reply-To address
addAttachment($path, $name)Add file attachment
addEmbeddedImage(...)Add inline embedded image
addCustomHeader($header)Add custom header
isHTML($isHtml)Set HTML mode
clearAllRecipients()Clear all recipients
clearAttachments()Clear attachments
clearCustomHeaders()Clear custom headers
clearReplyTos()Clear Reply-To addresses

Constants

From PHPMailerPHPMailerPHPMailer:

php
// Character sets
const CHARSET_ASCII = 'us-ascii';
const CHARSET_ISO88591 = 'iso-8859-1';
const CHARSET_UTF8 = 'utf-8';

// Content types
const CONTENT_TYPE_PLAINTEXT = 'text/plain';
const CONTENT_TYPE_TEXT_HTML = 'text/html';
const CONTENT_TYPE_MULTIPART_ALTERNATIVE = 'multipart/alternative';
const CONTENT_TYPE_MULTIPART_MIXED = 'multipart/mixed';
const CONTENT_TYPE_MULTIPART_RELATED = 'multipart/related';

// Encodings
const ENCODING_7BIT = '7bit';
const ENCODING_8BIT = '8bit';
const ENCODING_BASE64 = 'base64';
const ENCODING_QUOTED_PRINTABLE = 'quoted-printable';

// Encryption
const ENCRYPTION_STARTTLS = 'tls';
const ENCRYPTION_SMTPS = 'ssl';