Skip to content
WordPressPublic

About

No description, website, or topics provided.

Resources

Code of conduct

Security policy

Stars

31 stars

Watchers

3 watching

Forks

Latest commit

Β 

History

47 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SMTP

SMTP

Required PHP Version Required WordPress Version WordPress Tested Up To GPL-2.0-or-later License

Plugin Version WordPress.org Rating WordPress Plugin Downloads WordPress Plugin Active Installs WordPress Playground Demo

PHP Unit Testing PHP Linting codecov

SMTP plugin for WordPress. View on WordPress.org β†’

Send WordPress emails through an SMTP server, with an extensible provider API and a simple email log.

This is the technical documentation for developers. The user-facing description lives in readme.txt.

  • Requires WordPress 6.3+ and PHP 7.4+.
  • Namespace WordPress\SMTP\, slug and text domain smtp.
  • Repository: github.com/wordpress/smtp

Constants

Constants defined in wp-config.php override the saved value and lock the field in the settings screen. They are the recommended way to provide credentials in production.

Constant Field
SMTP_HOST Host
SMTP_PORT Port
SMTP_ENCRYPTION Encryption (none, ssl or tls)
SMTP_USER Username
SMTP_PASSWORD Password
SMTP_FROM_EMAIL From email

Adding a provider

Providers are added with the smtp_providers filter. A provider configures the PHPMailer instance used by wp_mail(), so it works with any service that offers SMTP. Providers that send through an HTTP API are supported too, see below. The built-in Generic SMTP and Google (Gmail API) providers are registered through the same filter.

add_filter( 'smtp_providers', function ( $providers ) {
	$providers['my-provider'] = array(
		'label'     => 'My provider',
		'required'  => array( 'api_user' ),
		'fields'    => array(
			'api_user' => array(
				'label'    => 'API user',
				'type'     => 'text',
				'constant' => 'MY_PROVIDER_USER',
			),
			'api_key'  => array(
				'label'    => 'API key',
				'type'     => 'password',
				'constant' => 'MY_PROVIDER_KEY',
			),
		),
		'configure' => function ( $phpmailer, $values ) {
			$phpmailer->isSMTP();
			$phpmailer->Host     = 'smtp.my-provider.example';
			$phpmailer->Port     = 587;
			$phpmailer->SMTPAuth = true;
			$phpmailer->Username = $values['api_user'];
			$phpmailer->Password = $values['api_key'];
		},
	);

	return $providers;
} );
Key Description
label (string) Name shown in the provider select. Required.
configure (callable) Receives the PHPMailer instance and the resolved field values. Required unless send is set.
send (callable) For providers that send through an HTTP API instead of SMTP. See below.
fields (array) Settings fields, keyed by field key.
required (string[]) Field keys that must have a value before the provider is used. Until then, mail is sent as usual.
required_if (array) Field keys that must have a value when another field is enabled, as field => condition field pairs. For example array( 'password' => 'auth' ).
rate_limits (array) Optional default sending limits, see Sending limits.

Each field accepts:

Key Description
label Field label.
type text (default), number, email, password, select or checkbox.
default Value used when nothing is saved.
options Value => label pairs, for select.
description Help text.
constant Name of a constant that overrides the value and locks the field.
max Highest accepted value of a number field. Higher values are rejected by wp smtp settings set and reset to the default on the settings screen, with a notice.

Fields of type password are stored apart from the other settings, are never printed back into the page, and are redacted from the connection log of the test email.

Providers with an invalid definition (no label, no configure or send, or one that is not callable) are ignored.

HTTP API providers

A provider that does not use SMTP sets a send callback. wp_mail() still builds the email as usual: it parses the headers, recipients and attachments, applies the From, content type and charset filters, and runs phpmailer_init. Then, instead of sending it, PHPMailer calls send with the ready PHPMailer instance and the resolved field values.

'send' => function ( $phpmailer, $values ) {
	$response = wp_remote_post(
		'https://api.my-provider.example/send',
		array(
			'timeout' => $phpmailer->Timeout,
			'headers' => array( 'Authorization' => 'Bearer ' . $values['api_key'] ),
			'body'    => array(
				'from'    => $phpmailer->From,
				'to'      => wp_list_pluck( $phpmailer->getToAddresses(), 0 ),
				'subject' => $phpmailer->Subject,
				'html'    => $phpmailer->Body,
				'text'    => $phpmailer->AltBody,
			),
		)
	);

	if ( is_wp_error( $response ) ) {
		return $response;
	}

	if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
		return new WP_Error( 'my_provider_error', wp_remote_retrieve_body( $response ) );
	}

	return true;
},
  • Return true when the email was accepted, or a WP_Error. Its message becomes the wp_mail_failed error, so it shows up in the email log, the alerts and the test email result. Values of password fields are redacted from it, also when they are base64, URL or JSON encoded.
  • The email is available through the usual PHPMailer accessors: getToAddresses(), getCcAddresses(), getBccAddresses(), getReplyToAddresses(), getAttachments(), getCustomHeaders(), Body, AltBody, ContentType and CharSet. APIs that accept a raw email can use getSentMIMEMessage().
  • Use $phpmailer->Timeout (see smtp_timeout) as the request timeout. There are no automatic retries.
  • Never call wp_mail() from send. That email fails, so the one in progress is not changed.
  • The plugin only uses its own PHPMailer when wp_mail() would create the default one. If another plugin already replaced it, the email is sent by that plugin, without the provider.

