wppaste
WordPress

wp_mail( string|string[] $to, string $subject, string $message, string|string[] $headers = '', string|string[] $attachments = array() ): bool

Since
1.2.1, 5.5.0
Source
wp-includes/pluggable.php:174
Sends an email, similar to PHP's mail function.

Description

A true return value does not automatically mean that the user received the email successfully. It just only means that the method used was able to process the request without any errors.

The default content type is text/plain which does not allow using HTML.
However, you can set the content type of the email by using the 'wp_mail_content_type' filter.

The default charset is based on the charset used on the blog. The charset can be set using the 'wp_mail_charset' filter.

Compatibility

WordPress
since 5.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$tostring|string[]
Array or comma-separated list of email addresses to send message.
$subjectstring
Email subject.
$messagestring
Message contents.
$headersstring|string[]optional
Additional headers.Default: ''
$attachmentsstring|string[]optional
Paths to files to attach.Default: array()

Return value

bool
Whether the email was sent successfully.

Performance profile

How much work a call to wp_mail() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Expensive

Sends mail.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
6

Executed per call on PHP 8.5. The body compiles to 543.

Plugin surface
9 hooks

Third-party callbacks on 'wp_mail', 'pre_wp_mail', 'wp_mail_from' run inside this call, and their cost is not bounded by anything here.

Called by
27

27 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • mailsends mailwp_mail()this function does it
  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()one call below wp_mail()

Further down the call graph this can also reach cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_mail() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always6is_email()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev543658
8.5543658
8.454365874 fewer instructions than PHP 8.3
8.3617658
8.26176582 more instructions than PHP 8.1
8.16156581 more instruction than PHP 7.4
7.4614658

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 10

10 hooks fire while wp_mail() runs, in this order:

  1. apply_filters( wp_mail )filterline 192 (+18 into the body)

    Filters the wp_mail() arguments.

  2. apply_filters( pre_wp_mail )filterline 214 (+40 into the body)

    Filters whether to preempt sending an email.

  3. apply_filters( wp_mail_from )filterline 396 (+222 into the body)

    Filters the email address to send from.

  4. apply_filters( wp_mail_from_name )filterline 405 (+231 into the body)

    Filters the name to associate with the "from" email address.

  5. do_action( wp_mail_failed )actionline 414 (+240 into the body)

    Fires after a PHPMailer\PHPMailer\Exception is caught.

  6. apply_filters( wp_mail_content_type )filterline 480 (+306 into the body)

    Filters the wp_mail() content type.

  7. apply_filters( wp_mail_charset )filterline 501 (+327 into the body)

    Filters the default wp_mail() charset.

  8. do_action( phpmailer_init )actionline 540 (+366 into the body)

    Fires after PHPMailer is initialized.

  9. do_action( wp_mail_succeeded )actionline 567 (+393 into the body)

    Fires after PHPMailer has successfully sent an email.

  10. do_action( wp_mail_failed )actionline 581 (+407 into the body)

    Fires after a PHPMailer\PHPMailer\Exception is caught.

Uses · 12

Used by · 27

Show all 27

Source code

	function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {		// Compact the input, apply the filters, and extract them back out. 		/**		 * Filters the wp_mail() arguments.		 *		 * @since 2.2.0		 *		 * @param array $args {		 *     Array of the `wp_mail()` arguments.		 *		 *     @type string|string[] $to          Array or comma-separated list of email addresses to send message.		 *     @type string          $subject     Email subject.		 *     @type string          $message     Message contents.		 *     @type string|string[] $headers     Additional headers.		 *     @type string|string[] $attachments Paths to files to attach.		 * }		 */		$atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ); 		/**		 * Filters whether to preempt sending an email.		 *		 * Returning a non-null value will short-circuit {@see wp_mail()}, returning		 * that value instead. A boolean return value should be used to indicate whether		 * the email was successfully sent.		 *		 * @since 5.7.0		 *		 * @param null|bool $return Short-circuit return value.		 * @param array     $atts {		 *     Array of the `wp_mail()` arguments.		 *		 *     @type string|string[] $to          Array or comma-separated list of email addresses to send message.		 *     @type string          $subject     Email subject.		 *     @type string          $message     Message contents.		 *     @type string|string[] $headers     Additional headers.		 *     @type string|string[] $attachments Paths to files to attach.		 * }		 */		$pre_wp_mail = apply_filters( 'pre_wp_mail', null, $atts ); 		if ( null !== $pre_wp_mail ) {			return $pre_wp_mail;		} 		if ( isset( $atts['to'] ) ) {			$to = $atts['to'];		} 		if ( ! is_array( $to ) ) {			$to = explode( ',', $to );		} 		if ( isset( $atts['subject'] ) ) {			$subject = $atts['subject'];		} 		if ( isset( $atts['message'] ) ) {			$message = $atts['message'];		} 		if ( isset( $atts['headers'] ) ) {			$headers = $atts['headers'];		} 		if ( isset( $atts['attachments'] ) ) {			$attachments = $atts['attachments'];		} 		if ( ! is_array( $attachments ) ) {			$attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );		}		global $phpmailer; 		// (Re)create it, if it's gone missing.		if ( ! ( $phpmailer instanceof PHPMailer\PHPMailer\PHPMailer ) ) {			require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';			require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php';			require_once ABSPATH . WPINC . '/PHPMailer/Exception.php';

Changelog

Introduced in 1.2.1. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.9.7
Parameter $embeds added.verified against source
5.5.0
is_email() is used for email validation, instead of PHPMailer's default validator.from the docblock
1.2.1
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/pluggable.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.