The built-in Google provider (includes/Providers/Google.php) is a complete example: it exchanges a refresh token for an OAuth access token, caches it in a transient, and posts the raw MIME message to the Gmail API.

Google (Gmail API)

Slug google. Fields and constants:

Field Constant Type
google_client_id SMTP_GOOGLE_CLIENT_ID text
google_client_secret SMTP_GOOGLE_CLIENT_SECRET password
google_refresh_token SMTP_GOOGLE_REFRESH_TOKEN password

The refresh token needs the https://www.googleapis.com/auth/gmail.send scope. Access tokens are cached per client ID and refresh token in smtp_google_token_* transients, until a minute before they expire, and dropped when Gmail answers 401. Gmail sends from the account of the refresh token, or from one of its verified Send mail as addresses.

Multiple connections

A connection is a provider with its own settings, credentials, From address and sending limits. The settings saved under Tools > SMTP > Settings are the default connection, so existing sites keep working unchanged. Add the others from the same Settings tab with Add connection: the ID used in code (such as sponsors) is generated from the name.

Emails use the default connection, unless the connection is picked for that email:

// Send one email through a connection.
wp_smtp_mail( 'sponsors', 'recipient@example.com', 'Subject', 'Message' );

// Or route emails you do not send yourself, such as the ones of another plugin.
add_filter( 'smtp_connection_for_email', function ( $connection_id, $mail_data ) {
	if ( false !== strpos( (string) $mail_data['subject'], 'Sponsor' ) ) {
		return 'sponsors';
	}

	return $connection_id;
}, 10, 2 );

wp_smtp_mail() takes the connection ID first and then the arguments of wp_mail(). The filter receives the connection ID (default unless another callback changed it) and the email data (to, subject, message, headers and attachments). An unknown connection, or one that is not fully configured, falls back to the default connection, so the email is still sent. The smtp_connection_fallback action fires in that case, with the ID that was asked for and the reason (unknown or incomplete), so you can log it or alert someone.

Other plugins list the connections without any access to the credentials:

foreach ( wp_smtp_get_connections() as $id => $connection ) {
	// $connection['label'], ['provider'], ['configured'] and ['rate_limits'].
}

Notes:

  • The mailbox used to authenticate (the username) and the From address are separate fields, so a connection can send from a different address than the one it logs in with. A provider can still reject From addresses it does not own.
  • Constants such as SMTP_HOST only configure the default connection.
  • Each connection has its own sending limits, see Sending limits. The smtp_rate_limits filter also receives the connection ID.
  • The email log has a Connection column once there is more than one connection.
  • Deleting a connection also deletes its saved credentials. wp smtp commands only handle the default connection for now.

Sending limits

Email providers limit how much you can send. A provider can declare its limits with the optional rate_limits key, and every value is optional, since not every provider publishes a limit for every interval:

$providers['my-provider'] = array(
	// ...
	'rate_limits' => array(
		'per_second'  => 2,
		'per_minute'  => 60,
		'per_hour'    => 500,
		'per_day'     => 2000,
		'concurrency' => 1,
		// For services that count recipients instead of messages.
		'recipients'  => array( 'per_day' => 5000 ),
	),
);
Key Description
per_second, per_minute, per_hour, per_day Maximum number of messages in that interval.
concurrency Maximum number of simultaneous connections or sending operations.
recipients The same interval keys, for the maximum number of recipients.

Limits resolve in this order:

  1. The default declared by the provider.
  2. The value saved under Tools > SMTP > Sending limits, for the active provider. It covers the message limits and concurrency, since the right values often depend on the account or plan.
  3. The smtp_rate_limits filter, which receives $limits and the provider slug.

Other plugins read the result with wp_smtp_get_rate_limits( $provider = '' ), which defaults to the active provider of the default connection. For the limits of each connection use wp_smtp_get_connections(). Keys without a known limit are missing, so the result is an empty array when there are none:

if ( function_exists( 'wp_smtp_get_rate_limits' ) ) {
	$limits = wp_smtp_get_rate_limits();
	$per_minute = $limits['per_minute'] ?? null;
}

The values are configured limits, not the remaining capacity: the same account may be shared with other applications. The plugin does not queue, delay or throttle email. It only reports the limits, so queue plugins can use them to schedule messages.

WP-CLI

Set up and check a site without the admin screen:

wp smtp settings set host=smtp.example.com port=587 encryption=tls username=me@example.com
printf '%s' "$SMTP_PASSWORD" | wp smtp settings set password=-
wp smtp test someone@example.com
wp smtp status --format=json
Command Description
wp smtp settings set <setting>... Changes the given settings and keeps the others. Nothing is saved when a value is invalid.
wp smtp test [<to>] [--connection-log] Sends a test email, to the admin email by default. Exits with a non-zero code on failure, with a hint on what to check when the problem is recognized.
wp smtp status [--format=<format>] Shows the active provider, each setting and whether it comes from a constant, the database or the default.

Like saving the admin form, settings set saves every field, so afterwards status reports database for all of them. The exceptions are fields set by a constant, which show constant, and passwords that were never set, which show default.

Passwords cannot be passed as arguments, since those end up in the shell history. Use password=- to read the value from STDIN, or to get a hidden prompt in a terminal. Passwords are never printed, and they are removed from the output of --connection-log.

Other filters

Filter Description
smtp_log_enabled Return false to skip logging an email. Receives $enabled, $mail_data and $status (sent or failed).
smtp_log_retention_days Number of days log entries are kept. Default 30. A value of 0 or less disables the purge.
smtp_failure_notice_period Seconds the admin notice about failed emails looks back. Default one day.
smtp_rate_limits Change the sending limits of a provider. Receives $limits, the provider slug and the connection ID. See Sending limits.
smtp_connection_for_email Pick the connection an email is sent through. Receives $connection_id (default) and $mail_data. See Multiple connections.
smtp_connection_fallback Action. Fires when an email asked for a connection that does not exist or is not fully configured and goes through the default one. Receives $connection_id, $reason (unknown or incomplete) and $mail_data.
smtp_timeout Seconds to wait for the SMTP server to connect and to answer each command. Default 15. A value of 0 or less keeps the PHPMailer default of 300 seconds.
smtp_alert_channels Add alert channels. Receives an array of WordPress\SMTP\Alerts\Alert_Channel instances.
smtp_alert_enabled Return false to skip the alert for a failed email. Receives $enabled and the Alert.
smtp_alert_throttle Minimum number of seconds between two alerts on the same channel. Default 900. Receives $seconds and the channel ID. 0 sends an alert for every failure.

Migration from the legacy plugin

This plugin replaces the SMTP plugin by Sorin Iclanzan (1.x) that was published under the same slug, so sites upgrade in place and the activation hook does not run. WordPress\SMTP\Migration imports the legacy settings once, on init (and on activation).

Legacy (smtp_options) This plugin
host Host, without a scheme
port Port (25 when invalid)
smtp_secure ('', ssl, tls) Encryption (none, ssl, tls)
username Username. Authentication is on when it is not empty, like before
password Password, decrypted and stored as a secret
(always the first user) From email and From name of user 1, with "Force from" on
  • Nothing is imported when smtp_settings already exists, or when the legacy plugin still has its activation placeholder (http://localhost, no username).
  • The legacy option is never modified. It is deleted on uninstall, together with smtp_legacy_migrated and smtp_migration_notice.
  • The legacy plugin encrypted the password with mcrypt (Rijndael-256, CBC) and a key hardcoded in its source. mcrypt was removed in PHP 7.2 and nothing else supports Rijndael-256, so the password is only imported on servers that still provide it. Otherwise an admin notice asks the user to enter it again.

Email log

Every email sent with wp_mail() is logged in the {prefix}smtp_logs table with its recipient, subject, status, error and provider. The message body, headers and attachments are never stored. A daily cron event (smtp_purge_logs) deletes old entries.

Alerts

The Alerts tab sends a notification outside of email when wp_mail() fails, because a broken mailer cannot report itself. Slack (Incoming Webhook) is the first channel.

  • Pick the channel in Send failure alerts via (None by default). Only the fields of the selected channel are shown, and alerts are sent once it is configured. The fields of the other channels are kept.
  • The Slack webhook URL is stored as a secret through Credentials and never printed back.
  • To avoid flooding a channel, at most one alert is sent every 15 minutes per channel. Failures in between are counted and reported with the next alert.
  • When an alert cannot be delivered, the error is shown in the tab. It never triggers another alert.
  • The message contains the error, the recipients and the subject (the same metadata as the email log), and a link to the failed entries of the log.

Add a channel by implementing WordPress\SMTP\Alerts\Alert_Channel and registering it:

add_filter( 'smtp_alert_channels', function ( $channels ) {
	$channels[] = new My_Teams_Channel();

	return $channels;
} );

A channel declares its fields (text, url or password), sanitizes them, says whether it is configured and delivers an Alert. It must never use wp_mail().

Development

composer install        # PHP dependencies and coding standards tooling
composer lint           # Run PHPCS
composer format         # Fix coding standards issues
npm ci                  # Node dependencies (@wordpress/env)
npm run wp-env:start:tests
npm run test-php        # Run PHPUnit in the test stack (requires Docker)

Run a single test class or method:

npm run test-php -- --filter Settings_Tests

See AGENTS.md for the project structure, coding standards and test requirements.

Releasing

Build the distribution with production dependencies only:

composer install --no-dev -o

Files that are not part of the release are listed in .distignore.

License

GPL-2.0-or-later.

About

No description, website, or topics provided.

Resources

Code of conduct

Security policy

Stars

31 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages